Skip to content

Commit

Permalink
Merge pull request shlinkio#54 from acelaya-forks/feature/drop-php8
Browse files Browse the repository at this point in the history
Feature/drop php8
  • Loading branch information
acelaya authored Aug 6, 2022
2 parents 7a9b3a7 + 2227d1e commit 4258020
Show file tree
Hide file tree
Showing 39 changed files with 339 additions and 557 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to [Semantic Versioning](https://semver.org).

## [4.0.0] - 2022-08-06
### Added
* *Nothing*

### Changed
* Used PHP 8.1 syntax wherever possible (readonly, enums, etc).

### Deprecated
* *Nothing*

### Removed
* Dropped support for PHP 8.0

### Fixed
* *Nothing*


## [3.0.0] - 2022-04-23
### Added
* [#47](https://github.com/shlinkio/shlink-importer/issues/47) Added support to import from YOURLS under a specific domain.
Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@
}
],
"require": {
"php": "^8.0",
"php": "^8.1",
"ext-json": "*",
"laminas/laminas-servicemanager": "^3.11.2",
"laminas/laminas-servicemanager": "^3.16",
"league/csv": "^9.8",
"lstrojny/functional-php": "^1.17",
"shlinkio/shlink-config": "^1.6",
"symfony/console": "^6.0"
"shlinkio/shlink-config": "^2.0",
"symfony/console": "^6.1"
},
"require-dev": {
"guzzlehttp/guzzle": "^7.4",
"infection/infection": "^0.26",
"phpspec/prophecy-phpunit": "^2.0",
"phpstan/phpstan": "^1.5",
"phpstan/phpstan": "^1.8",
"phpunit/phpunit": "^9.5",
"psr/http-factory": "^1.0",
"roave/security-advisories": "dev-master",
"shlinkio/php-coding-standard": "~2.3.0",
"symfony/var-dumper": "^6.0"
"symfony/var-dumper": "^6.1"
},
"suggest": {
"psr/http-client": "If you want to be able to import URLs from Bit.ly, YOURLS, Kutt.it or another Shlink instance",
Expand Down
20 changes: 10 additions & 10 deletions config/dependencies.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
],

'aliases' => [
Sources\ImportSources::BITLY => Sources\Bitly\BitlyApiImporter::class,
Sources\ImportSources::CSV => Sources\Csv\CsvImporter::class,
Sources\ImportSources::SHLINK => Sources\Shlink\ShlinkImporter::class,
Sources\ImportSources::YOURLS => Sources\Yourls\YourlsImporter::class,
Sources\ImportSources::KUTT => Sources\Kutt\KuttImporter::class,
Sources\ImportSource::BITLY->value => Sources\Bitly\BitlyApiImporter::class,
Sources\ImportSource::CSV->value => Sources\Csv\CsvImporter::class,
Sources\ImportSource::SHLINK->value => Sources\Shlink\ShlinkImporter::class,
Sources\ImportSource::YOURLS->value => Sources\Yourls\YourlsImporter::class,
Sources\ImportSource::KUTT->value => Sources\Kutt\KuttImporter::class,
],
],

Expand All @@ -60,11 +60,11 @@
],

'aliases' => [
Sources\ImportSources::BITLY => Sources\Bitly\BitlyApiParamsConsoleHelper::class,
Sources\ImportSources::CSV => Sources\Csv\CsvParamsConsoleHelper::class,
Sources\ImportSources::SHLINK => Sources\Shlink\ShlinkParamsConsoleHelper::class,
Sources\ImportSources::YOURLS => Sources\Yourls\YourlsParamsConsoleHelper::class,
Sources\ImportSources::KUTT => Sources\Kutt\KuttParamsConsoleHelper::class,
Sources\ImportSource::BITLY->value => Sources\Bitly\BitlyApiParamsConsoleHelper::class,
Sources\ImportSource::CSV->value => Sources\Csv\CsvParamsConsoleHelper::class,
Sources\ImportSource::SHLINK->value => Sources\Shlink\ShlinkParamsConsoleHelper::class,
Sources\ImportSource::YOURLS->value => Sources\Yourls\YourlsParamsConsoleHelper::class,
Sources\ImportSource::KUTT->value => Sources\Kutt\KuttParamsConsoleHelper::class,
],
],
],
Expand Down
38 changes: 18 additions & 20 deletions src/Command/ImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
use Shlinkio\Shlink\Importer\ImportedLinksProcessorInterface;
use Shlinkio\Shlink\Importer\Params\ConsoleHelper\ConsoleHelperManagerInterface;
use Shlinkio\Shlink\Importer\Params\ConsoleHelper\ParamsConsoleHelperInterface;
use Shlinkio\Shlink\Importer\Params\ImportParams;
use Shlinkio\Shlink\Importer\Sources\ImportSources;
use Shlinkio\Shlink\Importer\Sources\ImportSource;
use Shlinkio\Shlink\Importer\Strategy\ImporterStrategyInterface;
use Shlinkio\Shlink\Importer\Strategy\ImporterStrategyManagerInterface;
use Symfony\Component\Console\Command\Command;
Expand All @@ -19,7 +18,6 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

