Create service to delete orphan visits

This commit is contained in:
Alejandro Celaya 2023-05-18 09:01:57 +02:00
parent a4d8ebdfc9
commit abcf2f86be
4 changed files with 78 additions and 0 deletions

View file

@ -62,6 +62,7 @@ return [
Visit\VisitsTracker::class => ConfigAbstractFactory::class, Visit\VisitsTracker::class => ConfigAbstractFactory::class,
Visit\RequestTracker::class => ConfigAbstractFactory::class, Visit\RequestTracker::class => ConfigAbstractFactory::class,
Visit\VisitsDeleter::class => ConfigAbstractFactory::class,
Visit\Geolocation\VisitLocator::class => ConfigAbstractFactory::class, Visit\Geolocation\VisitLocator::class => ConfigAbstractFactory::class,
Visit\Geolocation\VisitToLocationHelper::class => ConfigAbstractFactory::class, Visit\Geolocation\VisitToLocationHelper::class => ConfigAbstractFactory::class,
Visit\VisitsStatsHelper::class => ConfigAbstractFactory::class, Visit\VisitsStatsHelper::class => ConfigAbstractFactory::class,
@ -122,6 +123,7 @@ return [
Options\TrackingOptions::class, Options\TrackingOptions::class,
], ],
Visit\RequestTracker::class => [Visit\VisitsTracker::class, Options\TrackingOptions::class], Visit\RequestTracker::class => [Visit\VisitsTracker::class, Options\TrackingOptions::class],
Visit\VisitsDeleter::class => [Visit\Repository\VisitDeleterRepository::class],
ShortUrl\ShortUrlService::class => [ ShortUrl\ShortUrlService::class => [
'em', 'em',
ShortUrl\ShortUrlResolver::class, ShortUrl\ShortUrlResolver::class,

View file

@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Visit;
use Shlinkio\Shlink\Core\Model\BulkDeleteResult;
use Shlinkio\Shlink\Core\Visit\Repository\VisitDeleterRepositoryInterface;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
class VisitsDeleter implements VisitsDeleterInterface
{
public function __construct(private readonly VisitDeleterRepositoryInterface $repository)
{
}
public function deleteOrphanVisits(?ApiKey $apiKey = null): BulkDeleteResult
{
// TODO Check API key has permissions for orphan visits
return new BulkDeleteResult($this->repository->deleteOrphanVisits());
}
}

View file

@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Visit;
use Shlinkio\Shlink\Core\Model\BulkDeleteResult;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
interface VisitsDeleterInterface
{
public function deleteOrphanVisits(?ApiKey $apiKey = null): BulkDeleteResult;
}

View file

@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Visit;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Core\Visit\Repository\VisitDeleterRepositoryInterface;
use Shlinkio\Shlink\Core\Visit\VisitsDeleter;
class VisitsDeleterTest extends TestCase
{
private VisitsDeleter $visitsDeleter;
private MockObject & VisitDeleterRepositoryInterface $repo;
protected function setUp(): void
{
$this->repo = $this->createMock(VisitDeleterRepositoryInterface::class);
$this->visitsDeleter = new VisitsDeleter($this->repo);
}
#[Test, DataProvider('provideVisitsCounts')]
public function returnsDeletedVisitsFromRepo(int $visitsCount): void
{
$this->repo->expects($this->once())->method('deleteOrphanVisits')->willReturn($visitsCount);
$result = $this->visitsDeleter->deleteOrphanVisits();
self::assertEquals($visitsCount, $result->affectedItems);
}
public static function provideVisitsCounts(): iterable
{
yield '45' => [45];
yield '5000' => [5000];
yield '0' => [0];
}
}