2020-03-10 22:10:27 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
use App\DataMapper\CompanySettings;
|
|
|
|
use App\Jobs\Util\StartMigration;
|
|
|
|
use App\Models\Account;
|
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\CompanyToken;
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
|
|
|
class ImportMigrations extends Command
|
|
|
|
{
|
|
|
|
use MakesHash;
|
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $signature = 'migrations:import {--path=}';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = 'Massively import the migrations.';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Faker\Generator
|
|
|
|
*/
|
|
|
|
private $faker;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new command instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2020-03-18 10:40:15 +01:00
|
|
|
$this->faker = \Faker\Factory::create();
|
2020-03-10 22:10:27 +01:00
|
|
|
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
$path = $this->option('path') ?? storage_path('migrations/import');
|
|
|
|
|
|
|
|
$directory = new \DirectoryIterator($path);
|
|
|
|
|
|
|
|
foreach ($directory as $file) {
|
|
|
|
if ($file->getExtension() === 'zip') {
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->info('Started processing: '.$file->getBasename().' at '.now());
|
2020-03-12 21:38:39 +01:00
|
|
|
StartMigration::dispatch($file->getRealPath(), $this->getUser(), $this->getUser()->companies()->first());
|
2020-03-10 22:10:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 21:38:22 +01:00
|
|
|
public function getUser(): User
|
2020-03-10 22:10:27 +01:00
|
|
|
{
|
2020-03-24 10:15:30 +01:00
|
|
|
$account = $this->getAccount();
|
|
|
|
$company = $this->getCompany($account);
|
|
|
|
|
2020-03-10 22:10:27 +01:00
|
|
|
$user = factory(\App\Models\User::class)->create([
|
2020-03-24 10:15:30 +01:00
|
|
|
'account_id' => $account->id,
|
2020-03-10 22:10:27 +01:00
|
|
|
'email' => $this->faker->email,
|
2020-09-06 11:38:10 +02:00
|
|
|
'confirmation_code' => $this->createDbHash(config('database.default')),
|
2020-03-10 22:10:27 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
$company_token = CompanyToken::create([
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'company_id' => $company->id,
|
|
|
|
'account_id' => $account->id,
|
|
|
|
'name' => 'test token',
|
|
|
|
'token' => \Illuminate\Support\Str::random(64),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$user->companies()->attach($company->id, [
|
|
|
|
'account_id' => $account->id,
|
|
|
|
'is_owner' => 1,
|
|
|
|
'is_admin' => 1,
|
|
|
|
'is_locked' => 0,
|
|
|
|
'notifications' => CompanySettings::notificationDefaults(),
|
|
|
|
'permissions' => '',
|
|
|
|
'settings' => null,
|
|
|
|
]);
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2020-03-12 21:38:22 +01:00
|
|
|
public function getAccount(): Account
|
2020-03-10 22:10:27 +01:00
|
|
|
{
|
|
|
|
return factory(\App\Models\Account::class)->create();
|
|
|
|
}
|
|
|
|
|
2020-03-12 21:38:22 +01:00
|
|
|
public function getCompany(Account $account): Company
|
2020-03-10 22:10:27 +01:00
|
|
|
{
|
|
|
|
$company = factory(Company::class)->create([
|
|
|
|
'account_id' => $account->id,
|
|
|
|
]);
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $account->default_company_id) {
|
|
|
|
$account->default_company_id = $company->id;
|
2020-04-19 12:29:58 +02:00
|
|
|
$account->save();
|
|
|
|
}
|
2020-03-10 22:10:27 +01:00
|
|
|
|
|
|
|
return $company;
|
|
|
|
}
|
|
|
|
}
|