1 | <?php |
||
17 | class Worker |
||
18 | { |
||
19 | /** @var \Ackintosh\Snidel\Task\Task */ |
||
20 | private $latestTask; |
||
21 | |||
22 | /** @var \Ackintosh\Snidel\Fork\Process */ |
||
23 | private $process; |
||
24 | |||
25 | /** @var \Ackintosh\Snidel\Task\QueueInterface */ |
||
26 | private $taskQueue; |
||
27 | |||
28 | /** @var \Ackintosh\Snidel\Result\QueueInterface */ |
||
29 | private $resultQueue; |
||
30 | |||
31 | /** @var \Ackintosh\Snidel\Pcntl */ |
||
32 | private $pcntl; |
||
33 | |||
34 | /** @var bool */ |
||
35 | private $isInProgress = false; |
||
36 | |||
37 | private $factory; |
||
38 | private $consumer; |
||
39 | private $producer; |
||
40 | |||
41 | /** |
||
42 | * @param \Ackintosh\Snidel\Fork\Process $process |
||
43 | * @param \Bernard\Driver $driver |
||
44 | */ |
||
45 | public function __construct($process, $driver) |
||
46 | { |
||
47 | $this->pcntl = new Pcntl(); |
||
48 | $this->process = $process; |
||
49 | |||
50 | $this->factory = new PersistentFactory($driver, new Serializer()); |
||
51 | $router = new SimpleRouter(); |
||
52 | $router->add('Task', $this); |
||
53 | $this->consumer = new Consumer($router, new EventDispatcher()); |
||
54 | $this->producer = new Producer($this->factory, new EventDispatcher()); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @param \Ackintosh\Snidel\Task\QueueInterface |
||
59 | * @return void |
||
60 | */ |
||
61 | public function setTaskQueue(TaskQueueInterface $queue) |
||
62 | { |
||
63 | $this->taskQueue = $queue; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @param \Ackintosh\Snidel\Result\QueueInterface |
||
68 | * @return void |
||
69 | */ |
||
70 | public function setResultQueue(ResultQueueInterface $queue) |
||
71 | { |
||
72 | $this->resultQueue = $queue; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @return int |
||
77 | */ |
||
78 | public function getPid() |
||
79 | { |
||
80 | return $this->process->getPid(); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @return void |
||
85 | * @throws \RuntimeException |
||
86 | */ |
||
87 | public function run() |
||
88 | { |
||
89 | $this->consumer->consume($this->factory->create('task')); |
||
90 | } |
||
91 | |||
92 | public function task($message) |
||
93 | { |
||
94 | $this->isInProgress = true; |
||
95 | $this->latestTask = $task = TaskFormatter::unserialize($message['task']); |
||
|
|||
96 | $result = $task->execute(); |
||
97 | $result->setProcess($this->process); |
||
98 | |||
99 | $this->producer->produce( |
||
100 | new PlainMessage( |
||
101 | 'Result', |
||
102 | [ |
||
103 | 'result' => ResultFormatter::serialize($result), |
||
104 | ] |
||
105 | ) |
||
106 | ); |
||
107 | $this->isInProgress = false; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @return void |
||
112 | * @throws \RuntimeException |
||
113 | */ |
||
114 | public function error() |
||
134 | |||
135 | /** |
||
136 | * @param int $sig |
||
137 | * @return void |
||
138 | */ |
||
139 | public function terminate($sig) |
||
145 | |||
146 | /** |
||
147 | * @return bool |
||
148 | */ |
||
149 | public function hasTask() |
||
153 | |||
154 | /** |
||
155 | * @return bool |
||
156 | */ |
||
157 | public function isInProgress() |
||
158 | { |
||
159 | return $this->isInProgress; |
||
161 | } |
||
162 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.