use function Functional\contains;
use function implode;
use function sprintf;

Expand All @@ -30,11 +28,11 @@ class ImportCommand extends Command
private array $validSources;

public function __construct(
private ImporterStrategyManagerInterface $importerStrategyManager,
private ConsoleHelperManagerInterface $consoleHelperManager,
private ImportedLinksProcessorInterface $importedLinksProcessor,
private readonly ImporterStrategyManagerInterface $importerStrategyManager,
private readonly ConsoleHelperManagerInterface $consoleHelperManager,
private readonly ImportedLinksProcessorInterface $importedLinksProcessor,
) {
$this->validSources = ImportSources::getAll();
$this->validSources = ImportSource::values();
parent::__construct();
}

Expand All @@ -49,14 +47,6 @@ protected function configure(): void
));
}

protected function initialize(InputInterface $input, OutputInterface $output): void
{
$source = $input->getArgument('source');
if ($source !== null && ! contains($this->validSources, $source)) {
throw InvalidSourceException::fromInvalidSource($source);
}
}

protected function interact(InputInterface $input, OutputInterface $output): void
{
$source = $input->getArgument('source');
Expand All @@ -70,18 +60,26 @@ protected function interact(InputInterface $input, OutputInterface $output): voi
}
}

protected function initialize(InputInterface $input, OutputInterface $output): void
{
$source = $input->getArgument('source');
if ($source !== null && ImportSource::tryFrom($source) === null) {
throw InvalidSourceException::fromInvalidSource($source);
}
}

