Completed
Push — master ( 6391fe...b20865 )
by Akihito
02:23
created

Queue::enqueue()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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