1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Movies\Service; |
6
|
|
|
|
7
|
|
|
use App\Countries\Repository\CountryRepository; |
8
|
|
|
use App\Movies\Entity\MovieReleaseDate; |
9
|
|
|
use App\Movies\Entity\ReleaseDateQueue; |
10
|
|
|
use App\Movies\Repository\ReleaseDateQueueRepository; |
11
|
|
|
use Doctrine\DBAL\Exception\UniqueConstraintViolationException; |
12
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
13
|
|
|
|
14
|
|
|
class ReleaseDateService |
15
|
|
|
{ |
16
|
|
|
private $em; |
17
|
|
|
private $repository; |
18
|
|
|
private $imdbReleaseDateService; |
19
|
|
|
private $countries; |
20
|
|
|
|
21
|
|
|
public function __construct(EntityManagerInterface $em, ReleaseDateQueueRepository $repository, ImdbReleaseDateService $imdbReleaseDateService, CountryRepository $countryRepository) |
22
|
|
|
{ |
23
|
|
|
$this->em = $em; |
24
|
|
|
$this->repository = $repository; |
25
|
|
|
$this->imdbReleaseDateService = $imdbReleaseDateService; |
26
|
|
|
$this->countries = $countryRepository->findAll(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function runCheck(): void |
30
|
|
|
{ |
31
|
|
|
$queueItems = $this->repository->findAllWithMovies()->getResult(); |
32
|
|
|
|
33
|
|
|
/** @var $queueItem ReleaseDateQueue */ |
34
|
|
|
foreach ($queueItems as $queueItem) { |
35
|
|
|
$allCountriesHaveReleaseDate = true; |
36
|
|
|
foreach ($this->countries as $country) { |
37
|
|
|
$movie = $queueItem->getMovie(); |
38
|
|
|
$releaseDate = $this->imdbReleaseDateService->getReleaseDate($movie, $country); |
39
|
|
|
if ($releaseDate === null) { |
40
|
|
|
$allCountriesHaveReleaseDate = false; |
41
|
|
|
} else { |
42
|
|
|
$movieReleaseDate = new MovieReleaseDate($movie, $country, $releaseDate); |
|
|
|
|
43
|
|
|
$this->em->persist($movieReleaseDate); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if ($allCountriesHaveReleaseDate === true) { |
48
|
|
|
$this->em->remove($queueItem); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
try { |
53
|
|
|
$this->em->flush(); |
54
|
|
|
} catch (UniqueConstraintViolationException $exception) { |
55
|
|
|
// If release date was saved early |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.