DrinkAggregator::doReply()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 4 Features 0
Metric Value
c 4
b 4
f 0
dl 0
loc 15
rs 9.2
cc 4
eloc 11
nc 4
nop 1
1
<?php
2
3
use PEIP\INF\Channel\Channel;
4
use PEIP\INF\Message\Message;
5
6
class DrinkAggregator extends \PEIP\Pipe\Pipe
7
{
8
    protected $orders = [];
9
    protected $preparedDrinks = [];
10
11 View Code Duplication
    public function __construct(Channel $inputChannel, Channel $outputChannel = null)
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...
12
    {
13
        $this->setInputChannel($inputChannel);
14
        if (is_object($outputChannel)) {
15
            $this->setOutputChannel($outputChannel);
16
        }
17
        $this->registerCommand('ADD_ORDER', [$this, 'receiveOrder']);
18
    }
19
20
    protected function doReply(Message $message)
21
    {
22
        $drink = $message->getContent();
23
        $nr = $drink->getOrderNumber();
24
        if (!isset($this->preparedDrinks[$nr])) {
25
            $this->preparedDrinks[$nr] = [];
26
        }
27
        $this->preparedDrinks[$drink->getOrderNumber()][] = $drink;
28
        if (isset($this->orders[$nr])
29
            && $this->orders[$nr]->getTotalCount() == count($this->preparedDrinks[$nr])) {
30
            $this->replyMessage($this->preparedDrinks[$nr]);
31
            unset($this->preparedDrinks[$nr]);
32
            echo PEIP_LINE_SEPARATOR.'DrinkAggregator : reply #'.$nr;
33
        }
34
    }
35
36
    public function receiveOrder(Order $order)
37
    {
38
        echo PEIP_LINE_SEPARATOR.'DrinkAggregator: received Order';
39
        $this->orders[$order->getOrderNumber()] = $order;
40
    }
41
}
42