Completed
Push — master ( 53c691...aee4b3 )
by Peter
02:49
created

PredisPullCommandQueue::publish()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 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\Pull;
12
13
use GpsLab\Component\Command\Command;
14
use Predis\Client;
15
use Psr\Log\LoggerInterface;
16
use Symfony\Component\Serializer\SerializerInterface;
17
18
class PredisPullCommandQueue implements PullCommandQueue
19
{
20
    const DEFAULT_FORMAT = 'predis';
21
22
    /**
23
     * @var Client
24
     */
25
    private $client;
26
27
    /**
28
     * @var SerializerInterface
29
     */
30
    private $serializer;
31
32
    /**
33
     * @var LoggerInterface
34
     */
35
    private $logger;
36
37
    /**
38
     * @var string
39
     */
40
    private $queue_name = '';
41
42
    /**
43
     * @var string
44
     */
45
    private $format = '';
46
47
    /**
48
     * @param Client              $client
49
     * @param SerializerInterface $serializer
50
     * @param LoggerInterface     $logger
51
     * @param string              $queue_name
52
     * @param string|null         $format
53
     */
54 6
    public function __construct(
55
        Client $client,
56
        SerializerInterface $serializer,
57
        LoggerInterface $logger,
58
        $queue_name,
59
        $format = null
60
    ) {
61 6
        $this->client = $client;
62 6
        $this->serializer = $serializer;
63 6
        $this->logger = $logger;
64 6
        $this->queue_name = $queue_name;
65 6
        $this->format = $format ?: self::DEFAULT_FORMAT;
66 6
    }
67
68
    /**
69
     * Publish command to queue.
70
     *
71
     * @param Command $command
72
     *
73
     * @return bool
74
     */
75 2
    public function publish(Command $command)
76
    {
77 2
        $value = $this->serializer->serialize($command, $this->format);
78
79 2
        return (bool) $this->client->rpush($this->queue_name, [$value]);
80
    }
81
82
    /**
83
     * Pop command from queue. Return NULL if queue is empty.
84
     *
85
     * @return Command|null
86
     */
87 4
    public function pull()
88
    {
89 4
        $value = $this->client->lpop($this->queue_name);
90
91 4
        if (!$value) {
92 2
            return null;
93
        }
94
95
        try {
96 4
            return $this->serializer->deserialize($value, Command::class, $this->format);
97 2
        } catch (\Exception $e) {
98
            // it's a critical error
99
            // it is necessary to react quickly to it
100 2
            $this->logger->critical('Failed denormalize a command in the Redis queue', [$value, $e->getMessage()]);
101
102
            // try denormalize in later
103 2
            $this->client->rpush($this->queue_name, [$value]);
104
105 2
            return null;
106
        }
107
    }
108
}
109