Skip to content

Commit

Permalink
Merge branch 'dev' into TrashBin
Browse files Browse the repository at this point in the history
  • Loading branch information
FatihKoz authored Oct 6, 2023
2 parents ca4a42b + a1fc564 commit 3d9b0a2
Show file tree
Hide file tree
Showing 36 changed files with 4,454 additions and 3,406 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ storage/*.key
storage/settings.json
storage/*.sqlite
.env.php
.env.old
.envrc
.env
.github/scripts/env.test
.vagrant
Expand Down
27 changes: 7 additions & 20 deletions app/Console/Commands/CreateConfigs.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Contracts\Command;
use App\Services\Installer\ConfigService;
use App\Services\Installer\MigrationService;
use App\Services\Installer\SeederService;
use DatabaseSeeder;
use Illuminate\Support\Facades\App;
Expand All @@ -16,15 +17,12 @@ class CreateConfigs extends Command
protected $signature = 'phpvms:config {db_host} {db_name} {db_user} {db_pass}';
protected $description = 'Create the config files';

private DatabaseSeeder $databaseSeeder;
private SeederService $seederSvc;

public function __construct(DatabaseSeeder $databaseSeeder, SeederService $seederSvc)
{
public function __construct(
private readonly DatabaseSeeder $databaseSeeder,
private readonly SeederService $seederSvc,
private readonly MigrationService $migrationSvc,
) {
parent::__construct();

$this->databaseSeeder = $databaseSeeder;
$this->seederSvc = $seederSvc;
}

/**
Expand All @@ -39,17 +37,7 @@ public function handle()
// Reload the configuration
App::boot();

$this->info('Recreating database');
$this->call('database:create', [
'--reset' => true,
]);

$this->info('Running migrations');
$this->call('migrate:fresh', [
'--seed' => true,
]);

$this->seederSvc->syncAllSeeds();
$this->migrationSvc->runAllMigrations();

$this->info('Done!');
}
Expand Down Expand Up @@ -81,7 +69,6 @@ protected function writeConfigs()

$this->info('Regenerating the config files');
$cfgSvc->createConfigFiles([
'APP_ENV' => 'dev',
'SITE_NAME' => $this->argument('name'),
'DB_CONNECTION' => 'mysql',
'DB_HOST' => $this->argument('db_host'),
Expand Down
4 changes: 2 additions & 2 deletions app/Console/Commands/ImportFromClassicCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Console\Commands;

use App\Contracts\Command;
use App\Services\ImporterService;
use App\Services\LegacyImporterService;
use Illuminate\Support\Facades\Log;

class ImportFromClassicCommand extends Command
Expand All @@ -24,7 +24,7 @@ public function handle()
'table_prefix' => $this->argument('table_prefix'),
];

$importerSvc = new ImporterService();
$importerSvc = new LegacyImporterService();

$importerSvc->saveCredentials($creds);
$manifest = $importerSvc->generateImportManifest();
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/System/ImporterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace App\Http\Controllers\System;

use App\Contracts\Controller;
use App\Services\ImporterService;
use App\Services\Installer\DatabaseService;
use App\Services\LegacyImporterService;
use App\Support\Utils;
use Exception;
use Illuminate\Http\JsonResponse;
Expand All @@ -17,7 +17,7 @@ class ImporterController extends Controller
{
public function __construct(
private readonly DatabaseService $dbSvc,
private readonly ImporterService $importerSvc
private readonly LegacyImporterService $importerSvc
) {
Utils::disableDebugToolbar();
}
Expand Down
1 change: 1 addition & 0 deletions app/Models/Airport.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
* @property float lat
* @property float lon
* @property int elevation
* @property bool hub
*/
class Airport extends Model
{
Expand Down
2 changes: 1 addition & 1 deletion app/Services/ImportExport/AircraftImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function import(array $row, $index): bool
'registration' => $row['registration'],
], $row);
} catch (\Exception $e) {
$this->errorLog('Error in row '.$index.': '.$e->getMessage());
$this->errorLog('Error in row '.($index + 1).': '.$e->getMessage());
return false;
}

