Completed
Push — master ( d15cf4...5163a0 )
by Valentyn
06:05
created

TmdbSyncService::addMovies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace App\Movies\Service;
5
6
use App\Movies\Entity\Movie;
7
use App\Movies\Event\MovieSyncProcessor;
8
use App\Movies\Repository\MovieRepository;
9
use Doctrine\ORM\EntityManagerInterface;
10
use Psr\Log\LoggerInterface;
11
use Enqueue\Client\ProducerInterface;
12
13
class TmdbSyncService
14
{
15
    private $logger;
16
    private $repository;
17
    private $em;
18
    private $producer;
19
20
    public function __construct(LoggerInterface $logger, MovieRepository $repository, EntityManagerInterface $entityManager, ProducerInterface $producer)
21
    {
22
        $this->logger = $logger;
23
        $this->repository = $repository;
24
        $this->em = $entityManager;
25
        $this->producer = $producer;
26
    }
27
28
    public function syncMovies(array $movies): void
29
    {
30
        if (false === $this->isSupport(reset($movies))) {
31
            $this->logger->error('Unsupported array of movies provided', [
32
                'movies' => $movies
33
            ]);
34
            throw new \InvalidArgumentException('Unsupported array of movies provided');
35
        }
36
37
        // todo movies to update
38
        $moviesToSave = $this->getMoviesToSave($movies);
39
        $this->addMovies($moviesToSave);
40
    }
41
42
    private function addMovies(array $movies): void
43
    {
44
        $this->logger->debug('START Send event ADD_MOVIES_TMDB');
45
        $this->producer->sendEvent(MovieSyncProcessor::ADD_MOVIES_TMDB, serialize($movies));
46
        $this->logger->debug('END Send event ADD_MOVIES_TMDB');
47
        /*
48
        // todo call method to add movies into the queue and then insert them into the database
49
        foreach ($movies as $movie) {
50
            $this->em->persist($movie);
51
        }
52
53
        $this->em->flush();*/
54
    }
55
56
    private function updateMovies(array $movies)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
Unused Code introduced by
The parameter $movies is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
57
    {
58
        // todo call method to update movies in the database (use queue)
59
    }
60
61
    /**
62
     * This method give you array of movies which is not yet loaded to our database
63
     * @param array $movies
64
     * @return array
65
     */
66
    private function getMoviesToSave(array $movies): array
67
    {
68
        $ids = array_map(function (Movie $movie) { return $movie->getTmdb()->getId(); }, $movies);
69
        $alreadySavedIds = $this->getAlreadySavedMoviesIdsByTmdbIds($ids);
70
71
        return array_filter($movies, function (Movie $movie) use ($alreadySavedIds) {
72
            return in_array($movie->getTmdb()->getId(), $alreadySavedIds) === false;
73
        });
74
    }
75
76
    private function getAlreadySavedMoviesIdsByTmdbIds(array $ids)
77
    {
78
         return $this->repository->getTmdbIds($ids);
79
    }
80
81
    private function isSupport($movie)
82
    {
83
        return $movie instanceof Movie;
84
    }
85
}