Completed
Push — master ( 94cacb...6d69f5 )
by Akihito
03:32
created

ResultQueue   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 48
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A enqueue() 0 4 1
A dequeue() 0 12 2
A dequeuedCount() 0 4 1
A __destruct() 0 7 2
1
<?php
2
namespace Ackintosh\Snidel;
3
4
use Ackintosh\Snidel\IpcKey;
5
use Opis\Closure\SerializableClosure;
6
7
class ResultQueue
8
{
9
10
    const RESULT_MAX_SIZE = 5120;
11
12
    private $dequeuedCount = 0;
13
14
    public function __construct($ownerPid)
15
    {
16
        $this->ownerPid = $ownerPid;
0 ignored issues
show
Bug introduced by
The property ownerPid does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
17
        $this->ipcKey = new IpcKey($ownerPid, 'snidel_result_' . uniqid((string) mt_rand(1, 100), true));
0 ignored issues
show
Bug introduced by
The property ipcKey does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
18
        $this->id = msg_get_queue($this->ipcKey->generate());
0 ignored issues
show
Bug introduced by
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
19
    }
20
21
    public function enqueue($result)
22
    {
23
        return msg_send($this->id, 1, serialize($result));
24
    }
25
26
    public function dequeue()
27
    {
28
        $this->dequeuedCount++;
29
        $msgtype = $message = null;
30
        $success = msg_receive($this->id, 1, $msgtype, self::RESULT_MAX_SIZE, $message);
31
32
        if (!$success) {
33
            throw new \RuntimeException('failed to dequeue result');
34
        }
35
36
        return unserialize($message);
37
    }
38
39
    /**
40
     * @return int
41
     */
42
    public function dequeuedCount()
43
    {
44
        return $this->dequeuedCount;
45
    }
46
47
    public function __destruct()
48
    {
49
        if ($this->ipcKey->isOwner(getmypid())) {
50
            $this->ipcKey->delete();
51
            return msg_remove_queue($this->id);
52
        }
53
    }// @codeCoverageIgnore
54
}
55