Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set the most common config values in .env instead of in the config file #44

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 80 additions & 28 deletions src/Console/LarexImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
use Illuminate\Support\Facades\File;
use InvalidArgumentException;
use Lukasss93\Larex\Contracts\Importer;
use Lukasss93\Larex\Contracts\Writer;
use Lukasss93\Larex\Exceptions\ImportException;
use Lukasss93\Larex\Support\CsvWriter;
use Lukasss93\Larex\Exceptions\WriterException;
use Throwable;

class LarexImportCommand extends Command
Expand All @@ -20,99 +21,141 @@ class LarexImportCommand extends Command
*/
protected $signature = 'larex:import
{importer? : Importer}
{--f|force : Overwrite CSV file if already exists}
{--include= : Languages allowed to import in the CSV}
{--exclude= : Languages not allowed to import in the CSV}
{writer? : Writer}
{--f|force : Overwrite desgination if already exists}
{--include= : Languages allowed to import}
{--exclude= : Languages not allowed to import}
{--skip-source-reordering : Skip source reordering}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Import entries into CSV file';
protected $description = 'Import entries into destination location depending on the writer';

/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
public function handle() : int
{
//get the importer name
$importerKey = $this->argument('importer') ?? config('larex.importers.default');
$importers = config('larex.importers.list');

//check if importer exists
if (!array_key_exists($importerKey, $importers)) {
if (! array_key_exists($importerKey, $importers))
{
$this->error("Importer '$importerKey' not found.");
$this->line('');
$this->info('Available importers:');
foreach ($importers as $key => $importer) {
foreach ($importers as $key => $importer)
{
$this->line("<fg=yellow>$key</> - {$importer::description()}");
}
$this->line('');

return 1;
}

//get the writer name
$writerKey = $this->argument('writer') ?? config('larex.writers.default');
$writers = config('larex.writers.list');
//check if writer exists
if (! array_key_exists($writerKey, $writers))
{
$this->error("Writer '$writerKey' not found.");
$this->line('');
$this->info('Available writers:');
foreach ($writers as $key => $writer)
{
$this->line("<fg=yellow>$key</> - {$writer::description()}");
}
$this->line('');

return 1;
}

//initialize importer
$importer = new $importers[$importerKey]();

//check if importer is valid
if (!($importer instanceof Importer)) {
if (! ($importer instanceof Importer))
{
$this->error(sprintf("Importer '%s' must implements %s interface.", $importerKey, Importer::class));

return 1;
}

//check file exists
if (!$this->option('force') && File::exists(csv_path())) {
if (! $this->option('force') && File::exists(csv_path()))
{
$this->error(sprintf("The '%s' already exists.", csv_path(true)));

return 1;
}

//check concurrent options
if ($this->option('include') !== null && $this->option('exclude') !== null) {
if ($this->option('include') !== null && $this->option('exclude') !== null)
{
$this->error('The --include and --exclude options can be used only one at a time.');

return 1;
}

$this->warn('Importing entries...');

try {
try
{
//call the importer
$items = $importer->handle($this);
} catch (ImportException $e) {
} catch (ImportException $e)
{
$this->error($e->getMessage());

return 1;
}

//check no data
if ($items->isEmpty()) {
if ($items->isEmpty())
{
$this->warn('No data found to import.');

return 0;
}

try {
try
{
//validate items structure
self::validateCollection($items);
} catch (Throwable $e) {
} catch (Throwable $e)
{
$this->error($e->getMessage());

return 1;
}

//initialize importer
$writer = new $writers[$writerKey]();
try
{
$writer->handle($items);
} catch (WriterException $e)
{
$this->error($e->getMessage());
return 1;
}

/*
//write csv
CsvWriter::create(csv_path())
->addRows($items->toArray());

*/
//set source languages
if (!$this->option('skip-source-reordering')) {
if (! $this->option('skip-source-reordering'))
{
$this->callSilently(LarexLangOrderCommand::class,
['from' => config('larex.source_language', 'en'), 'to' => 1]);
}
Expand All @@ -122,39 +165,48 @@ public function handle(): int
return 0;
}

protected static function validateCollection(Collection $rows): void
protected static function validateCollection(Collection $rows) : void
{
$compare = null;
foreach ($rows as $i => $columns) {
if (!is_array($columns)) {
foreach ($rows as $i => $columns)
{
if (! is_array($columns))
{
throw new InvalidArgumentException("The item must be an array at index $i.");
}

$keys = collect($columns)->keys();

if ($keys->get(0) !== 'group') {
if ($keys->get(0) !== 'group')
{
throw new InvalidArgumentException("The first key name of the item must be 'group' at index $i.");
}

if ($keys->get(1) !== 'key') {
if ($keys->get(1) !== 'key')
{
throw new InvalidArgumentException("The first key name of the item must be 'key' at index $i.");
}

if ($keys->count() <= 2) {
if ($keys->count() <= 2)
{
throw new InvalidArgumentException("There must be at least one language code at index $i.");
}

if ($compare === null) {
if ($compare === null)
{
$compare = $keys;
continue;
}

if ($keys->count() !== $compare->count()) {
if ($keys->count() !== $compare->count())
{
throw new InvalidArgumentException("All items in the collection must be the same length at index $i.");
}

foreach ($keys->skip(2) as $j => $key) {
if ($key !== $compare->get($j)) {
foreach ($keys->skip(2) as $j => $key)
{
if ($key !== $compare->get($j))
{
throw new InvalidArgumentException("All items in the collection must have the same keys values in the same position at index $i.");
}
}
Expand Down
25 changes: 25 additions & 0 deletions src/Contracts/Writer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Lukasss93\Larex\Contracts;

use Illuminate\Support\Collection;
use Lukasss93\Larex\Console\LarexImportCommand;
use Lukasss93\Larex\Exceptions\WriterException;

interface Writer
{
/**
* Writer description.
* @return string
*/
public static function description() : string;

/**
* Writer logic.
* @param Collection $data
* @return bool
* @throws WriterException
*/
public function handle(Collection $data) : self;

}
9 changes: 9 additions & 0 deletions src/Exceptions/WriterException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Lukasss93\Larex\Exceptions;

use Exception;

class WriterException extends Exception
{
}
Loading