Collector   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 7
dl 0
loc 95
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getGroupKeys() 0 4 1
A setUnique() 0 4 1
A addGroupSet() 0 4 1
C addEntry() 0 27 7
A toArray() 0 4 1
1
<?php
2
3
namespace Dwo\Aggregator\Collector;
4
5
use Dwo\Aggregator\Dumper\DeepDumper;
6
use Dwo\Aggregator\Exception\AggregatorException;
7
use Dwo\Aggregator\Model\EntriesTrait;
8
use Dwo\Aggregator\Model\GroupKey;
9
use Dwo\Aggregator\Model\GroupSet;
10
use Dwo\Aggregator\Model\PreAggregate;
11
use Dwo\Aggregator\Model\PreAggregateGroup;
12
13
/**
14
 * Class Collector
15
 *
16
 * @author Dave Www <[email protected]>
17
 */
18
class Collector implements \Iterator
19
{
20
    use EntriesTrait;
21
22
    /**
23
     * @var array
24
     */
25
    protected $groupKeys;
26
27
    /**
28
     * @var GroupSet[]|null
29
     */
30
    protected $groupSets;
31
32
    /**
33
     * @var bool
34
     */
35
    protected $unique;
36
37
    /**
38
     * @param array $groupKeys
39
     */
40
    public function __construct(array $groupKeys)
41
    {
42
        $this->groupKeys = $groupKeys;
43
        $this->unique = false;
44
    }
45
46
    /**
47
     * @return array
48
     */
49
    public function getGroupKeys()
50
    {
51
        return $this->groupKeys;
52
    }
53
54
    /**
55
     * @param boolean $unique
56
     */
57
    public function setUnique($unique)
58
    {
59
        $this->unique = $unique;
60
    }
61
62
    /**
63
     * @param string   $key
64
     * @param GroupSet $groupSets
65
     */
66
    public function addGroupSet($key, GroupSet $groupSets)
67
    {
68
        $this->groupSets[$key] = $groupSets;
69
    }
70
71
    /**
72
     * :TODO: maybe count key instead of $inc
73
     *
74
     * @param PreAggregate|mixed $entry
75
     * @param int                $inc
76
     */
77
    public function addEntry($entry, $inc = 1)
78
    {
79
        if (!$entry instanceof PreAggregate) {
80
            $entry = new PreAggregate($entry, null, $inc);
81
        }
82
83
        if (null !== $this->groupSets) {
84
            $data = $entry->getData();
85
            foreach ($this->groupSets as $key => $groupSet) {
86
                if (isset($data[$key])) {
87
                    $data[$key] = $groupSet->getValue($data[$key]);
88
                }
89
            }
90
            $entry->setData($data);
91
        }
92
93
        $group = GroupKey::create($entry->getData(), $this->getGroupKeys());
94
        $key = (string) $group;
95
96
        if (!isset($this->entries[$key])) {
97
            $this->entries[$key] = new PreAggregateGroup($group);
98
        } elseif ($this->unique) {
99
            throw new AggregatorException(sprintf('there is already a entry for this group: %s', $key));
100
        }
101
102
        $this->entries[$key]->addEntry($entry);
103
    }
104
105
    /**
106
     * @return array
107
     */
108
    public function toArray()
109
    {
110
        return DeepDumper::toArray($this);
111
    }
112
}