Completed
Push — master ( 134712...8789de )
by Peter
06:34
created

PredisUniqueCommandQueue::push()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
rs 9.6666
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
/**
4
 * GpsLab component.
5
 *
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2011, Peter Gribanov
8
 * @license   http://opensource.org/licenses/MIT
9
 */
10
11
namespace GpsLab\Component\Command\Queue;
12
13
use GpsLab\Component\Command\Command;
14
use Predis\Client;
15
use Psr\Log\LoggerInterface;
16
use Symfony\Component\Serializer\Serializer;
17
18
class PredisUniqueCommandQueue implements CommandQueue
19
{
20
    const LIST_KEY = 'unique_commands';
21
    const FORMAT = PredisCommandQueue::FORMAT;
22
23
    /**
24
     * @var Client
25
     */
26
    private $client;
27
28
    /**
29
     * @var Serializer
30
     */
31
    private $serializer;
32
33
    /**
34
     * @var LoggerInterface
35
     */
36
    private $logger;
37
38
    /**
39
     * @param Client          $client
40
     * @param Serializer      $serializer
41
     * @param LoggerInterface $logger
42
     */
43
    public function __construct(Client $client, Serializer $serializer, LoggerInterface $logger)
44
    {
45
        $this->client = $client;
46
        $this->serializer = $serializer;
47
        $this->logger = $logger;
48
    }
49
50
    /**
51
     * Push command to queue.
52
     *
53
     * @param Command $command
54
     *
55
     * @return bool
56
     */
57
    public function push(Command $command)
58
    {
59
        $value = $this->serializer->normalize($command, self::FORMAT);
60
61
        // remove exists command and push it again
62
        $this->client->lrem(self::LIST_KEY, 0, $value);
63
64
        return (bool) $this->client->rpush(self::LIST_KEY, [$value]);
65
    }
66
67
    /**
68
     * Pop command from queue. Return NULL if queue is empty.
69
     *
70
     * @return Command|null
71
     */
72 View Code Duplication
    public function pop()
0 ignored issues
show
Duplication introduced by
This method 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...
73
    {
74
        $value = $this->client->lpop(self::LIST_KEY);
75
76
        if (!$value) {
77
            return null;
78
        }
79
80
        try {
81
            return $this->serializer->denormalize($value, Command::class, self::FORMAT);
82
        } catch (\Exception $e) {
83
            // it's a critical error
84
            // it is necessary to react quickly to it
85
            $this->logger->critical('Failed denormalize a command in the Redis queue', [$value, $e->getMessage()]);
86
87
            // try denormalize in later
88
            $this->client->rpush(self::LIST_KEY, [$value]);
89
90
            return null;
91
        }
92
    }
93
}
94