Converted visit types into enum

This commit is contained in:
Alejandro Celaya
2022-04-23 18:19:16 +02:00
parent bca3e62ced
commit 404455928e
10 changed files with 70 additions and 51 deletions

View File

@@ -8,8 +8,8 @@ use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Types\Types; use Doctrine\DBAL\Types\Types;
use Doctrine\Migrations\AbstractMigration; use Doctrine\Migrations\AbstractMigration;
use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\Model\Visitor; use Shlinkio\Shlink\Core\Model\Visitor;
use Shlinkio\Shlink\Core\Visit\Model\VisitType;
final class Version20210207100807 extends AbstractMigration final class Version20210207100807 extends AbstractMigration
{ {
@@ -27,7 +27,7 @@ final class Version20210207100807 extends AbstractMigration
]); ]);
$visits->addColumn('type', Types::STRING, [ $visits->addColumn('type', Types::STRING, [
'length' => 255, 'length' => 255,
'default' => Visit::TYPE_VALID_SHORT_URL, 'default' => VisitType::VALID_SHORT_URL->value,
]); ]);
} }

View File

@@ -6,9 +6,11 @@ namespace Shlinkio\Shlink\Core;
use Doctrine\DBAL\Types\Types; use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder;
use Doctrine\ORM\Mapping\Builder\FieldBuilder;
use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\Mapping\ClassMetadata;
use Shlinkio\Shlink\Common\Doctrine\Type\ChronosDateTimeType; use Shlinkio\Shlink\Common\Doctrine\Type\ChronosDateTimeType;
use Shlinkio\Shlink\Core\Model\Visitor; use Shlinkio\Shlink\Core\Model\Visitor;
use Shlinkio\Shlink\Core\Visit\Model\VisitType;
return static function (ClassMetadata $metadata, array $emConfig): void { return static function (ClassMetadata $metadata, array $emConfig): void {
$builder = new ClassMetadataBuilder($metadata); $builder = new ClassMetadataBuilder($metadata);
@@ -61,10 +63,13 @@ return static function (ClassMetadata $metadata, array $emConfig): void {
->nullable() ->nullable()
->build(); ->build();
$builder->createField('type', Types::STRING) (new FieldBuilder($builder, [
->columnName('type') 'fieldName' => 'type',
->length(255) 'type' => Types::STRING,
->build(); 'enumType' => VisitType::class,
]))->columnName('type')
->length(255)
->build();
$builder->createField('potentialBot', Types::BOOLEAN) $builder->createField('potentialBot', Types::BOOLEAN)
->columnName('potential_bot') ->columnName('potential_bot')

View File

@@ -16,6 +16,7 @@ use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
use Shlinkio\Shlink\Core\ShortUrl\Resolver\ShortUrlRelationResolverInterface; use Shlinkio\Shlink\Core\ShortUrl\Resolver\ShortUrlRelationResolverInterface;
use Shlinkio\Shlink\Core\ShortUrl\Resolver\SimpleShortUrlRelationResolver; use Shlinkio\Shlink\Core\ShortUrl\Resolver\SimpleShortUrlRelationResolver;
use Shlinkio\Shlink\Core\Validation\ShortUrlInputFilter; use Shlinkio\Shlink\Core\Validation\ShortUrlInputFilter;
use Shlinkio\Shlink\Core\Visit\Model\VisitType;
use Shlinkio\Shlink\Importer\Model\ImportedShlinkUrl; use Shlinkio\Shlink\Importer\Model\ImportedShlinkUrl;
use Shlinkio\Shlink\Rest\Entity\ApiKey; use Shlinkio\Shlink\Rest\Entity\ApiKey;
@@ -174,7 +175,7 @@ class ShortUrl extends AbstractEntity
{ {
/** @var Selectable $visits */ /** @var Selectable $visits */
$visits = $this->visits; $visits = $this->visits;
$criteria = Criteria::create()->where(Criteria::expr()->eq('type', Visit::TYPE_IMPORTED)) $criteria = Criteria::create()->where(Criteria::expr()->eq('type', VisitType::IMPORTED))
->orderBy(['id' => 'DESC']) ->orderBy(['id' => 'DESC'])
->setMaxResults(1); ->setMaxResults(1);

View File

@@ -11,30 +11,24 @@ 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\VisitLocationInterface; use Shlinkio\Shlink\Core\Visit\Model\VisitLocationInterface;
use Shlinkio\Shlink\Core\Visit\Model\VisitType;
use Shlinkio\Shlink\Importer\Model\ImportedShlinkVisit; use Shlinkio\Shlink\Importer\Model\ImportedShlinkVisit;
use function Shlinkio\Shlink\Core\isCrawler; use function Shlinkio\Shlink\Core\isCrawler;
class Visit extends AbstractEntity implements JsonSerializable class Visit extends AbstractEntity implements JsonSerializable
{ {
// TODO Convert to enum
public const TYPE_VALID_SHORT_URL = 'valid_short_url';
public const TYPE_IMPORTED = 'imported';
public const TYPE_INVALID_SHORT_URL = 'invalid_short_url';
public const TYPE_BASE_URL = 'base_url';
public const TYPE_REGULAR_404 = 'regular_404';
private string $referer; private string $referer;
private Chronos $date; private Chronos $date;
private ?string $remoteAddr = null; private ?string $remoteAddr = null;
private ?string $visitedUrl = null; private ?string $visitedUrl = null;
private string $userAgent; private string $userAgent;
private string $type; private VisitType $type;
private ?ShortUrl $shortUrl; private ?ShortUrl $shortUrl;
private ?VisitLocation $visitLocation = null; private ?VisitLocation $visitLocation = null;
private bool $potentialBot; private bool $potentialBot;
private function __construct(?ShortUrl $shortUrl, string $type) private function __construct(?ShortUrl $shortUrl, VisitType $type)
{ {
$this->shortUrl = $shortUrl; $this->shortUrl = $shortUrl;
$this->date = Chronos::now(); $this->date = Chronos::now();
@@ -43,7 +37,7 @@ class Visit extends AbstractEntity implements JsonSerializable
public static function forValidShortUrl(ShortUrl $shortUrl, Visitor $visitor, bool $anonymize = true): self public static function forValidShortUrl(ShortUrl $shortUrl, Visitor $visitor, bool $anonymize = true): self
{ {
$instance = new self($shortUrl, self::TYPE_VALID_SHORT_URL); $instance = new self($shortUrl, VisitType::VALID_SHORT_URL);
$instance->hydrateFromVisitor($visitor, $anonymize); $instance->hydrateFromVisitor($visitor, $anonymize);
return $instance; return $instance;
@@ -51,7 +45,7 @@ class Visit extends AbstractEntity implements JsonSerializable
public static function fromImport(ShortUrl $shortUrl, ImportedShlinkVisit $importedVisit): self public static function fromImport(ShortUrl $shortUrl, ImportedShlinkVisit $importedVisit): self
{ {
$instance = new self($shortUrl, self::TYPE_IMPORTED); $instance = new self($shortUrl, VisitType::IMPORTED);
$instance->userAgent = $importedVisit->userAgent(); $instance->userAgent = $importedVisit->userAgent();
$instance->potentialBot = isCrawler($instance->userAgent); $instance->potentialBot = isCrawler($instance->userAgent);
$instance->referer = $importedVisit->referer(); $instance->referer = $importedVisit->referer();
@@ -65,7 +59,7 @@ class Visit extends AbstractEntity implements JsonSerializable
public static function forBasePath(Visitor $visitor, bool $anonymize = true): self public static function forBasePath(Visitor $visitor, bool $anonymize = true): self
{ {
$instance = new self(null, self::TYPE_BASE_URL); $instance = new self(null, VisitType::BASE_URL);
$instance->hydrateFromVisitor($visitor, $anonymize); $instance->hydrateFromVisitor($visitor, $anonymize);
return $instance; return $instance;
@@ -73,7 +67,7 @@ class Visit extends AbstractEntity implements JsonSerializable
public static function forInvalidShortUrl(Visitor $visitor, bool $anonymize = true): self public static function forInvalidShortUrl(Visitor $visitor, bool $anonymize = true): self
{ {
$instance = new self(null, self::TYPE_INVALID_SHORT_URL); $instance = new self(null, VisitType::INVALID_SHORT_URL);
$instance->hydrateFromVisitor($visitor, $anonymize); $instance->hydrateFromVisitor($visitor, $anonymize);
return $instance; return $instance;
@@ -81,7 +75,7 @@ class Visit extends AbstractEntity implements JsonSerializable
public static function forRegularNotFound(Visitor $visitor, bool $anonymize = true): self public static function forRegularNotFound(Visitor $visitor, bool $anonymize = true): self
{ {
$instance = new self(null, self::TYPE_REGULAR_404); $instance = new self(null, VisitType::REGULAR_404);
$instance->hydrateFromVisitor($visitor, $anonymize); $instance->hydrateFromVisitor($visitor, $anonymize);
return $instance; return $instance;
@@ -151,7 +145,7 @@ class Visit extends AbstractEntity implements JsonSerializable
return $this->visitedUrl; return $this->visitedUrl;
} }
public function type(): string public function type(): VisitType
{ {
return $this->type; return $this->type;
} }
@@ -160,11 +154,19 @@ class Visit extends AbstractEntity implements JsonSerializable
* Needed only for ArrayCollections to be able to apply criteria filtering * Needed only for ArrayCollections to be able to apply criteria filtering
* @internal * @internal
*/ */
public function getType(): string public function getType(): VisitType
{ {
return $this->type(); return $this->type();
} }
/**
* @internal
*/
public function getDate(): Chronos
{
return $this->date;
}
public function jsonSerialize(): array public function jsonSerialize(): array
{ {
return [ return [
@@ -175,12 +177,4 @@ class Visit extends AbstractEntity implements JsonSerializable
'potentialBot' => $this->potentialBot, 'potentialBot' => $this->potentialBot,
]; ];
} }
/**
* @internal
*/
public function getDate(): Chronos
{
return $this->date;
}
} }

View File

@@ -7,13 +7,13 @@ namespace Shlinkio\Shlink\Core\ErrorHandler\Model;
use Mezzio\Router\RouteResult; use Mezzio\Router\RouteResult;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Shlinkio\Shlink\Core\Action\RedirectAction; use Shlinkio\Shlink\Core\Action\RedirectAction;
use Shlinkio\Shlink\Core\Entity\Visit; use Shlinkio\Shlink\Core\Visit\Model\VisitType;
use function rtrim; use function rtrim;
class NotFoundType class NotFoundType
{ {
private function __construct(private string $type) private function __construct(private readonly VisitType $type)
{ {
} }
@@ -24,10 +24,10 @@ class NotFoundType
$isBaseUrl = rtrim($request->getUri()->getPath(), '/') === $basePath; $isBaseUrl = rtrim($request->getUri()->getPath(), '/') === $basePath;
$type = match (true) { $type = match (true) {
$isBaseUrl => Visit::TYPE_BASE_URL, $isBaseUrl => VisitType::BASE_URL,
$routeResult->isFailure() => Visit::TYPE_REGULAR_404, $routeResult->isFailure() => VisitType::REGULAR_404,
$routeResult->getMatchedRouteName() === RedirectAction::class => Visit::TYPE_INVALID_SHORT_URL, $routeResult->getMatchedRouteName() === RedirectAction::class => VisitType::INVALID_SHORT_URL,
default => self::class, default => VisitType::VALID_SHORT_URL,
}; };
return new self($type); return new self($type);
@@ -35,16 +35,16 @@ class NotFoundType
public function isBaseUrl(): bool public function isBaseUrl(): bool
{ {
return $this->type === Visit::TYPE_BASE_URL; return $this->type === VisitType::BASE_URL;
} }
public function isRegularNotFound(): bool public function isRegularNotFound(): bool
{ {
return $this->type === Visit::TYPE_REGULAR_404; return $this->type === VisitType::REGULAR_404;
} }
public function isInvalidShortUrl(): bool public function isInvalidShortUrl(): bool
{ {
return $this->type === Visit::TYPE_INVALID_SHORT_URL; return $this->type === VisitType::INVALID_SHORT_URL;
} }
} }

View File

@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
// phpcs:disable
// TODO Enable coding style checks again once code sniffer 3.7 is released https://github.com/squizlabs/PHP_CodeSniffer/issues/3474
namespace Shlinkio\Shlink\Core\Visit\Model;
enum VisitType: string
{
case VALID_SHORT_URL = 'valid_short_url';
case IMPORTED = 'imported';
case INVALID_SHORT_URL = 'invalid_short_url';
case BASE_URL = 'base_url';
case REGULAR_404 = 'regular_404';
}

View File

@@ -17,7 +17,7 @@ class OrphanVisitDataTransformer implements DataTransformerInterface
{ {
$serializedVisit = $visit->jsonSerialize(); $serializedVisit = $visit->jsonSerialize();
$serializedVisit['visitedUrl'] = $visit->visitedUrl(); $serializedVisit['visitedUrl'] = $visit->visitedUrl();
$serializedVisit['type'] = $visit->type(); $serializedVisit['type'] = $visit->type()->value;
return $serializedVisit; return $serializedVisit;
} }

View File

@@ -17,6 +17,7 @@ use Shlinkio\Shlink\Core\EventDispatcher\Event\VisitLocated;
use Shlinkio\Shlink\Core\EventDispatcher\NotifyVisitToMercure; use Shlinkio\Shlink\Core\EventDispatcher\NotifyVisitToMercure;
use Shlinkio\Shlink\Core\Mercure\MercureUpdatesGeneratorInterface; use Shlinkio\Shlink\Core\Mercure\MercureUpdatesGeneratorInterface;
use Shlinkio\Shlink\Core\Model\Visitor; use Shlinkio\Shlink\Core\Model\Visitor;
use Shlinkio\Shlink\Core\Visit\Model\VisitType;
use Symfony\Component\Mercure\HubInterface; use Symfony\Component\Mercure\HubInterface;
use Symfony\Component\Mercure\Update; use Symfony\Component\Mercure\Update;
@@ -160,8 +161,8 @@ class NotifyVisitToMercureTest extends TestCase
{ {
$visitor = Visitor::emptyInstance(); $visitor = Visitor::emptyInstance();
yield Visit::TYPE_REGULAR_404 => [Visit::forRegularNotFound($visitor)]; yield VisitType::REGULAR_404->value => [Visit::forRegularNotFound($visitor)];
yield Visit::TYPE_INVALID_SHORT_URL => [Visit::forInvalidShortUrl($visitor)]; yield VisitType::INVALID_SHORT_URL->value => [Visit::forInvalidShortUrl($visitor)];
yield Visit::TYPE_BASE_URL => [Visit::forBasePath($visitor)]; yield VisitType::BASE_URL->value => [Visit::forBasePath($visitor)];
} }
} }

View File

@@ -12,6 +12,7 @@ use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
use Shlinkio\Shlink\Core\Model\Visitor; use Shlinkio\Shlink\Core\Model\Visitor;
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier; use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier;
use Shlinkio\Shlink\Core\ShortUrl\Transformer\ShortUrlDataTransformer; use Shlinkio\Shlink\Core\ShortUrl\Transformer\ShortUrlDataTransformer;
use Shlinkio\Shlink\Core\Visit\Model\VisitType;
use Shlinkio\Shlink\Core\Visit\Transformer\OrphanVisitDataTransformer; use Shlinkio\Shlink\Core\Visit\Transformer\OrphanVisitDataTransformer;
use function Shlinkio\Shlink\Common\json_decode; use function Shlinkio\Shlink\Common\json_decode;
@@ -95,7 +96,7 @@ class MercureUpdatesGeneratorTest extends TestCase
'date' => $orphanVisit->getDate()->toAtomString(), 'date' => $orphanVisit->getDate()->toAtomString(),
'potentialBot' => false, 'potentialBot' => false,
'visitedUrl' => $orphanVisit->visitedUrl(), 'visitedUrl' => $orphanVisit->visitedUrl(),
'type' => $orphanVisit->type(), 'type' => $orphanVisit->type()->value,
], ],
], json_decode($update->getData())); ], json_decode($update->getData()));
} }
@@ -104,8 +105,8 @@ class MercureUpdatesGeneratorTest extends TestCase
{ {
$visitor = Visitor::emptyInstance(); $visitor = Visitor::emptyInstance();
yield Visit::TYPE_REGULAR_404 => [Visit::forRegularNotFound($visitor)]; yield VisitType::REGULAR_404->value => [Visit::forRegularNotFound($visitor)];
yield Visit::TYPE_INVALID_SHORT_URL => [Visit::forInvalidShortUrl($visitor)]; yield VisitType::INVALID_SHORT_URL->value => [Visit::forInvalidShortUrl($visitor)];
yield Visit::TYPE_BASE_URL => [Visit::forBasePath($visitor)]; yield VisitType::BASE_URL->value => [Visit::forBasePath($visitor)];
} }
} }

View File

@@ -10,6 +10,7 @@ use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Core\Entity\Visit; use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\Entity\VisitLocation; use Shlinkio\Shlink\Core\Entity\VisitLocation;
use Shlinkio\Shlink\Core\Model\Visitor; use Shlinkio\Shlink\Core\Model\Visitor;
use Shlinkio\Shlink\Core\Visit\Model\VisitType;
use Shlinkio\Shlink\Core\Visit\Transformer\OrphanVisitDataTransformer; use Shlinkio\Shlink\Core\Visit\Transformer\OrphanVisitDataTransformer;
use Shlinkio\Shlink\IpGeolocation\Model\Location; use Shlinkio\Shlink\IpGeolocation\Model\Location;
@@ -44,7 +45,7 @@ class OrphanVisitDataTransformerTest extends TestCase
'visitLocation' => null, 'visitLocation' => null,
'potentialBot' => false, 'potentialBot' => false,
'visitedUrl' => '', 'visitedUrl' => '',
'type' => Visit::TYPE_BASE_URL, 'type' => VisitType::BASE_URL->value,
], ],
]; ];
yield 'invalid short url visit' => [ yield 'invalid short url visit' => [
@@ -60,7 +61,7 @@ class OrphanVisitDataTransformerTest extends TestCase
'visitLocation' => null, 'visitLocation' => null,
'potentialBot' => false, 'potentialBot' => false,
'visitedUrl' => 'https://example.com/foo', 'visitedUrl' => 'https://example.com/foo',
'type' => Visit::TYPE_INVALID_SHORT_URL, 'type' => VisitType::INVALID_SHORT_URL->value,
], ],
]; ];
yield 'regular 404 visit' => [ yield 'regular 404 visit' => [
@@ -78,7 +79,7 @@ class OrphanVisitDataTransformerTest extends TestCase
'visitLocation' => $location, 'visitLocation' => $location,
'potentialBot' => false, 'potentialBot' => false,
'visitedUrl' => 'https://doma.in/foo/bar', 'visitedUrl' => 'https://doma.in/foo/bar',
'type' => Visit::TYPE_REGULAR_404, 'type' => VisitType::REGULAR_404->value,
], ],
]; ];
} }