| 1 | <?php |
||
| 11 | class DeleteShortUrlService implements DeleteShortUrlServiceInterface |
||
| 12 | { |
||
| 13 | use FindShortCodeTrait; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var EntityManagerInterface |
||
| 17 | */ |
||
| 18 | private $em; |
||
| 19 | /** |
||
| 20 | * @var DeleteShortUrlsOptions |
||
| 21 | */ |
||
| 22 | private $deleteShortUrlsOptions; |
||
| 23 | |||
| 24 | 4 | public function __construct(EntityManagerInterface $em, DeleteShortUrlsOptions $deleteShortUrlsOptions) |
|
| 29 | |||
| 30 | /** |
||
| 31 | * @throws Exception\InvalidShortCodeException |
||
| 32 | * @throws Exception\DeleteShortUrlException |
||
| 33 | */ |
||
| 34 | 4 | public function deleteByShortCode(string $shortCode, bool $ignoreThreshold = false): void |
|
| 35 | { |
||
| 36 | 4 | $shortUrl = $this->findByShortCode($this->em, $shortCode); |
|
| 37 | 4 | if (! $ignoreThreshold && $this->isThresholdReached($shortUrl)) { |
|
| 38 | 1 | throw Exception\DeleteShortUrlException::fromVisitsThreshold( |
|
| 39 | 1 | $this->deleteShortUrlsOptions->getVisitsThreshold(), |
|
| 40 | 1 | $shortUrl->getShortCode() |
|
| 41 | ); |
||
| 42 | } |
||
| 43 | |||
| 44 | 3 | $this->em->remove($shortUrl); |
|
| 45 | 3 | $this->em->flush(); |
|
| 46 | 3 | } |
|
| 47 | |||
| 48 | 3 | private function isThresholdReached(ShortUrl $shortUrl): bool |
|
| 56 | } |
||
| 57 |