onMovieAddedFromTmdb()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 0
cts 14
cp 0
rs 9.2248
c 0
b 0
f 0
cc 5
nc 6
nop 1
crap 30
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Movies\EventListener;
6
7
use App\Movies\Entity\ReleaseDateQueue;
8
use App\Movies\Event\MovieAddedFromTmdbEvent;
9
use App\Movies\Service\ImdbIdLoaderService;
10
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
11
use Doctrine\ORM\EntityManagerInterface;
12
13
class MovieAddedFromTmdbEventListener
14
{
15
    /** @var $em EntityManagerInterface */
16
    private $em;
17
18
    private $imdbIdLoaderService;
19
20 2
    public function __construct(EntityManagerInterface $em, ImdbIdLoaderService $imdbIdLoaderService)
21
    {
22 2
        $this->em = $em;
23 2
        $this->imdbIdLoaderService = $imdbIdLoaderService;
24 2
    }
25
26
    public function onMovieAddedFromTmdb(MovieAddedFromTmdbEvent $event): void
27
    {
28
        $movie = $event->getMovie();
29
30
        if ($movie->getImdbId() === null) {
31
            $theMovieDbId = $movie->getTmdb()->getId();
32
            $imdbId = $this->imdbIdLoaderService->getImdbId($theMovieDbId);
33
            $movie->setImdbId($imdbId);
34
            $this->em->persist($movie);
35
            $this->em->flush();
36
        }
37
38
        // If we dont know release date yet or movie was/would be released this year or in the future
39
        // All movies in queue will be checked every day for any info about release date (parsing imdb/tmdb)
40
        if ($movie->getReleaseDate() === null || $movie->getReleaseDate()->format('Y') >= date('Y')) {
41
            $releaseDateQueueItem = new ReleaseDateQueue($movie);
42
            $this->em->persist($releaseDateQueueItem);
43
            try {
44
                $this->em->flush();
45
            } catch (UniqueConstraintViolationException $exception) {
46
                // It's ok
47
            }
48
        }
49
    }
50
}
51