Passed
Pull Request — master (#20)
by
unknown
03:43
created

Group   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 89.47%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
eloc 28
dl 0
loc 82
ccs 34
cts 38
cp 0.8947
rs 10
c 5
b 1
f 0
wmc 17

12 Methods

Rating   Name   Duplication   Size   Complexity  
A count() 0 3 1
A first() 0 3 1
A addAggregator() 0 4 1
A max() 0 3 1
A min() 0 3 1
A __invoke() 0 20 6
A addToSet() 0 3 1
A __construct() 0 3 1
A last() 0 3 1
A sum() 0 3 1
A by() 0 3 1
A avg() 0 3 1
1
<?php
2
3
namespace WS\Utils\Collections\Functions\Group;
4
5
use WS\Utils\Collections\Collection;
6
use WS\Utils\Collections\Functions\ObjectFunctions;
7
8
class Group
9
{
10
11
    private $key;
12
    private $aggregators;
13
14 7
    public function __construct(string $key)
15
    {
16 7
        $this->key = $key;
17 7
    }
18
19 7
    public function __invoke(Collection $collection)
20
    {
21 7
        $groupedResult = [];
22 7
        foreach ($collection as $element) {
23 7
            if (!$groupKey = ObjectFunctions::getPropertyValue($element, $this->key)) {
24 1
                continue;
25
            }
26 7
            $groupedResult[$groupKey][] = $element;
27
        }
28 7
        if (!$this->aggregators) {
29 5
            return $groupedResult;
30
        }
31 2
        $aggregatedResult = [];
32 2
        foreach ($groupedResult as $groupKey => $items) {
33 2
            foreach ($this->aggregators as $item) {
34 2
                [$destKey, $aggregator] = $item;
35 2
                $aggregatedResult[$groupKey][$destKey] = $aggregator($items);
36
            }
37
        }
38 2
        return $aggregatedResult;
39
    }
40
41 7
    public static function by(string $key): self
42
    {
43 7
        return new self($key);
44
    }
45
46 2
    public function sum(string $sourceKey, string $destKey = null): self
47
    {
48 2
        return $this->addAggregator($destKey ?? $sourceKey, new Aggregator\Sum($sourceKey));
49
    }
50
51 2
    public function min(string $sourceKey, string $destKey = null): self
52
    {
53 2
        return $this->addAggregator($destKey ?? $sourceKey, new Aggregator\Min($sourceKey));
54
    }
55
56 2
    public function max(string $sourceKey, string $destKey = null): self
57
    {
58 2
        return $this->addAggregator($destKey ?? $sourceKey, new Aggregator\Max($sourceKey));
59
    }
60
61 2
    public function avg(string $sourceKey, string $destKey = null): self
62
    {
63 2
        return $this->addAggregator($destKey ?? $sourceKey, new Aggregator\Avg($sourceKey));
64
    }
65
66
    public function addToSet(string $sourceKey, string $destKey = null): self
67
    {
68
        return $this->addAggregator($destKey ?? $sourceKey, new Aggregator\AddToSet($sourceKey));
69
    }
70
71 2
    public function first(string $sourceKey, string $destKey = null): self
72
    {
73 2
        return $this->addAggregator($destKey ?? $sourceKey, new Aggregator\First($sourceKey));
74
    }
75
76 2
    public function last(string $sourceKey, string $destKey = null): self
77
    {
78 2
        return $this->addAggregator($destKey ?? $sourceKey, new Aggregator\Last($sourceKey));
79
    }
80
81
    public function count(string $destKey): self
82
    {
83
        return $this->addAggregator($destKey, new Aggregator\Count());
84
    }
85
86 2
    public function addAggregator(string $destKey, callable $aggregator): self
87
    {
88 2
        $this->aggregators[] = [$destKey, $aggregator];
89 2
        return $this;
90
    }
91
92
}
93