mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-10 17:23:12 +08:00
Converted visit types into enum
This commit is contained in:
@@ -16,6 +16,7 @@ use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Resolver\ShortUrlRelationResolverInterface;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Resolver\SimpleShortUrlRelationResolver;
|
||||
use Shlinkio\Shlink\Core\Validation\ShortUrlInputFilter;
|
||||
use Shlinkio\Shlink\Core\Visit\Model\VisitType;
|
||||
use Shlinkio\Shlink\Importer\Model\ImportedShlinkUrl;
|
||||
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
||||
|
||||
@@ -174,7 +175,7 @@ class ShortUrl extends AbstractEntity
|
||||
{
|
||||
/** @var Selectable $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'])
|
||||
->setMaxResults(1);
|
||||
|
||||
|
||||
@@ -11,30 +11,24 @@ use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
|
||||
use Shlinkio\Shlink\Common\Util\IpAddress;
|
||||
use Shlinkio\Shlink\Core\Model\Visitor;
|
||||
use Shlinkio\Shlink\Core\Visit\Model\VisitLocationInterface;
|
||||
use Shlinkio\Shlink\Core\Visit\Model\VisitType;
|
||||
use Shlinkio\Shlink\Importer\Model\ImportedShlinkVisit;
|
||||
|
||||
use function Shlinkio\Shlink\Core\isCrawler;
|
||||
|
||||
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 Chronos $date;
|
||||
private ?string $remoteAddr = null;
|
||||
private ?string $visitedUrl = null;
|
||||
private string $userAgent;
|
||||
private string $type;
|
||||
private VisitType $type;
|
||||
private ?ShortUrl $shortUrl;
|
||||
private ?VisitLocation $visitLocation = null;
|
||||
private bool $potentialBot;
|
||||
|
||||
private function __construct(?ShortUrl $shortUrl, string $type)
|
||||
private function __construct(?ShortUrl $shortUrl, VisitType $type)
|
||||
{
|
||||
$this->shortUrl = $shortUrl;
|
||||
$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
|
||||
{
|
||||
$instance = new self($shortUrl, self::TYPE_VALID_SHORT_URL);
|
||||
$instance = new self($shortUrl, VisitType::VALID_SHORT_URL);
|
||||
$instance->hydrateFromVisitor($visitor, $anonymize);
|
||||
|
||||
return $instance;
|
||||
@@ -51,7 +45,7 @@ class Visit extends AbstractEntity implements JsonSerializable
|
||||
|
||||
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->potentialBot = isCrawler($instance->userAgent);
|
||||
$instance->referer = $importedVisit->referer();
|
||||
@@ -65,7 +59,7 @@ class Visit extends AbstractEntity implements JsonSerializable
|
||||
|
||||
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);
|
||||
|
||||
return $instance;
|
||||
@@ -73,7 +67,7 @@ class Visit extends AbstractEntity implements JsonSerializable
|
||||
|
||||
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);
|
||||
|
||||
return $instance;
|
||||
@@ -81,7 +75,7 @@ class Visit extends AbstractEntity implements JsonSerializable
|
||||
|
||||
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);
|
||||
|
||||
return $instance;
|
||||
@@ -151,7 +145,7 @@ class Visit extends AbstractEntity implements JsonSerializable
|
||||
return $this->visitedUrl;
|
||||
}
|
||||
|
||||
public function type(): string
|
||||
public function type(): VisitType
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
@@ -160,11 +154,19 @@ class Visit extends AbstractEntity implements JsonSerializable
|
||||
* Needed only for ArrayCollections to be able to apply criteria filtering
|
||||
* @internal
|
||||
*/
|
||||
public function getType(): string
|
||||
public function getType(): VisitType
|
||||
{
|
||||
return $this->type();
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public function getDate(): Chronos
|
||||
{
|
||||
return $this->date;
|
||||
}
|
||||
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return [
|
||||
@@ -175,12 +177,4 @@ class Visit extends AbstractEntity implements JsonSerializable
|
||||
'potentialBot' => $this->potentialBot,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public function getDate(): Chronos
|
||||
{
|
||||
return $this->date;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user