Completed
Push — master ( b85f9d...1fa11a )
by Akihito
02:52
created

Worker::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
namespace Ackintosh\Snidel;
3
4
use Ackintosh\Snidel\Result\Result;
5
6
class Worker
7
{
8
    /** @var \Ackintosh\Snidel\Task\Task */
9
    private $task;
10
11
    /** @var \Ackintosh\Snidel\Fork\Fork */
12
    private $fork;
13
14
    /** @var \Ackintosh\Snidel\Result\Queue */
15
    private $resultQueue;
16
17
    /**
18
     * @param   \Ackintosh\Snidel\Fork\Fork $fork
19
     * @param   \Ackintosh\Snidel\Task\Task
20
     */
21
    public function __construct($fork, $task)
22
    {
23
        $this->fork = $fork;
24
        $this->task = $task;
25
    }
26
27
    /**
28
     * @param   \Ackintosh\Snidel\Result\Queue
29
     * @return  void
30
     */
31
    public function setResultQueue($queue)
32
    {
33
        $this->resultQueue = $queue;
34
    }
35
36
    /**
37
     * @return  int
38
     */
39
    public function getPid()
40
    {
41
        return $this->fork->getPid();
42
    }
43
44
    /**
45
     * @return  void
46
     * @throws  \RuntimeException
47
     */
48
    public function run()
49
    {
50
        try {
51
            $result = $this->task->execute();
52
        } catch (\RuntimeException $e) {
53
            throw $e;
54
        }
55
56
        $result->setFork($this->fork);
57
58
        try {
59
            $this->resultQueue->enqueue($result);
60
        } catch (\RuntimeException $e) {
61
            throw $e;
62
        }
63
    }
64
65
    /**
66
     * @return  void
67
     * @throws  \RuntimeException
68
     */
69
    public function error()
70
    {
71
        $result = new Result();
72
        $result->setError(error_get_last());
73
        $result->setTask($this->task);
74
        $result->setFork($this->fork);
75
76
        try {
77
            $this->resultQueue->enqueue($result);
78
        } catch (\RuntimeException $e) {
79
            throw $e;
80
        }
81
    }
82
}
83