Skip to content

Commit

Permalink
Merge pull request #3 from Chemaclass/refactor/add-custom-exception
Browse files Browse the repository at this point in the history
Add custom exception
  • Loading branch information
pscheit authored May 30, 2024
2 parents 61cc790 + 799be44 commit 1e2ef86
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
9 changes: 5 additions & 4 deletions src/Command/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
namespace PrivatePackagist\VendorDataExporter\Command;

use PrivatePackagist\ApiClient\Client as PackagistApiClient;
use PrivatePackagist\VendorDataExporter\Exceptions\MissingApiCredentialsException;
use PrivatePackagist\VendorDataExporter\Formatter\Manager;
use PrivatePackagist\VendorDataExporter\Formatter\ManagerInterface;
use PrivatePackagist\VendorDataExporter\Model;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @phpstan-import-type CustomerShape from Model\Customer
* @phpstan-import-type PackageShape from Model\Package
* @phpstan-import-type VersionShape from Model\Version
*/
class ListCommand extends Command
final class ListCommand extends Command
{
public const DEFAULT_COMMAND_NAME = 'list';

Expand Down Expand Up @@ -73,10 +74,10 @@ private function getPackagistApiClient(InputInterface $input): PackagistApiClien
}

if (!is_string($token = $input->getOption('token'))) {
$token = $_ENV['PACKAGIST_API_TOKEN'] ?? throw new \InvalidArgumentException('Missing API credentials: provide API token via command flag or environment variable.');
$token = $_ENV['PACKAGIST_API_TOKEN'] ?? throw MissingApiCredentialsException::missingApiToken();
}
if (!is_string($secret = $input->getOption('secret'))) {
$secret = $_ENV['PACKAGIST_API_SECRET'] ?? throw new \InvalidArgumentException('Missing API credentials: provide API secret via command flag or environment variable.');
$secret = $_ENV['PACKAGIST_API_SECRET'] ?? throw MissingApiCredentialsException::missingApiSecret();
}

$this->packagistApiClient = new PackagistApiClient(null, $_ENV['PACKAGIST_API_URL'] ?? null);
Expand Down
20 changes: 20 additions & 0 deletions src/Exceptions/MissingApiCredentialsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace PrivatePackagist\VendorDataExporter\Exceptions;

use InvalidArgumentException;

final class MissingApiCredentialsException extends InvalidArgumentException
{
public static function missingApiToken(): self
{
return new self('Missing API credentials: provide API token via command flag or environment variable.');
}

public static function missingApiSecret(): self
{
return new self('Missing API credentials: provide API secret via command flag or environment variable.');
}
}
6 changes: 3 additions & 3 deletions tests/Command/ListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testTextOutput(): void
$this->command->run($input, $output);

static::assertCount(49, $this->httpClient->getRequests());
static::assertSame(file_get_contents(__DIR__ . '/../res/output/output.txt'), $output->fetch());
static::assertStringEqualsFile(__DIR__ . '/../res/output/output.txt', $output->fetch());
}

public function testJsonOutput(): void
Expand All @@ -51,7 +51,7 @@ public function testJsonOutput(): void
$this->command->run($input, $output);

static::assertCount(49, $this->httpClient->getRequests());
static::assertSame(file_get_contents(__DIR__ . '/../res/output/output.json'), $output->fetch());
static::assertStringEqualsFile(__DIR__ . '/../res/output/output.json', $output->fetch());
}

public function testCsvOutput(): void
Expand All @@ -62,6 +62,6 @@ public function testCsvOutput(): void
$this->command->run($input, $output);

static::assertCount(49, $this->httpClient->getRequests());
static::assertSame(file_get_contents(__DIR__ . '/../res/output/output.csv'), $output->fetch());
static::assertStringEqualsFile(__DIR__ . '/../res/output/output.csv', $output->fetch());
}
}

0 comments on commit 1e2ef86

Please sign in to comment.