Move some database calls into a support class
This commit is contained in:
parent
a083f9c671
commit
9bc3a67c9a
@ -3,9 +3,11 @@
|
||||
namespace App\Services;
|
||||
|
||||
use App\Interfaces\Service;
|
||||
use App\Support\Database;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Symfony\Component\VarDumper\Cloner\Data;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
use Webpatser\Uuid\Uuid;
|
||||
|
||||
@ -42,8 +44,7 @@ class DatabaseService extends Service
|
||||
*/
|
||||
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);
|
||||
return Database::seed_from_yaml_file($yaml_file, $ignore_errors);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -54,27 +55,7 @@ class DatabaseService extends Service
|
||||
*/
|
||||
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) {
|
||||
try {
|
||||
$row = $this->insert_row($table, $row);
|
||||
} catch(QueryException $e) {
|
||||
if ($ignore_errors) {
|
||||
continue;
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
++$imported[$table];
|
||||
}
|
||||
}
|
||||
|
||||
return $imported;
|
||||
return Database::seed_from_yaml($yml, $ignore_errors);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -92,24 +73,6 @@ class DatabaseService extends Service
|
||||
}
|
||||
}
|
||||
|
||||
# encrypt any password fields
|
||||
if (array_key_exists('password', $row)) {
|
||||
$row['password'] = bcrypt($row['password']);
|
||||
}
|
||||
|
||||
# if any time fields are == to "now", then insert the right time
|
||||
foreach($row as $column => $value) {
|
||||
if(strtolower($value) === 'now') {
|
||||
$row[$column] = $this->time();
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
DB::table($table)->insert($row);
|
||||
} catch (QueryException $e) {
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return $row;
|
||||
return Database::insert_row($table, $row);
|
||||
}
|
||||
}
|
||||
|
95
app/Support/Database.php
Normal file
95
app/Support/Database.php
Normal file
@ -0,0 +1,95 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
use Webpatser\Uuid\Uuid;
|
||||
|
||||
class Database
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected static function time(): string
|
||||
{
|
||||
return Carbon::now('UTC');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $yaml_file
|
||||
* @param bool $ignore_errors
|
||||
* @return array
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function seed_from_yaml_file($yaml_file, $ignore_errors = false): array
|
||||
{
|
||||
$yml = file_get_contents($yaml_file);
|
||||
return static::seed_from_yaml($yml, $ignore_errors);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $yml
|
||||
* @param bool $ignore_errors
|
||||
* @return array
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static 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) {
|
||||
try {
|
||||
static::insert_row($table, $row);
|
||||
} catch (QueryException $e) {
|
||||
if ($ignore_errors) {
|
||||
continue;
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
++$imported[$table];
|
||||
}
|
||||
}
|
||||
|
||||
return $imported;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $table
|
||||
* @param $row
|
||||
* @return mixed
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function insert_row($table, $row)
|
||||
{
|
||||
# encrypt any password fields
|
||||
if (array_key_exists('password', $row)) {
|
||||
$row['password'] = bcrypt($row['password']);
|
||||
}
|
||||
|
||||
# if any time fields are == to "now", then insert the right time
|
||||
foreach ($row as $column => $value) {
|
||||
if (strtolower($value) === 'now') {
|
||||
$row[$column] = static::time();
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
DB::table($table)->insert($row);
|
||||
} catch (QueryException $e) {
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return $row;
|
||||
}
|
||||
}
|
@ -53,7 +53,7 @@ return [
|
||||
'config' => ['path' => 'Config', 'generate' => true],
|
||||
'command' => ['path' => 'Console', 'generate' => true],
|
||||
'migration' => ['path' => 'Database/migrations', 'generate' => true],
|
||||
'seeder' => ['path' => 'Database/seeders', 'generate' => true],
|
||||
'seeds' => ['path' => 'Database/seeds', 'generate' => true],
|
||||
'factory' => ['path' => 'Database/factories', 'generate' => true],
|
||||
'model' => ['path' => 'Models', 'generate' => true],
|
||||
'controller' => ['path' => 'Http/Controllers', 'generate' => true],
|
||||
|
Loading…
Reference in New Issue
Block a user