Completed
Pull Request — master (#16)
by Akihito
02:30
created

AbstractQueue   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 98
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A sendMessage() 0 4 1
A receiveMessage() 0 12 2
A isExceedsLimit() 0 4 1
A queuedCount() 0 4 1
A dequeuedCount() 0 4 1
A delete() 0 5 1
A __destruct() 0 6 3
1
<?php
2
declare(ticks=1);
3
4
namespace Ackintosh\Snidel;
5
6
abstract class AbstractQueue
7
{
8
    /** @var int */
9
    protected $ownerPid;
10
11
    /** @var \Ackintosh\Snidel\IpcKey */
12
    protected $ipcKey;
13
14
    /** @var \Ackintosh\Snidel\Semaphore */
15
    protected $semaphore;
16
17
    /** @var resource */
18
    protected $id;
19
20
    /** @var array */
21
    protected $stat;
22
23
    /** @var int */
24
    protected $queuedCount = 0;
25
26
    /** @var int */
27
    protected $dequeuedCount = 0;
28
29
    public function __construct(Config $config)
30
    {
31
        $this->ownerPid = $config->get('ownerPid');
32
        $this->ipcKey   = new IpcKey($this->ownerPid, $config->get('id') . str_replace('\\', '_', get_class($this)));
33
        $this->semaphore = new Semaphore();
34
        $this->id       = $this->semaphore->getQueue($this->ipcKey->generate());
35
        $this->stat     = $this->semaphore->statQueue($this->id);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->semaphore->statQueue($this->id) of type * is incompatible with the declared type array of property $stat.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
36
    }
37
38
    /**
39
     * @param   string  $message
40
     */
41
    protected function sendMessage($message)
42
    {
43
        return $this->semaphore->sendMessage($this->id, 1, $message, false);
44
    }
45
46
    /**
47
     * @return  string
48
     * @throws  \RuntimeException
49
     */
50
    protected function receiveMessage()
51
    {
52
        $msgtype = $message = null;
53
54
        // argument #3: specify the maximum number of bytes allowsed in one message queue.
55
        $success = $this->semaphore->receiveMessage($this->id, 1, $msgtype, $this->stat['msg_qbytes'], $message, false);
56
        if (!$success) {
57
            throw new \RuntimeException('failed to receive message.');
58
        }
59
60
        return $message;
61
    }
62
63
    /**
64
     * @param   string  $message
65
     * @return  bool
66
     */
67
    protected function isExceedsLimit($message)
68
    {
69
        return $this->stat['msg_qbytes'] < strlen($message);
70
    }
71
72
    /**
73
     * @return  int
74
     */
75
    public function queuedCount()
76
    {
77
        return $this->queuedCount;
78
    }
79
80
    /**
81
     * @return  int
82
     */
83
    public function dequeuedCount()
84
    {
85
        return $this->dequeuedCount;
86
    }
87
88
    /**
89
     * @return bool
90
     */
91
    public function delete()
92
    {
93
        $this->ipcKey->delete();
94
        return msg_remove_queue($this->id);
95
    }
96
97
    public function __destruct()
98
    {
99
        if (isset($this->ipcKey) && $this->ipcKey->isOwner(getmypid())) {
100
            $this->delete();
101
        }
102
    }// @codeCoverageIgnore
103
}
104