1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Console/Commands/ImportMigrations.php

139 lines
3.3 KiB
PHP
Raw Normal View History

<?php
2020-09-07 12:18:56 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
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;
2020-11-18 11:46:36 +01:00
use App\Utils\Traits\AppSetup;
use App\Utils\Traits\MakesHash;
2020-10-28 11:10:49 +01:00
use DirectoryIterator;
use Faker\Factory;
use Faker\Generator;
use Illuminate\Console\Command;
2020-10-28 11:10:49 +01:00
use Illuminate\Support\Str;
class ImportMigrations extends Command
{
use MakesHash;
2020-11-18 11:46:36 +01:00
use AppSetup;
/**
* 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.';
/**
2020-10-28 11:10:49 +01:00
* @var Generator
*/
private $faker;
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
2020-10-28 11:10:49 +01:00
$this->faker = Factory::create();
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
2020-11-18 11:46:36 +01:00
$this->buildCache();
$path = $this->option('path') ?? storage_path('migrations/import');
2020-10-28 11:10:49 +01:00
$directory = new DirectoryIterator($path);
foreach ($directory as $file) {
if ($file->getExtension() === 'zip') {
$this->info('Started processing: '.$file->getBasename().' at '.now());
StartMigration::dispatch($file->getRealPath(), $this->getUser(), $this->getUser()->companies()->first());
}
}
}
public function getUser(): User
{
$account = $this->getAccount();
$company = $this->getCompany($account);
2020-10-01 13:34:05 +02:00
$user = User::factory()->create([
'account_id' => $account->id,
2020-11-18 10:35:09 +01:00
'email' => Str::random(10) . "@example.com",
'confirmation_code' => $this->createDbHash(config('database.default')),
]);
2020-11-16 11:29:17 +01:00
CompanyToken::unguard();
$company_token = CompanyToken::create([
'user_id' => $user->id,
'company_id' => $company->id,
'account_id' => $account->id,
2020-11-16 05:40:52 +01:00
'name' => 'First token',
2020-10-28 11:10:49 +01:00
'token' => Str::random(64),
]);
2020-11-16 05:40:52 +01:00
$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;
}
public function getAccount(): Account
{
2020-10-01 12:49:47 +02:00
return Account::factory()->create();
}
public function getCompany(Account $account): Company
{
2020-10-02 00:19:42 +02:00
$company = Company::factory()->create([
'account_id' => $account->id,
2020-11-23 13:55:04 +01:00
'is_disabled' => true,
]);
if (! $account->default_company_id) {
$account->default_company_id = $company->id;
$account->save();
}
return $company;
}
}