Completed
Push — master ( 922e8a...cfbc00 )
by Akihito
02:15
created

Queue::enqueue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
namespace Ackintosh\Snidel\Task;
3
4
use Ackintosh\Snidel\AbstractQueue;
5
use Ackintosh\Snidel\Task\Formatter;
6
7
class Queue extends AbstractQueue
8
{
9
    /**
10
     * @param   \Ackintosh\Snidel\Task  $task
11
     * @return  void
12
     * @throws  RuntimeException
13
     */
14
    public function enqueue($task)
15
    {
16
        $this->queuedCount++;
17
18
        $serialized = Formatter::serialize($task);
19
        if ($this->isExceedsLimit($serialized)) {
20
            throw new \RuntimeException('the task exceeds the message queue limit.');
21
        }
22
23
        if (!$this->sendMessage($serialized)) {
24
            throw new \RuntimeException('failed to enqueue task.');
25
        }
26
    }
27
28
    /**
29
     * @return  \Ackintosh\Snidel\Task
30
     * @throws  \RuntimeException
31
     */
32
    public function dequeue()
33
    {
34
        $this->dequeuedCount++;
35
        try {
36
            $serializedTask = $this->receiveMessage();
37
        } catch (\RuntimeException $e) {
38
            throw new \RuntimeException('failed to dequeue task');
39
        }
40
41
        return Formatter::unserialize($serializedTask);
42
    }
43
}
44