Decoupled Common module from any other module

This commit is contained in:
Alejandro Celaya 2019-08-11 14:29:22 +02:00
parent 97a362617d
commit cb715c0877
5 changed files with 10 additions and 9 deletions

View File

@ -7,8 +7,8 @@ use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration; use Doctrine\Migrations\AbstractMigration;
use PDO; use PDO;
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
use Shlinkio\Shlink\Common\Util\IpAddress; use Shlinkio\Shlink\Common\Util\IpAddress;
use Shlinkio\Shlink\IpGeolocation\Exception\WrongIpException;
/** /**
* Auto-generated Migration: Please modify to your needs! * Auto-generated Migration: Please modify to your needs!
@ -60,7 +60,7 @@ final class Version20180913205455 extends AbstractMigration
try { try {
return (string) IpAddress::fromString($addr)->getObfuscatedCopy(); return (string) IpAddress::fromString($addr)->getObfuscatedCopy();
} catch (WrongIpException $e) { } catch (InvalidArgumentException $e) {
return null; return null;
} }
} }

View File

@ -3,11 +3,12 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Common\Util; namespace Shlinkio\Shlink\Common\Util;
use Shlinkio\Shlink\IpGeolocation\Exception\WrongIpException; use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
use function count; use function count;
use function explode; use function explode;
use function implode; use function implode;
use function sprintf;
use function trim; use function trim;
final class IpAddress final class IpAddress
@ -36,14 +37,14 @@ final class IpAddress
/** /**
* @param string $address * @param string $address
* @return IpAddress * @return IpAddress
* @throws WrongIpException * @throws InvalidArgumentException
*/ */
public static function fromString(string $address): self public static function fromString(string $address): self
{ {
$address = trim($address); $address = trim($address);
$parts = explode('.', $address); $parts = explode('.', $address);
if (count($parts) !== self::IPV4_PARTS_COUNT) { if (count($parts) !== self::IPV4_PARTS_COUNT) {
throw WrongIpException::fromIpAddress($address); throw new InvalidArgumentException(sprintf('Provided IP "%s" is invalid', $address));
} }
return new self(...$parts); return new self(...$parts);

View File

@ -9,7 +9,6 @@ use GuzzleHttp\ClientInterface;
use GuzzleHttp\RequestOptions; use GuzzleHttp\RequestOptions;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Shlinkio\Shlink\Rest\Authentication\Plugin\ApiKeyHeaderPlugin;
use function Shlinkio\Shlink\Common\json_decode; use function Shlinkio\Shlink\Common\json_decode;
use function sprintf; use function sprintf;
@ -48,7 +47,7 @@ abstract class ApiTestCase extends TestCase implements StatusCodeInterface, Requ
protected function callApiWithKey(string $method, string $uri, array $options = []): ResponseInterface protected function callApiWithKey(string $method, string $uri, array $options = []): ResponseInterface
{ {
$headers = $options[RequestOptions::HEADERS] ?? []; $headers = $options[RequestOptions::HEADERS] ?? [];
$headers[ApiKeyHeaderPlugin::HEADER_NAME] = 'valid_api_key'; $headers['X-Api-Key'] = 'valid_api_key';
$options[RequestOptions::HEADERS] = $headers; $options[RequestOptions::HEADERS] = $headers;
return $this->callApi($method, $uri, $options); return $this->callApi($method, $uri, $options);

View File

@ -6,11 +6,11 @@ namespace Shlinkio\Shlink\Core\Entity;
use Cake\Chronos\Chronos; use Cake\Chronos\Chronos;
use JsonSerializable; use JsonSerializable;
use Shlinkio\Shlink\Common\Entity\AbstractEntity; use Shlinkio\Shlink\Common\Entity\AbstractEntity;
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
use Shlinkio\Shlink\Common\Util\IpAddress; use Shlinkio\Shlink\Common\Util\IpAddress;
use Shlinkio\Shlink\Core\Model\Visitor; use Shlinkio\Shlink\Core\Model\Visitor;
use Shlinkio\Shlink\Core\Visit\Model\UnknownVisitLocation; use Shlinkio\Shlink\Core\Visit\Model\UnknownVisitLocation;
use Shlinkio\Shlink\Core\Visit\Model\VisitLocationInterface; use Shlinkio\Shlink\Core\Visit\Model\VisitLocationInterface;
use Shlinkio\Shlink\IpGeolocation\Exception\WrongIpException;
class Visit extends AbstractEntity implements JsonSerializable class Visit extends AbstractEntity implements JsonSerializable
{ {
@ -45,7 +45,7 @@ class Visit extends AbstractEntity implements JsonSerializable
try { try {
return (string) IpAddress::fromString($address)->getObfuscatedCopy(); return (string) IpAddress::fromString($address)->getObfuscatedCopy();
} catch (WrongIpException $e) { } catch (InvalidArgumentException $e) {
return null; return null;
} }
} }

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace ShlinkioApiTest\Shlink\Rest\Action; namespace ShlinkioApiTest\Shlink\Rest\Action;
use ShlinkioTest\Shlink\Common\ApiTest\ApiTestCase; use ShlinkioTest\Shlink\Common\ApiTest\ApiTestCase;
use function explode; use function explode;
class OptionsRequestTest extends ApiTestCase class OptionsRequestTest extends ApiTestCase