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
|
|
|
use Psr\Log\LoggerInterface; |
14
|
|
|
|
15
|
|
|
class ReleaseDateService |
16
|
|
|
{ |
17
|
|
|
private $em; |
18
|
|
|
private $repository; |
19
|
|
|
private $imdbReleaseDateService; |
20
|
|
|
private $countryRepository; |
21
|
|
|
private $countries; |
22
|
|
|
private $logger; |
23
|
|
|
|
24
|
|
|
public function __construct(EntityManagerInterface $em, ReleaseDateQueueRepository $repository, ImdbReleaseDateService $imdbReleaseDateService, CountryRepository $countryRepository, LoggerInterface $logger) |
25
|
|
|
{ |
26
|
|
|
$this->em = $em; |
27
|
|
|
$this->repository = $repository; |
28
|
|
|
$this->imdbReleaseDateService = $imdbReleaseDateService; |
29
|
|
|
$this->countryRepository = $countryRepository; |
30
|
|
|
$this->logger = $logger; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function runCheck(): void |
34
|
|
|
{ |
35
|
|
|
$queueItems = $this->repository->findAllWithMovies()->getResult(); |
36
|
|
|
$this->countries = $this->countryRepository->findAll(); |
37
|
|
|
|
38
|
|
|
$this->logger->debug('[ReleaseDateService] runCheck', [ |
39
|
|
|
'count' => \count($queueItems), |
40
|
|
|
]); |
41
|
|
|
|
42
|
|
|
/** @var $queueItem ReleaseDateQueue */ |
43
|
|
|
foreach ($queueItems as $queueItem) { |
44
|
|
|
$allCountriesHaveReleaseDate = true; |
45
|
|
|
$movie = $queueItem->getMovie(); |
46
|
|
|
foreach ($this->countries as $country) { |
47
|
|
|
$releaseDate = $this->imdbReleaseDateService->getReleaseDate($movie, $country); |
48
|
|
|
|
49
|
|
|
$this->logger->debug('[ReleaseDateService] trying to load release date for movie', [ |
50
|
|
|
'movie' => sprintf('%s with id: %s', $movie->getOriginalTitle(), $movie->getId()), |
51
|
|
|
'releaseDate' => $releaseDate ? $releaseDate->format('Y-m-d') : '[EMPTY]', |
52
|
|
|
]); |
53
|
|
|
|
54
|
|
|
if ($releaseDate === null) { |
55
|
|
|
$allCountriesHaveReleaseDate = false; |
56
|
|
|
} else { |
57
|
|
|
$this->logger->debug( |
58
|
|
|
sprintf('[ReleaseDateService] Added new release date %s for movie %s in country %s', |
59
|
|
|
$releaseDate->format('Y-m-d'), |
60
|
|
|
$movie->getOriginalTitle(), |
61
|
|
|
$country->getName() |
62
|
|
|
) |
63
|
|
|
); |
64
|
|
|
$movieReleaseDate = new MovieReleaseDate($movie, $country, $releaseDate); |
|
|
|
|
65
|
|
|
$this->em->persist($movieReleaseDate); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($allCountriesHaveReleaseDate === true) { |
70
|
|
|
$this->logger->debug( |
71
|
|
|
sprintf('[ReleaseDateService] Movie %s removed from queue. All available countries already have release dates', |
72
|
|
|
$movie->getOriginalTitle() |
73
|
|
|
) |
74
|
|
|
); |
75
|
|
|
$this->em->remove($queueItem); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
try { |
80
|
|
|
$this->em->flush(); |
81
|
|
|
} catch (UniqueConstraintViolationException $exception) { |
82
|
|
|
// If release date was saved early |
83
|
|
|
$this->logger->debug("[ReleaseDateService] Exception with message: {$exception->getMessage()}"); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
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
.