Move some database calls into a support class

This commit is contained in:
Nabeel Shahzad 2018-06-20 11:18:30 -05:00
parent a083f9c671
commit 9bc3a67c9a
3 changed files with 101 additions and 43 deletions

View File

@ -3,9 +3,11 @@
namespace App\Services; namespace App\Services;
use App\Interfaces\Service; use App\Interfaces\Service;
use App\Support\Database;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Database\QueryException; use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Symfony\Component\VarDumper\Cloner\Data;
use Symfony\Component\Yaml\Yaml; use Symfony\Component\Yaml\Yaml;
use Webpatser\Uuid\Uuid; use Webpatser\Uuid\Uuid;
@ -42,8 +44,7 @@ class DatabaseService extends Service
*/ */
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 Database::seed_from_yaml_file($yaml_file, $ignore_errors);
return $this->seed_from_yaml($yml, $ignore_errors);
} }
/** /**
@ -54,27 +55,7 @@ class DatabaseService extends Service
*/ */
public function seed_from_yaml($yml, $ignore_errors = false): array public function seed_from_yaml($yml, $ignore_errors = false): array
{ {
$imported = []; return Database::seed_from_yaml($yml, $ignore_errors);
$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;
} }
/** /**
@ -92,24 +73,6 @@ class DatabaseService extends Service
} }
} }
# encrypt any password fields return Database::insert_row($table, $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($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;
} }
} }

95
app/Support/Database.php Normal file
View 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;
}
}

View File

@ -53,7 +53,7 @@ return [
'config' => ['path' => 'Config', 'generate' => true], 'config' => ['path' => 'Config', 'generate' => true],
'command' => ['path' => 'Console', 'generate' => true], 'command' => ['path' => 'Console', 'generate' => true],
'migration' => ['path' => 'Database/migrations', '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], 'factory' => ['path' => 'Database/factories', 'generate' => true],
'model' => ['path' => 'Models', 'generate' => true], 'model' => ['path' => 'Models', 'generate' => true],
'controller' => ['path' => 'Http/Controllers', 'generate' => true], 'controller' => ['path' => 'Http/Controllers', 'generate' => true],