Expand Down
6 changes: 3 additions & 3 deletions app/Services/ImportExport/AirportImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ public function import(array $row, $index): bool
$row['id'] = $row['icao'];
$row['hub'] = get_truth_state($row['hub']);

if ($row['ground_handling_cost'] === null && $row['ground_handling_cost'] !== 0.0) {
if (!is_numeric($row['ground_handling_cost'])) {
$row['ground_handling_cost'] = (float) setting('airports.default_ground_handling_cost');
} else {
$row['ground_handling_cost'] = (float) $row['ground_handling_cost'];
}

if ($row['fuel_jeta_cost'] === null && $row['fuel_jeta_cost'] !== 0.0) {
if (!is_numeric($row['fuel_jeta_cost'])) {
$row['fuel_jeta_cost'] = (float) setting('airports.default_jet_a_fuel_cost');
} else {
$row['fuel_jeta_cost'] = (float) $row['fuel_jeta_cost'];
Expand All @@ -65,7 +65,7 @@ public function import(array $row, $index): bool
'id' => $row['icao'],
], $row);
} catch (\Exception $e) {
$this->errorLog('Error in row '.$index.': '.$e->getMessage());
$this->errorLog('Error in row '.($index + 1).': '.$e->getMessage());
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion app/Services/ImportExport/ExpenseImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function import(array $row, $index): bool
'name' => $row['name'],
], $row);
} catch (\Exception $e) {
$this->errorLog('Error in row '.$index.': '.$e->getMessage());
$this->errorLog('Error in row '.($index + 1).': '.$e->getMessage());
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion app/Services/ImportExport/FareImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function import(array $row, $index): bool
'code' => $row['code'],
], $row);
} catch (\Exception $e) {
$this->errorLog('Error in row '.$index.': '.$e->getMessage());
$this->errorLog('Error in row '.($index + 1).': '.$e->getMessage());
return false;
}

Expand Down
18 changes: 9 additions & 9 deletions app/Services/ImportExport/FlightImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function import(array $row, $index): bool
try {
$flight->save();
} catch (\Exception $e) {
$this->errorLog('Error in row '.$index.': '.$e->getMessage());
$this->errorLog('Error in row '.($index + 1).': '.$e->getMessage());
return false;
}

Expand All @@ -144,7 +144,7 @@ public function import(array $row, $index): bool
$this->processFares($flight, $row['fares']);
$this->processFields($flight, $row['fields']);

$this->log('Imported row '.$index);
$this->log('Imported row '.($index + 1));
return true;
}

Expand All @@ -162,31 +162,31 @@ protected function setDays($day_str)
}

