forked from shlinkio/shlink-importer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request shlinkio#24 from acelaya-forks/feature/csv-import
Feature/csv import
- Loading branch information
Showing
27 changed files
with
505 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Shlinkio\Shlink\Importer\Sources\Csv; | ||
|
||
use DateTimeImmutable; | ||
use DateTimeInterface; | ||
use League\Csv\Reader; | ||
use Shlinkio\Shlink\Importer\Exception\ImportException; | ||
use Shlinkio\Shlink\Importer\Model\ImportedShlinkUrl; | ||
use Shlinkio\Shlink\Importer\Sources\ImportSources; | ||
use Shlinkio\Shlink\Importer\Strategy\ImporterStrategyInterface; | ||
|
||
use function array_filter; | ||
use function explode; | ||
use function Functional\reduce_left; | ||
use function str_replace; | ||
use function strtolower; | ||
use function trim; | ||
|
||
class CsvImporter implements ImporterStrategyInterface | ||
{ | ||
private const TAG_SEPARATOR = '|'; | ||
|
||
private ?DateTimeInterface $date; | ||
|
||
public function __construct(?DateTimeInterface $date = null) | ||
{ | ||
$this->date = $date; | ||
} | ||
|
||
/** | ||
* @return ImportedShlinkUrl[] | ||
* @throws ImportException | ||
*/ | ||
public function import(array $rawParams): iterable | ||
{ | ||
$params = CsvParams::fromRawParams($rawParams); | ||
$now = $this->date ?? new DateTimeImmutable(); | ||
|
||
$csvReader = Reader::createFromStream($params->stream())->setDelimiter($params->delimiter()) | ||
->setHeaderOffset(0); | ||
|
||
foreach ($csvReader as $record) { | ||
$record = $this->remapRecordHeaders($record); | ||
|
||
yield new ImportedShlinkUrl( | ||
ImportSources::CSV, | ||
$record['longurl'], | ||
$this->parseTags($record), | ||
$now, | ||
$this->nonEmptyValueOrNull($record, 'domain'), | ||
$record['shortcode'], | ||
$this->nonEmptyValueOrNull($record, 'title'), | ||
); | ||
} | ||
} | ||
|
||
private function remapRecordHeaders(array $record): array | ||
{ | ||
return reduce_left($record, static function ($value, string $index, array $c, array $acc) { | ||
$normalizedKey = strtolower(str_replace(' ', '', $index)); | ||
$acc[$normalizedKey] = $value; | ||
|
||
return $acc; | ||
}, []); | ||
} | ||
|
||
private function nonEmptyValueOrNull(array $record, string $key): ?string | ||
{ | ||
$value = $record[$key] ?? null; | ||
if (empty($value)) { | ||
return null; | ||
} | ||
|
||
$trimmedValue = trim($value); | ||
if (empty($trimmedValue)) { | ||
return null; | ||
} | ||
|
||
return $trimmedValue; | ||
} | ||
|
||
private function parseTags(array $record): array | ||
{ | ||
return array_filter(explode(self::TAG_SEPARATOR, $this->nonEmptyValueOrNull($record, 'tags') ?? '')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Shlinkio\Shlink\Importer\Sources\Csv; | ||
|
||
final class CsvParams | ||
{ | ||
/** @var resource */ | ||
private $stream; | ||
private string $delimiter; | ||
|
||
private function __construct() | ||
{ | ||
} | ||
|
||
public static function fromRawParams(array $params): self | ||
{ | ||
$instance = new self(); | ||
$instance->delimiter = $params['delimiter'] ?? ''; | ||
$instance->stream = $params['stream'] ?? ''; | ||
|
||
return $instance; | ||
} | ||
|
||
/** | ||
* @return resource | ||
*/ | ||
public function stream() | ||
{ | ||
return $this->stream; | ||
} | ||
|
||
public function delimiter(): string | ||
{ | ||
return $this->delimiter; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Shlinkio\Shlink\Importer\Sources\Csv; | ||
|
||
use Shlinkio\Shlink\Importer\Params\ConsoleHelper\ParamsConsoleHelperInterface; | ||
use Symfony\Component\Console\Style\StyleInterface; | ||
|
||
use function fopen; | ||
|
||
class CsvParamsConsoleHelper implements ParamsConsoleHelperInterface | ||
{ | ||
public function requestParams(StyleInterface $io): array | ||
{ | ||
return [ | ||
'import_short_codes' => true, | ||
'stream' => $io->ask('What\'s the path for the CSV file you want to import', null, [$this, 'pathToStream']), | ||
'delimiter' => $io->choice('What\'s the delimiter used to separate values?', [ | ||
',' => 'Comma', | ||
';' => 'Semicolon', | ||
], ','), | ||
]; | ||
} | ||
|
||
/** | ||
* @return resource | ||
*/ | ||
public function pathToStream(?string $value) | ||
{ | ||
if (empty($value)) { | ||
throw InvalidPathException::pathNotProvided(); | ||
} | ||
|
||
$file = @fopen($value, 'rb'); | ||
if (! $file) { | ||
throw InvalidPathException::pathIsNotFile($value); | ||
} | ||
|
||
return $file; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Shlinkio\Shlink\Importer\Sources\Csv; | ||
|
||
use RuntimeException; | ||
use Shlinkio\Shlink\Importer\Exception\ExceptionInterface; | ||
|
||
use function sprintf; | ||
|
||
class InvalidPathException extends RuntimeException implements ExceptionInterface | ||
{ | ||
public static function pathNotProvided(): self | ||
{ | ||
return new self('The path of the file is required.'); | ||
} | ||
|
||
public static function pathIsNotFile(string $path): self | ||
{ | ||
return new self(sprintf('The file "%s" does not seem to exist. Try another one.', $path)); | ||
} | ||
} |
Oops, something went wrong.