Fix formatting and interfaces in nearly every file
This commit is contained in:
@@ -2,16 +2,19 @@
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Interfaces\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
use Webpatser\Uuid\Uuid;
|
||||
|
||||
|
||||
class DatabaseService extends BaseService
|
||||
/**
|
||||
* Class DatabaseService
|
||||
* @package App\Services
|
||||
*/
|
||||
class DatabaseService extends Service
|
||||
{
|
||||
|
||||
protected $time_fields = [
|
||||
'created_at',
|
||||
'updated_at'
|
||||
@@ -23,55 +26,57 @@ class DatabaseService extends BaseService
|
||||
'pireps',
|
||||
];
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function time(): string
|
||||
{
|
||||
return Carbon::now('UTC')->format('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $yaml_file
|
||||
* @param $yaml_file
|
||||
* @param bool $ignore_errors
|
||||
* @return array
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function seed_from_yaml_file($yaml_file, $ignore_errors=false): array
|
||||
public function seed_from_yaml_file($yaml_file, $ignore_errors = false): array
|
||||
{
|
||||
$yml = file_get_contents($yaml_file);
|
||||
|
||||
return $this->seed_from_yaml($yml, $ignore_errors);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $yml
|
||||
* @param $yml
|
||||
* @param bool $ignore_errors
|
||||
* @return array
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function seed_from_yaml($yml, $ignore_errors=false): array
|
||||
public function seed_from_yaml($yml, $ignore_errors = false): array
|
||||
{
|
||||
$imported = [];
|
||||
$yml = Yaml::parse($yml);
|
||||
foreach ($yml as $table => $rows) {
|
||||
|
||||
$imported[$table] = 0;
|
||||
|
||||
foreach ($rows as $row) {
|
||||
|
||||
# see if this table uses a UUID as the PK
|
||||
# if no ID is specified
|
||||
if(in_array($table, $this->uuid_tables)) {
|
||||
if(!array_key_exists('id', $row)) {
|
||||
if (in_array($table, $this->uuid_tables)) {
|
||||
if (!array_key_exists('id', $row)) {
|
||||
$row['id'] = Uuid::generate()->string;
|
||||
}
|
||||
}
|
||||
|
||||
# encrypt any password fields
|
||||
if(array_key_exists('password', $row)) {
|
||||
if (array_key_exists('password', $row)) {
|
||||
$row['password'] = bcrypt($row['password']);
|
||||
}
|
||||
|
||||
# if any time fields are == to "now", then insert the right time
|
||||
foreach($this->time_fields as $tf) {
|
||||
if(array_key_exists($tf, $row) && strtolower($row[$tf]) === 'now') {
|
||||
foreach ($this->time_fields as $tf) {
|
||||
if (array_key_exists($tf, $row) && strtolower($row[$tf]) === 'now') {
|
||||
$row[$tf] = $this->time();
|
||||
}
|
||||
}
|
||||
@@ -79,8 +84,8 @@ class DatabaseService extends BaseService
|
||||
try {
|
||||
DB::table($table)->insert($row);
|
||||
++$imported[$table];
|
||||
} catch(QueryException $e) {
|
||||
if($ignore_errors) {
|
||||
} catch (QueryException $e) {
|
||||
if ($ignore_errors) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user