protected function execute(InputInterface $input, OutputInterface $output): ?int
{
$io = new SymfonyStyle($input, $output);
$source = $input->getArgument('source');
$source = ImportSource::from($input->getArgument('source'));

/** @var ParamsConsoleHelperInterface $paramsHelper */
$paramsHelper = $this->consoleHelperManager->get($source);
$paramsHelper = $this->consoleHelperManager->get($source->value);
/** @var ImporterStrategyInterface $importerStrategy */
$importerStrategy = $this->importerStrategyManager->get($source);
$importerStrategy = $this->importerStrategyManager->get($source->value);

try {
$params = ImportParams::fromSourceAndCallableMap($source, $paramsHelper->requestParams($io));
$params = $source->toParamsWithCallableMap($paramsHelper->requestParams($io));
$links = $importerStrategy->import($params);
$this->importedLinksProcessor->process($io, $links, $params);
} catch (ImportException $e) {
Expand All @@ -94,7 +92,7 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int

private function handleImportError(ImportException $e, SymfonyStyle $io): void
{
$continueToken = $e->continueToken();
$continueToken = $e->continueToken;

if ($continueToken === null) {
$io->error('An error occurred while importing URLs.');
Expand Down
7 changes: 1 addition & 6 deletions src/Exception/ImportException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ImportException extends RuntimeException implements ExceptionInterface
{
protected function __construct(
string $message,
private ?string $continueToken,
public readonly ?string $continueToken,
int $code = 0,
?Throwable $previous = null,
) {
Expand All @@ -22,9 +22,4 @@ public static function fromError(Throwable $e): self
{
return new self('An error occurred while importing URLs', null, -1, $e);
}

public function continueToken(): ?string
{
return $this->continueToken;
}
}
4 changes: 2 additions & 2 deletions src/Exception/InvalidSourceException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Shlinkio\Shlink\Importer\Exception;

use InvalidArgumentException;
use Shlinkio\Shlink\Importer\Sources\ImportSources;
use Shlinkio\Shlink\Importer\Sources\ImportSource;

use function implode;
use function sprintf;
Expand All @@ -17,7 +17,7 @@ public static function fromInvalidSource(string $source): self
return new self(sprintf(
'Provided source "%s" is not valid. Expected one of ["%s"]',
$source,
implode('", "', ImportSources::getAll()),
implode('", "', ImportSource::values()),
));
}
}
23 changes: 9 additions & 14 deletions src/Http/InvalidRequestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
use Shlinkio\Shlink\Importer\Exception\ExceptionInterface;

use function sprintf;
use function str_contains;

class InvalidRequestException extends RuntimeException implements ExceptionInterface
{
private function __construct(string $message, private string $url, private int $statusCode, private string $body)
{
private function __construct(
string $message,
public readonly string $url,
public readonly int $statusCode,
public readonly string $body,
) {
parent::__construct($message);
}

Expand All @@ -26,18 +31,8 @@ public static function fromResponseData(string $url, int $statusCode, string $bo
);
}

public function url(): string
{
return $this->url;
}

public function statusCode(): int
{
return $this->statusCode;
}

public function body(): string
public function isShlinkPluginMissingError(): bool
{
return $this->body;
return str_contains($this->body, '"message":"Unknown or missing \"action\" parameter"');
}
}
6 changes: 4 additions & 2 deletions src/Http/RestApiConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@

class RestApiConsumer implements RestApiConsumerInterface
{
public function __construct(private ClientInterface $httpClient, private RequestFactoryInterface $requestFactory)
{
public function __construct(
private readonly ClientInterface $httpClient,
private readonly RequestFactoryInterface $requestFactory,
) {
}

/**
Expand Down
75 changes: 11 additions & 64 deletions src/Model/ImportedShlinkUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,77 +5,24 @@
namespace Shlinkio\Shlink\Importer\Model;

use DateTimeInterface;
use Shlinkio\Shlink\Importer\Sources\ImportSource;

class ImportedShlinkUrl
{
/**
* @param iterable<ImportedShlinkVisit> $visits
*/
public function __construct(
private string $source,
private string $longUrl,
private array $tags,
private DateTimeInterface $createdAt,
private ?string $domain,
private string $shortCode,
private ?string $title,
private iterable $visits = [],
private ?int $visitsCount = null,
private ?ImportedShlinkUrlMeta $meta = null,
public readonly ImportSource $source,
public readonly string $longUrl,
public readonly array $tags,
public readonly DateTimeInterface $createdAt,
public readonly ?string $domain,
public readonly string $shortCode,
public readonly ?string $title,
public readonly iterable $visits = [],
public readonly ?int $visitsCount = null,
public ImportedShlinkUrlMeta $meta = new ImportedShlinkUrlMeta(),
) {
}

public function source(): string
{
return $this->source;
}

public function longUrl(): string
{
return $this->longUrl;
}

public function tags(): array
{
return $this->tags;
}

public function createdAt(): DateTimeInterface
{
return $this->createdAt;
}

public function domain(): ?string
{
return $this->domain;
}

public function shortCode(): string
{
return $this->shortCode;
}

public function title(): ?string
{
return $this->title;
}

/**
* @return iterable<ImportedShlinkVisit>
*/
public function visits(): iterable
{
return $this->visits;
}

public function visitsCount(): ?int
{
return $this->visitsCount;
}

public function meta(): ImportedShlinkUrlMeta
{
$this->meta = $this->meta ?? ImportedShlinkUrlMeta::emptyInstance();
return $this->meta;
}
}
26 changes: 3 additions & 23 deletions src/Model/ImportedShlinkUrlMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,9 @@
class ImportedShlinkUrlMeta
{
public function __construct(
private ?DateTimeInterface $validSince,
private ?DateTimeInterface $validUntil,
private ?int $maxVisits,
public readonly ?DateTimeInterface $validSince = null,
public readonly ?DateTimeInterface $validUntil = null,
public readonly ?int $maxVisits = null,
) {
}

public static function emptyInstance(): self
{
return new self(null, null, null);
}

public function validSince(): ?DateTimeInterface
{
return $this->validSince;
}

public function validUntil(): ?DateTimeInterface
{
return $this->validUntil;
}

public function maxVisits(): ?int
{
return $this->maxVisits;
}
}
Loading

0 comments on commit 4258020

Please sign in to comment.