$days = [];
if (strpos($day_str, '1') !== false) {
if (str_contains($day_str, '1')) {
$days[] = Days::MONDAY;
}

if (strpos($day_str, '2') !== false) {
if (str_contains($day_str, '2')) {
$days[] = Days::TUESDAY;
}

if (strpos($day_str, '3') !== false) {
if (str_contains($day_str, '3')) {
$days[] = Days::WEDNESDAY;
}

if (strpos($day_str, '4') !== false) {
if (str_contains($day_str, '4')) {
$days[] = Days::THURSDAY;
}

if (strpos($day_str, '5') !== false) {
if (str_contains($day_str, '5')) {
$days[] = Days::FRIDAY;
}

if (strpos($day_str, '6') !== false) {
if (str_contains($day_str, '6')) {
$days[] = Days::SATURDAY;
}

if (strpos($day_str, '7') !== false) {
if (str_contains($day_str, '7')) {
$days[] = Days::SUNDAY;
}

Expand Down
2 changes: 1 addition & 1 deletion app/Services/ImportExport/SubfleetImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function import(array $row, $index): bool
'type' => $row['type'],
], $row);
} catch (\Exception $e) {
$this->errorLog('Error in row '.$index.': '.$e->getMessage());
$this->errorLog('Error in row '.($index + 1).': '.$e->getMessage());
return false;
}

Expand Down
30 changes: 6 additions & 24 deletions app/Services/ImportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ protected function throwError($error, \Exception $e = null): void
public function openCsv($csv_file)
{
try {
$reader = Reader::createFromPath($csv_file);
$reader = Reader::createFromPath($csv_file, 'r');
$reader->setDelimiter(',');
$reader->setHeaderOffset(0);
$reader->setEnclosure('"');
$reader->setEscape('\\');
return $reader;
} catch (Exception $e) {
$this->throwError('Error opening CSV: '.$e->getMessage(), $e);
Expand All @@ -94,33 +96,13 @@ protected function runImport($file_path, ImportExport $importer): array
$first_header = $cols[0];

$first = true;
$records = $reader->getRecords($cols);
$header_rows = $reader->getHeader();
$records = $reader->getRecords($header_rows);
foreach ($records as $offset => $row) {
// check if the first row being read is the header
if ($first) {
$first = false;

if ($row[$first_header] !== $first_header) {
$this->throwError('CSV file doesn\'t seem to match import type');
}

continue;
}

// Do a sanity check on the number of columns first
if (!$importer->checkColumns($row)) {
$importer->errorLog('Number of columns in row doesn\'t match');
continue;
}

// turn it into a collection and run some filtering
$row = collect($row)->map(function ($val, $index) {
$val = trim($val);
if ($val === '') {
return;
}

return $val;
return str_ireplace(['\\n', '\\r'], '', $val);
})->toArray();

// Try to validate
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Services\Importers;
namespace App\Services\LegacyImporter;

use App\Models\Aircraft;
use App\Models\Airline;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Services\Importers;
namespace App\Services\LegacyImporter;

use App\Models\Airline;
use Illuminate\Support\Facades\Log;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Services\Importers;
namespace App\Services\LegacyImporter;

use App\Models\Airport;
use Illuminate\Database\QueryException;
Expand All @@ -25,32 +25,34 @@ public function run($start = 0)
'fuel_jeta_cost',
];

// Legacy name to current name
$set_if_exists = [
'ground_handling_cost' => 'ground_handling_cost',
'fuel_jeta_cost' => 'fuel_jeta_cost',
'tz' => 'timezone',
'elevation' => 'elevation',
'region' => 'region',
];

$count = 0;
$rows = $this->db->readRows($this->table, $this->idField, $start, $fields);
$rows = $this->db->readRows($this->table, $this->idField, $start);
foreach ($rows as $row) {
$ground_handling_cost = (float) $row->ground_handling_cost;
$fuel_jetA_cost = (float) $row->fuel_jeta_cost;

if (empty($ground_handling_cost)) {
$ground_handling_cost = 0;
}

if (empty($fuel_jetA_cost)) {
$fuel_jetA_cost = 0;
}

$attrs = [
'id' => trim($row->icao),
'icao' => trim($row->icao),
'name' => $row->name,
'country' => $row->country,
'lat' => $row->lat,
'lon' => $row->lng,
'hub' => $row->hub,
'ground_handling_cost' => $ground_handling_cost,
'fuel_jeta_cost' => $fuel_jetA_cost,
'id' => trim($row->icao),
'icao' => trim($row->icao),
'name' => $row->name,
'country' => $row->country,
'lat' => $row->lat,
'lon' => $row->lng,
'hub' => $row->hub,
];

foreach ($set_if_exists as $legacy_name => $current_name) {
if (property_exists($row, $legacy_name) && !empty($row->{$legacy_name})) {
$attrs[$current_name] = $row->{$legacy_name};
}
}

$w = ['id' => $attrs['id']];
//$airport = Airport::updateOrCreate($w, $attrs);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace App\Services\Importers;
namespace App\Services\LegacyImporter;

use App\Services\ImporterService;
use App\Services\Installer\LoggerTrait;
use App\Services\LegacyImporterService;
use App\Utils\IdMapper;
use App\Utils\ImporterDB;
use Carbon\Carbon;
Expand Down Expand Up @@ -48,7 +48,7 @@ abstract class BaseImporter

public function __construct()
{
$importerService = app(ImporterService::class);
$importerService = app(LegacyImporterService::class);
$this->db = new ImporterDB($importerService->getCredentials());
$this->idMapper = app(IdMapper::class);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Services\Importers;
namespace App\Services\LegacyImporter;

use App\Models\Acars;
use App\Models\Aircraft;
Expand Down
Loading

0 comments on commit 3d9b0a2

Please sign in to comment.