Completed
Push — master ( 5163a0...712d21 )
by Valentyn
06:59
created

MovieSyncProcessor   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A process() 0 18 2
A getSubscribedTopics() 0 4 1
1
<?php
2
3
namespace App\Movies\Event;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Interop\Queue\PsrMessage;
7
use Interop\Queue\PsrContext;
8
use Interop\Queue\PsrProcessor;
9
use Enqueue\Client\TopicSubscriberInterface;
10
use Psr\Log\LoggerInterface;
11
12
class MovieSyncProcessor implements PsrProcessor, TopicSubscriberInterface
13
{
14
    const ADD_MOVIES_TMDB = 'addMoviesTMDB';
15
    const UPDATE_MOVIES_TMDB = 'updateMoviesTMDB';
16
17
    private $em;
18
    private $logger;
19
20
    public function __construct(EntityManagerInterface $em, LoggerInterface $logger)
21
    {
22
        $this->em = $em;
23
        $this->logger = $logger;
24
    }
25
26
    public function process(PsrMessage $message, PsrContext $session)
27
    {
28
        $movies = $message->getBody();
29
        $movies = unserialize($movies);
30
        $moviesCount = 0;
31
32
        foreach ($movies as $movie) {
33
            $this->em->persist($movie);
34
            $moviesCount++;
35
        }
36
37
        $this->em->flush();
38
39
        $this->logger->debug("Successfully saved {$moviesCount} movies!\r\n");
40
        $this->logger->debug("Properties:\r\n", $message->getProperties());
41
42
        return self::ACK;
43
    }
44
45
    public static function getSubscribedTopics()
46
    {
47
        return [self::ADD_MOVIES_TMDB, self::UPDATE_MOVIES_TMDB];
48
    }
49
}