mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Inject DomainRepository in DomainService
This commit is contained in:
parent
dba9302f78
commit
102169b6c7
@ -67,6 +67,7 @@ return [
|
|||||||
Tag\Repository\TagRepository::class => [EntityRepositoryFactory::class, Tag\Entity\Tag::class],
|
Tag\Repository\TagRepository::class => [EntityRepositoryFactory::class, Tag\Entity\Tag::class],
|
||||||
|
|
||||||
Domain\DomainService::class => ConfigAbstractFactory::class,
|
Domain\DomainService::class => ConfigAbstractFactory::class,
|
||||||
|
Domain\Repository\DomainRepository::class => [EntityRepositoryFactory::class, Domain\Entity\Domain::class],
|
||||||
|
|
||||||
Visit\VisitsTracker::class => ConfigAbstractFactory::class,
|
Visit\VisitsTracker::class => ConfigAbstractFactory::class,
|
||||||
Visit\RequestTracker::class => ConfigAbstractFactory::class,
|
Visit\RequestTracker::class => ConfigAbstractFactory::class,
|
||||||
@ -167,7 +168,11 @@ return [
|
|||||||
ShortUrl\ShortUrlResolver::class,
|
ShortUrl\ShortUrlResolver::class,
|
||||||
],
|
],
|
||||||
ShortUrl\Helper\ShortCodeUniquenessHelper::class => ['em', Config\Options\UrlShortenerOptions::class],
|
ShortUrl\Helper\ShortCodeUniquenessHelper::class => ['em', Config\Options\UrlShortenerOptions::class],
|
||||||
Domain\DomainService::class => ['em', Config\Options\UrlShortenerOptions::class],
|
Domain\DomainService::class => [
|
||||||
|
'em',
|
||||||
|
Config\Options\UrlShortenerOptions::class,
|
||||||
|
Domain\Repository\DomainRepository::class,
|
||||||
|
],
|
||||||
|
|
||||||
Util\DoctrineBatchHelper::class => ['em'],
|
Util\DoctrineBatchHelper::class => ['em'],
|
||||||
Util\RedirectResponseHelper::class => [Config\Options\RedirectOptions::class],
|
Util\RedirectResponseHelper::class => [Config\Options\RedirectOptions::class],
|
||||||
|
@ -19,8 +19,11 @@ use function array_map;
|
|||||||
|
|
||||||
readonly class DomainService implements DomainServiceInterface
|
readonly class DomainService implements DomainServiceInterface
|
||||||
{
|
{
|
||||||
public function __construct(private EntityManagerInterface $em, private UrlShortenerOptions $urlShortenerOptions)
|
public function __construct(
|
||||||
{
|
private EntityManagerInterface $em,
|
||||||
|
private UrlShortenerOptions $urlShortenerOptions,
|
||||||
|
private DomainRepositoryInterface $repo,
|
||||||
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -49,9 +52,7 @@ readonly class DomainService implements DomainServiceInterface
|
|||||||
*/
|
*/
|
||||||
private function defaultDomainAndRest(ApiKey|null $apiKey): array
|
private function defaultDomainAndRest(ApiKey|null $apiKey): array
|
||||||
{
|
{
|
||||||
/** @var DomainRepositoryInterface $repo */
|
$allDomains = $this->repo->findDomains($apiKey);
|
||||||
$repo = $this->em->getRepository(Domain::class);
|
|
||||||
$allDomains = $repo->findDomains($apiKey);
|
|
||||||
$defaultDomain = null;
|
$defaultDomain = null;
|
||||||
$restOfDomains = [];
|
$restOfDomains = [];
|
||||||
|
|
||||||
@ -71,7 +72,6 @@ readonly class DomainService implements DomainServiceInterface
|
|||||||
*/
|
*/
|
||||||
public function getDomain(string $domainId): Domain
|
public function getDomain(string $domainId): Domain
|
||||||
{
|
{
|
||||||
/** @var Domain|null $domain */
|
|
||||||
$domain = $this->em->find(Domain::class, $domainId);
|
$domain = $this->em->find(Domain::class, $domainId);
|
||||||
if ($domain === null) {
|
if ($domain === null) {
|
||||||
throw DomainNotFoundException::fromId($domainId);
|
throw DomainNotFoundException::fromId($domainId);
|
||||||
@ -82,7 +82,7 @@ readonly class DomainService implements DomainServiceInterface
|
|||||||
|
|
||||||
public function findByAuthority(string $authority, ApiKey|null $apiKey = null): Domain|null
|
public function findByAuthority(string $authority, ApiKey|null $apiKey = null): Domain|null
|
||||||
{
|
{
|
||||||
return $this->em->getRepository(Domain::class)->findOneByAuthority($authority, $apiKey);
|
return $this->repo->findOneByAuthority($authority, $apiKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -15,7 +15,7 @@ use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions;
|
|||||||
use Shlinkio\Shlink\Core\Domain\DomainService;
|
use Shlinkio\Shlink\Core\Domain\DomainService;
|
||||||
use Shlinkio\Shlink\Core\Domain\Entity\Domain;
|
use Shlinkio\Shlink\Core\Domain\Entity\Domain;
|
||||||
use Shlinkio\Shlink\Core\Domain\Model\DomainItem;
|
use Shlinkio\Shlink\Core\Domain\Model\DomainItem;
|
||||||
use Shlinkio\Shlink\Core\Domain\Repository\DomainRepository;
|
use Shlinkio\Shlink\Core\Domain\Repository\DomainRepositoryInterface;
|
||||||
use Shlinkio\Shlink\Core\Exception\DomainNotFoundException;
|
use Shlinkio\Shlink\Core\Exception\DomainNotFoundException;
|
||||||
use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta;
|
use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta;
|
||||||
use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition;
|
use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition;
|
||||||
@ -25,19 +25,23 @@ class DomainServiceTest extends TestCase
|
|||||||
{
|
{
|
||||||
private DomainService $domainService;
|
private DomainService $domainService;
|
||||||
private MockObject & EntityManagerInterface $em;
|
private MockObject & EntityManagerInterface $em;
|
||||||
|
private MockObject & DomainRepositoryInterface $repo;
|
||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$this->em = $this->createMock(EntityManagerInterface::class);
|
$this->em = $this->createMock(EntityManagerInterface::class);
|
||||||
$this->domainService = new DomainService($this->em, new UrlShortenerOptions(defaultDomain: 'default.com'));
|
$this->repo = $this->createMock(DomainRepositoryInterface::class);
|
||||||
|
$this->domainService = new DomainService(
|
||||||
|
$this->em,
|
||||||
|
new UrlShortenerOptions(defaultDomain: 'default.com'),
|
||||||
|
$this->repo,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[Test, DataProvider('provideExcludedDomains')]
|
#[Test, DataProvider('provideExcludedDomains')]
|
||||||
public function listDomainsDelegatesIntoRepository(array $domains, array $expectedResult, ApiKey|null $apiKey): void
|
public function listDomainsDelegatesIntoRepository(array $domains, array $expectedResult, ApiKey|null $apiKey): void
|
||||||
{
|
{
|
||||||
$repo = $this->createMock(DomainRepository::class);
|
$this->repo->expects($this->once())->method('findDomains')->with($apiKey)->willReturn($domains);
|
||||||
$repo->expects($this->once())->method('findDomains')->with($apiKey)->willReturn($domains);
|
|
||||||
$this->em->expects($this->once())->method('getRepository')->with(Domain::class)->willReturn($repo);
|
|
||||||
|
|
||||||
$result = $this->domainService->listDomains($apiKey);
|
$result = $this->domainService->listDomains($apiKey);
|
||||||
|
|
||||||
@ -127,11 +131,9 @@ class DomainServiceTest extends TestCase
|
|||||||
public function getOrCreateAlwaysPersistsDomain(Domain|null $foundDomain, ApiKey|null $apiKey): void
|
public function getOrCreateAlwaysPersistsDomain(Domain|null $foundDomain, ApiKey|null $apiKey): void
|
||||||
{
|
{
|
||||||
$authority = 'example.com';
|
$authority = 'example.com';
|
||||||
$repo = $this->createMock(DomainRepository::class);
|
$this->repo->expects($this->once())->method('findOneByAuthority')->with($authority, $apiKey)->willReturn(
|
||||||
$repo->method('findOneByAuthority')->with($authority, $apiKey)->willReturn(
|
|
||||||
$foundDomain,
|
$foundDomain,
|
||||||
);
|
);
|
||||||
$this->em->expects($this->once())->method('getRepository')->with(Domain::class)->willReturn($repo);
|
|
||||||
$this->em->expects($this->once())->method('persist')->with($foundDomain ?? $this->isInstanceOf(Domain::class));
|
$this->em->expects($this->once())->method('persist')->with($foundDomain ?? $this->isInstanceOf(Domain::class));
|
||||||
$this->em->expects($this->once())->method('flush');
|
$this->em->expects($this->once())->method('flush');
|
||||||
|
|
||||||
@ -149,9 +151,7 @@ class DomainServiceTest extends TestCase
|
|||||||
$domain = Domain::withAuthority($authority);
|
$domain = Domain::withAuthority($authority);
|
||||||
$domain->setId('1');
|
$domain->setId('1');
|
||||||
$apiKey = ApiKey::fromMeta(ApiKeyMeta::withRoles(RoleDefinition::forDomain($domain)));
|
$apiKey = ApiKey::fromMeta(ApiKeyMeta::withRoles(RoleDefinition::forDomain($domain)));
|
||||||
$repo = $this->createMock(DomainRepository::class);
|
$this->repo->expects($this->once())->method('findOneByAuthority')->with($authority, $apiKey)->willReturn(null);
|
||||||
$repo->method('findOneByAuthority')->with($authority, $apiKey)->willReturn(null);
|
|
||||||
$this->em->expects($this->once())->method('getRepository')->with(Domain::class)->willReturn($repo);
|
|
||||||
$this->em->expects($this->never())->method('persist');
|
$this->em->expects($this->never())->method('persist');
|
||||||
$this->em->expects($this->never())->method('flush');
|
$this->em->expects($this->never())->method('flush');
|
||||||
|
|
||||||
@ -166,9 +166,9 @@ class DomainServiceTest extends TestCase
|
|||||||
ApiKey|null $apiKey,
|
ApiKey|null $apiKey,
|
||||||
): void {
|
): void {
|
||||||
$authority = 'example.com';
|
$authority = 'example.com';
|
||||||
$repo = $this->createMock(DomainRepository::class);
|
$this->repo->expects($this->once())->method('findOneByAuthority')->with($authority, $apiKey)->willReturn(
|
||||||
$repo->method('findOneByAuthority')->with($authority, $apiKey)->willReturn($foundDomain);
|
$foundDomain,
|
||||||
$this->em->expects($this->once())->method('getRepository')->with(Domain::class)->willReturn($repo);
|
);
|
||||||
$this->em->expects($this->once())->method('persist')->with($foundDomain ?? $this->isInstanceOf(Domain::class));
|
$this->em->expects($this->once())->method('persist')->with($foundDomain ?? $this->isInstanceOf(Domain::class));
|
||||||
$this->em->expects($this->once())->method('flush');
|
$this->em->expects($this->once())->method('flush');
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user