2019-11-27 22:19:20 +08:00
|
|
|
<?php
|
|
|
|
|
2019-12-13 04:07:35 +08:00
|
|
|
namespace Modules\Importer\Services;
|
2019-11-27 22:19:20 +08:00
|
|
|
|
|
|
|
use App\Contracts\Service;
|
|
|
|
use App\Repositories\KvpRepository;
|
2019-12-02 22:57:35 +08:00
|
|
|
use Exception;
|
2019-11-27 22:19:20 +08:00
|
|
|
use Illuminate\Http\Request;
|
2020-02-24 08:48:28 +08:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2019-12-13 04:07:35 +08:00
|
|
|
use Modules\Importer\Services\Importers\AircraftImporter;
|
|
|
|
use Modules\Importer\Services\Importers\AirlineImporter;
|
|
|
|
use Modules\Importer\Services\Importers\AirportImporter;
|
|
|
|
use Modules\Importer\Services\Importers\ClearDatabase;
|
|
|
|
use Modules\Importer\Services\Importers\FinalizeImporter;
|
|
|
|
use Modules\Importer\Services\Importers\FlightImporter;
|
|
|
|
use Modules\Importer\Services\Importers\GroupImporter;
|
|
|
|
use Modules\Importer\Services\Importers\PirepImporter;
|
|
|
|
use Modules\Importer\Services\Importers\RankImport;
|
|
|
|
use Modules\Importer\Services\Importers\UserImport;
|
2019-11-27 22:19:20 +08:00
|
|
|
|
|
|
|
class ImporterService extends Service
|
|
|
|
{
|
|
|
|
private $CREDENTIALS_KEY = 'legacy.importer.db';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var KvpRepository
|
|
|
|
*/
|
|
|
|
private $kvpRepo;
|
|
|
|
|
|
|
|
/**
|
2019-12-02 22:57:35 +08:00
|
|
|
* The list of importers, in proper order
|
2019-11-27 22:19:20 +08:00
|
|
|
*/
|
2019-12-02 22:57:35 +08:00
|
|
|
private $importList = [
|
|
|
|
ClearDatabase::class,
|
|
|
|
RankImport::class,
|
|
|
|
GroupImporter::class,
|
|
|
|
AirlineImporter::class,
|
|
|
|
AircraftImporter::class,
|
|
|
|
AirportImporter::class,
|
|
|
|
FlightImporter::class,
|
|
|
|
UserImport::class,
|
|
|
|
PirepImporter::class,
|
|
|
|
FinalizeImporter::class,
|
|
|
|
];
|
2019-11-27 22:19:20 +08:00
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->kvpRepo = app(KvpRepository::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the credentials from a request
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
*/
|
|
|
|
public function saveCredentialsFromRequest(Request $request)
|
|
|
|
{
|
|
|
|
$creds = [
|
|
|
|
'admin_email' => $request->post('email'),
|
|
|
|
'host' => $request->post('db_host'),
|
|
|
|
'port' => $request->post('db_port'),
|
|
|
|
'name' => $request->post('db_name'),
|
|
|
|
'user' => $request->post('db_user'),
|
|
|
|
'pass' => $request->post('db_pass'),
|
|
|
|
'table_prefix' => $request->post('db_prefix'),
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->saveCredentials($creds);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the given credentials
|
|
|
|
*
|
|
|
|
* @param array $creds
|
|
|
|
*/
|
|
|
|
public function saveCredentials(array $creds)
|
|
|
|
{
|
|
|
|
$creds = array_merge([
|
|
|
|
'admin_email' => '',
|
|
|
|
'host' => '',
|
|
|
|
'port' => '',
|
|
|
|
'name' => '',
|
|
|
|
'user' => '',
|
|
|
|
'pass' => 3306,
|
|
|
|
'table_prefix' => 'phpvms_',
|
|
|
|
], $creds);
|
|
|
|
|
|
|
|
$this->kvpRepo->save($this->CREDENTIALS_KEY, $creds);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the saved credentials
|
|
|
|
*/
|
|
|
|
public function getCredentials()
|
|
|
|
{
|
|
|
|
return $this->kvpRepo->get($this->CREDENTIALS_KEY);
|
|
|
|
}
|
|
|
|
|
2019-12-02 22:57:35 +08:00
|
|
|
/**
|
|
|
|
* Create a manifest of the import. Creates an array with the importer name,
|
|
|
|
* which then has a subarray of all of the different steps/stages it needs to run
|
|
|
|
*/
|
|
|
|
public function generateImportManifest()
|
|
|
|
{
|
|
|
|
$manifest = [];
|
|
|
|
|
|
|
|
foreach ($this->importList as $importerKlass) {
|
2019-12-13 04:07:35 +08:00
|
|
|
/** @var \Modules\Importer\Services\BaseImporter $importer */
|
2019-12-02 22:57:35 +08:00
|
|
|
$importer = new $importerKlass();
|
|
|
|
$manifest = array_merge($manifest, $importer->getManifest());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $manifest;
|
|
|
|
}
|
|
|
|
|
2019-11-27 22:19:20 +08:00
|
|
|
/**
|
|
|
|
* Run a given stage
|
|
|
|
*
|
2019-12-02 22:57:35 +08:00
|
|
|
* @param $importer
|
2019-11-27 22:19:20 +08:00
|
|
|
* @param int $start
|
|
|
|
*
|
2019-12-02 22:57:35 +08:00
|
|
|
* @throws \Exception
|
2019-11-27 22:19:20 +08:00
|
|
|
*
|
|
|
|
* @return int|void
|
|
|
|
*/
|
2019-12-02 22:57:35 +08:00
|
|
|
public function run($importer, $start = 0)
|
2019-11-27 22:19:20 +08:00
|
|
|
{
|
2019-12-02 22:57:35 +08:00
|
|
|
if (!in_array($importer, $this->importList)) {
|
|
|
|
throw new Exception('Unknown importer "'.$importer.'"');
|
|
|
|
}
|
2019-11-27 22:19:20 +08:00
|
|
|
|
2019-12-13 04:07:35 +08:00
|
|
|
/** @var $importerInst \Modules\Importer\Services\BaseImporter */
|
2019-12-02 22:57:35 +08:00
|
|
|
$importerInst = new $importer();
|
2020-02-24 08:48:28 +08:00
|
|
|
|
|
|
|
try {
|
|
|
|
$importerInst->run($start);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
Log::error('Error running importer: '.$e->getMessage());
|
|
|
|
}
|
2019-11-27 22:19:20 +08:00
|
|
|
}
|
|
|
|
}
|