AddRecommendationProcessor::process()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 16
cts 16
cp 1
rs 9.408
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 4
1
<?php
2
3
namespace App\Movies\EventListener;
4
5
use App\Movies\Repository\MovieRepository;
6
use App\Users\Entity\User;
7
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
8
use Doctrine\ORM\EntityManagerInterface;
9
use Enqueue\Client\TopicSubscriberInterface;
10
use Interop\Queue\Context;
11
use Interop\Queue\Message as QMessage;
12
use Interop\Queue\Processor;
13
14
class AddRecommendationProcessor implements Processor, TopicSubscriberInterface
15
{
16
    public const ADD_RECOMMENDATION = 'AddRecommendation';
17
18
    private $em;
19
    private $movieRepository;
20
21 4
    public function __construct(EntityManagerInterface $em, MovieRepository $movieRepository)
22
    {
23 4
        $this->em = $em;
24 4
        $this->movieRepository = $movieRepository;
25 4
    }
26
27 4
    public function process(QMessage $message, Context $session)
28
    {
29 4
        $movie = $message->getBody();
30 4
        $movie = json_decode($movie, true);
31
32 4
        $originalMovie = $this->movieRepository->findOneByIdOrTmdbId($movie['movie_id']);
33
34 4
        if ($originalMovie === null) {
35 1
            return self::REJECT;
36
        }
37
38 3
        $recommendedMovie = $this->movieRepository->findOneByIdOrTmdbId(null, $movie['tmdb_id']);
39
40 3
        if ($recommendedMovie === null) {
41 1
            return self::REJECT;
42
        }
43
44 2
        $user = $this->em->getReference(User::class, $movie['user_id']);
45
46 2
        $originalMovie->addRecommendation($user, $recommendedMovie);
0 ignored issues
show
Documentation introduced by
$user is of type object|null, but the function expects a object<App\Users\Entity\User>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$recommendedMovie is of type object<App\Movies\Entity\Movie>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
47 2
        $this->em->persist($originalMovie);
48
49
        try {
50 2
            $this->em->flush();
51 1
        } catch (UniqueConstraintViolationException $uniqueConstraintViolationException) {
52
            // do nothing, it's ok
53
        }
54
55 2
        $this->em->clear();
56
57 2
        return self::ACK;
58
    }
59
60
    public static function getSubscribedTopics()
61
    {
62
        return [self::ADD_RECOMMENDATION];
63
    }
64
}
65