MovieAddedFromTmdbEventListener   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 22.22%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 6
dl 0
loc 38
ccs 4
cts 18
cp 0.2222
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A onMovieAddedFromTmdb() 0 24 5
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