Completed
Push — master ( f9ba0c...336b1b )
by Kamil
23:42
created

AdjustmentsByLabelAggregator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 5
c 1
b 1
f 0
lcom 0
cbo 0
dl 0
loc 33
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A aggregate() 0 15 3
A assertElementIsAdjustment() 0 6 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Component\Order\Aggregator;
13
14
use Sylius\Component\Order\Model\AdjustmentInterface;
15
16
/**
17
 * @author Mateusz Zalewski <[email protected]>
18
 */
19
final class AdjustmentsByLabelAggregator implements AdjustmentsAggregatorInterface
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function aggregate(array $adjustments)
25
    {
26
        $aggregatedAdjustments = array();
27
        foreach ($adjustments as $adjustment) {
28
            $this->assertElementIsAdjustment($adjustment);
29
30
            if (!isset($aggregatedAdjustments[$adjustment->getDescription()])) {
31
                $aggregatedAdjustments[$adjustment->getDescription()] = 0;
32
            }
33
34
            $aggregatedAdjustments[$adjustment->getDescription()] += $adjustment->getAmount();
35
        }
36
37
        return $aggregatedAdjustments;
38
    }
39
40
    /**
41
     * @param mixed $adjustment
42
     *
43
     * @throws \InvalidArgumentException
44
     */
45
    private function assertElementIsAdjustment($adjustment)
46
    {
47
        if (!$adjustment instanceof AdjustmentInterface) {
48
            throw new \InvalidArgumentException('Each adjustments array element must implement ' . AdjustmentInterface::class . '.');
49
        }
50
    }
51
}
52