AddSimilarMoviesProcessor::process()   B
last analyzed

Complexity

Conditions 11
Paths 8

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 0
cts 25
cp 0
rs 7.3166
c 0
b 0
f 0
cc 11
nc 8
nop 2
crap 132

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Movies\EventListener;
4
5
use App\Movies\Entity\Movie;
6
use App\Movies\Repository\MovieRepository;
7
use App\Movies\Service\TmdbSearchService;
8
use App\Users\Entity\User;
9
use Doctrine\ORM\EntityManager;
10
use Doctrine\ORM\EntityManagerInterface;
11
use Enqueue\Client\TopicSubscriberInterface;
12
use Interop\Queue\Context;
13
use Interop\Queue\Message as QMessage;
14
use Interop\Queue\Processor;
15
16
class AddSimilarMoviesProcessor implements Processor, TopicSubscriberInterface
17
{
18
    const ADD_SIMILAR_MOVIES = 'AddSimilarMovies';
19
20
    /** @var EntityManager */
21
    private $em;
22
    private $searchService;
23
    private $movieRepository;
24
25
    public function __construct(EntityManagerInterface $em, MovieRepository $movieRepository, TmdbSearchService $searchService)
26
    {
27
        $this->em = $em;
0 ignored issues
show
Documentation Bug introduced by
$em is of type object<Doctrine\ORM\EntityManagerInterface>, but the property $em was declared to be of type object<Doctrine\ORM\EntityManager>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
28
        $this->movieRepository = $movieRepository;
29
        $this->searchService = $searchService;
30
    }
31
32
    public function process(QMessage $message, Context $session)
33
    {
34
        $moviesTable = $message->getBody();
35
        $moviesTable = json_decode($moviesTable, true);
36
37
        if ($this->em->isOpen() === false) {
38
            throw new \ErrorException('em is closed');
39
        }
40
41
        $originalMoviesIds = array_keys($moviesTable);
42
        $movies = $this->movieRepository->findAllByIds($originalMoviesIds);
43
        $supportAcc = $this->em->getReference(User::class, 1);
44
45
        //todo add tests
46
        foreach ($movies as $movie) {
47
            $similarMovies = $this->movieRepository->findAllIdsByTmdbIds($moviesTable[$movie->getId()]);
48
            foreach ($similarMovies as $similarMovie) {
49
                $similarMovieRef = $this->em->getReference(Movie::class, $similarMovie['id']);
50
                $movie->addSimilarMovie($similarMovieRef);
51
                if (is_numeric($similarMovie['tmdb.voteAverage']) && $similarMovie['tmdb.voteAverage'] >= 7) {
52
                    if ($similarMovie['releaseDate'] !== null) {
53
                        $releaseDate = new \DateTimeImmutable($similarMovie['releaseDate']);
54
                        // some kind of discrimination of old movies
55
                        if ($releaseDate->format('Y') <= 1980 && $similarMovie['tmdb.voteAverage'] <= 8) {
56
                            continue;
57
                        }
58
                        if ($releaseDate->format('Y') <= 1990 && $similarMovie['tmdb.voteAverage'] <= 7) {
59
                            continue;
60
                        }
61
                    }
62
63
                    $movie->addRecommendation($supportAcc, $similarMovieRef);
64
                }
65
            }
66
67
            $this->em->persist($movie);
68
        }
69
70
        $this->em->flush();
71
        $this->em->clear();
72
73
        return self::ACK;
74
    }
75
76
    public static function getSubscribedTopics()
77
    {
78
        return [self::ADD_SIMILAR_MOVIES];
79
    }
80
}
81