Completed
Push — master ( 3e1af8...d54861 )
by Akihito
02:18
created

TaskQueue::isClosure()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
namespace Ackintosh\Snidel;
3
4
use Ackintosh\Snidel\IpcKey;
5
6
class TaskQueue
7
{
8
    const TASK_MAX_SIZE = 1024;
9
10
    private $queuedCount = 0;
11
    private $dequeuedCount = 0;
12
13
    public function __construct($ownerPid)
14
    {
15
        $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...
16
        $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...
17
        $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...
18
    }
19
20
    /**
21
     * @param   \Ackintosh\Snidel\Task  $task
22
     * @return  void
23
     * @throws  RuntimeException
24
     */
25
    public function enqueue($task)
26
    {
27
        $this->queuedCount++;
28
29
        if (!msg_send($this->id, 1, $task->serialize())) {
30
            throw new RuntimeException('failed to enqueue task.');
31
        }
32
    }
33
34
    /**
35
     * @return  \Ackintosh\Snidel\Task
36
     * @throws  \RuntimeException
37
     */
38 View Code Duplication
    public function dequeue()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
    {
40
        $this->dequeuedCount++;
41
        $msgtype = $message = null;
42
        $success = msg_receive($this->id, 1, $msgtype, self::TASK_MAX_SIZE, $message);
43
44
        if (!$success) {
45
            throw new \RuntimeException('failed to dequeue task');
46
        }
47
48
        $task = unserialize($message);
49
        return $task->unserialize();
50
    }
51
52
    public function queuedCount()
53
    {
54
        return $this->queuedCount;
55
    }
56
57
    public function dequeuedCount()
58
    {
59
        return $this->dequeuedCount;
60
    }
61
62
    public function __destruct()
63
    {
64
        if ($this->ipcKey->isOwner(getmypid())) {
65
            $this->ipcKey->delete();
66
            return msg_remove_queue($this->id);
67
        }
68
    }// @codeCoverageIgnore
69
}
70