GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#234)
by David
02:05
created

PersistentQueue   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 10
Bugs 1 Features 1
Metric Value
wmc 13
c 10
b 1
f 1
lcom 1
cbo 4
dl 0
loc 125
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A register() 0 6 1
A count() 0 6 1
A close() 0 6 1
A enqueue() 0 11 2
A acknowledge() 0 10 2
A dequeue() 0 15 2
A peek() 0 8 1
A getReceipt() 0 8 2
1
<?php
2
3
namespace Bernard\Queue;
4
5
use Bernard\Driver;
6
use Bernard\Envelope;
7
use Bernard\Serializer;
8
9
/**
10
 * @package Bernard
11
 */
12
class PersistentQueue extends AbstractQueue
13
{
14
    protected $driver;
15
    protected $serializer;
16
    protected $receipts;
17
18
    /**
19
     * @param string     $name
20
     * @param Driver     $driver
21
     * @param Serializer $serializer
22
     */
23
    public function __construct($name, Driver $driver, Serializer $serializer)
24
    {
25
        parent::__construct($name);
26
27
        $this->driver = $driver;
28
        $this->serializer = $serializer;
29
        $this->receipts = new \SplObjectStorage();
30
31
        $this->register();
32
    }
33
34
    /**
35
     * Register with the driver
36
     */
37
    public function register()
38
    {
39
        $this->errorIfClosed();
40
41
        $this->driver->createQueue($this->name);
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function count()
48
    {
49
        $this->errorIfClosed();
50
51
        return $this->driver->countMessages($this->name);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function close()
58
    {
59
        parent::close();
60
61
        $this->driver->removeQueue($this->name);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function enqueue(Envelope $envelope)
68
    {
69
        $this->errorIfClosed();
70
71
        $receipt = $this->driver->pushMessage($this->name, $this->serializer->serialize($envelope));
72
73
        if ($receipt) {
74
            $envelope->setReceipt($receipt);
75
            $this->receipts->attach($envelope, $receipt);
76
        }
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function acknowledge(Envelope $envelope)
83
    {
84
        $this->errorIfClosed();
85
86
        if ($this->receipts->contains($envelope)) {
87
            $this->driver->acknowledgeMessage($this->name, $this->receipts[$envelope]);
88
89
            $this->receipts->detach($envelope);
90
        }
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function dequeue()
97
    {
98
        $this->errorIfClosed();
99
100
        list($serialized, $receipt) = $this->driver->popMessage($this->name);
101
102
        if ($serialized) {
103
            $envelope = $this->serializer->unserialize($serialized);
104
105
            $envelope->setReceipt($receipt);
106
            $this->receipts->attach($envelope, $receipt);
107
108
            return $envelope;
109
        }
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function peek($index = 0, $limit = 20)
116
    {
117
        $this->errorIfClosed();
118
119
        $messages = $this->driver->peekQueue($this->name, $index, $limit);
120
121
        return array_map(array($this->serializer, 'unserialize'), $messages);
122
    }
123
124
    /**
125
     * @param Envelope $envelope The envelope.
126
     * @return mixed
127
     */
128
    public function getReceipt(Envelope $envelope)
129
    {
130
        if (!$this->receipts->contains($envelope)) {
131
            return null;
132
        }
133
134
        return $this->receipts->offsetGet($envelope);
135
    }
136
}
137