Completed
Push — master ( c5110b...a167e6 )
by Valentyn
13:46
created

MovieAddedFromTmdbEventListener   A

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 we dont know release date yet or it would be in the future - add movie to queue
31
        // All movies in queue will be checked every day for any info about release date (parsing imdb/tmdb)
32
        if ($movie->getReleaseDate() === null || $movie->getReleaseDate()->getTimestamp() > time()) {
33
            $releaseDateQueueItem = new ReleaseDateQueue($movie);
34
            $this->em->persist($releaseDateQueueItem);
35
            try {
36
                $this->em->flush();
37
            } catch (UniqueConstraintViolationException $exception) {
38
                // It's ok
39
            }
40
        }
41
42
        if ($movie->getImdbId() === null) {
43
            $theMovieDbId = $movie->getTmdb()->getId();
44
            $imdbId = $this->imdbIdLoaderService->getImdbId($theMovieDbId);
45
            $movie->setImdbId($imdbId);
46
            $this->em->persist($movie);
47
            $this->em->flush();
48
        }
49
    }
50
}
51