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

TaskQueue   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A enqueue() 0 19 3
A dequeue() 0 18 3
A queuedCount() 0 4 1
A dequeuedCount() 0 4 1
A isClosure() 0 4 2
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 TaskQueue
8
{
9
    const TASK_MAX_SIZE = 1024;
10
11
    private $queuedCount = 0;
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_task_' . 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
    /**
22
     * @param   callable            $callable
23
     * @param   array               $args
24
     * @param   string              $tag
25
     * @return  void
26
     * @throws  RuntimeException
27
     */
28
    public function enqueue($callable, $args = array(), $tag = null)
29
    {
30
        $this->queuedCount++;
31
        if ($this->isClosure($callable)) {
32
            $serializedCallable = new SerializableClosure($callable);
0 ignored issues
show
Documentation introduced by
$callable is of type callable, but the function expects a object<Closure>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
33
        } else {
34
            $serializedCallable = serialize($callable);
35
        }
36
37
        $data = array(
38
            'callable'  => $serializedCallable,
39
            'args'      => $args,
40
            'tag'       => $tag,
41
        );
42
43
        if (!msg_send($this->id, 1, serialize($data))) {
44
            throw new RuntimeException('failed to enqueue task.');
45
        }
46
    }
47
48
    public function dequeue()
49
    {
50
        $this->dequeuedCount++;
51
        $msgtype = $message = null;
52
        $success = msg_receive($this->id, 1, $msgtype, self::TASK_MAX_SIZE, $message);
53
54
        if (!$success) {
55
            throw new \RuntimeException('failed to dequeue task');
56
        }
57
58
        $data = unserialize($message);
59
        if ($this->isClosure($data['callable'])) {
60
            $data['callable'] = $data['callable']->getClosure();
61
        } else {
62
            $data['callable'] = unserialize($data['callable']);
63
        }
64
        return $data;
65
    }
66
67
    public function queuedCount()
68
    {
69
        return $this->queuedCount;
70
    }
71
72
    public function dequeuedCount()
73
    {
74
        return $this->dequeuedCount;
75
    }
76
77
    private function isClosure($callable)
78
    {
79
        return is_object($callable) && is_callable($callable);
80
    }
81
82
    public function __destruct()
83
    {
84
        if ($this->ipcKey->isOwner(getmypid())) {
85
            $this->ipcKey->delete();
86
            return msg_remove_queue($this->id);
87
        }
88
    }// @codeCoverageIgnore
89
}
90