Completed
Push — master ( 9bf5de...63a510 )
by Peter
22:57
created

PredisEventQueue   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 73
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 73
loc 73
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 6 6 1
A push() 6 6 1
A pop() 21 21 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * GpsLab component.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2016, Peter Gribanov
7
 * @license   http://opensource.org/licenses/MIT
8
 */
9
10
namespace GpsLab\Domain\Event\Queue;
11
12
use GpsLab\Domain\Event\EventInterface;
13
use Predis\Client;
14
use Psr\Log\LoggerInterface;
15
use Symfony\Component\Serializer\Serializer;
16
17 View Code Duplication
class PredisEventQueue implements EventQueueInterface
1 ignored issue
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
18
{
19
    const SET_KEY = 'events';
20
    const FORMAT = 'predis';
21
22
    /**
23
     * @var Client
24
     */
25
    private $client;
26
27
    /**
28
     * @var Serializer
29
     */
30
    private $serializer;
31
32
    /**
33
     * @var LoggerInterface
34
     */
35
    private $logger;
36
37
    /**
38
     * @param Client $client
39
     * @param Serializer $serializer
40
     * @param LoggerInterface $logger
41
     */
42
    public function __construct(Client $client, Serializer $serializer, LoggerInterface $logger)
43
    {
44
        $this->client = $client;
45
        $this->serializer = $serializer;
46
        $this->logger = $logger;
47
    }
48
49
    /**
50
     * Push event to queue.
51
     *
52
     * @param EventInterface $event
53
     *
54
     * @return bool
55
     */
56
    public function push(EventInterface $event)
57
    {
58
        $value = $this->serializer->normalize($event, self::FORMAT);
59
60
        return (bool)$this->client->lpush(self::SET_KEY, [$value]);
61
    }
62
63
    /**
64
     * Pop event from queue. Return NULL if queue is empty.
65
     *
66
     * @return EventInterface|null
67
     */
68
    public function pop()
69
    {
70
        $value = $this->client->lpop(self::SET_KEY);
71
72
        if (!$value) {
73
            return null;
74
        }
75
76
        try {
77
            return $this->serializer->denormalize($value, EventInterface::class, self::FORMAT);
78
        } catch (\Exception $e) {
79
            // it's a critical error
80
            // it is necessary to react quickly to it
81
            $this->logger->critical('Failed denormalize a event in the Redis queue', [$value, $e->getMessage()]);
82
83
            // try denormalize in later
84
            $this->client->rpush(self::SET_KEY, [$value]);
85
86
            return null;
87
        }
88
    }
89
}
90