Collection   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 0
loc 110
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 14 2
A getData() 0 10 2
A addEntity() 0 6 1
A getIterator() 0 8 2
A count() 0 4 1
A setTotal() 0 6 1
A getTotal() 0 4 2
1
<?php
2
3
namespace Percy\Entity;
4
5
use Countable;
6
use IteratorAggregate;
7
8
class Collection implements IteratorAggregate, Countable
9
{
10
    /**
11
     * @var \Percy\Entity\EntityInterface[]
12
     */
13
    protected $entities = [];
14
15
    /**
16
     * @var integer
17
     */
18
    protected $total = 0;
19
20
    /**
21
     * Return api representation of collection.
22
     *
23
     * @param array $scopes
24
     *
25
     * @return array
26
     */
27 2
    public function toArray(array $scopes = [])
28
    {
29
        $collection = [
30 2
            'count'     => count($this),
31 2
            'total'     => $this->getTotal(),
32 2
            '_embedded' => []
33 2
        ];
34
35 2
        foreach ($this->getIterator() as $entity) {
36 2
            $collection['_embedded'][$entity->getDataSource()][] = $entity->toArray($scopes);
37 1
        }
38
39 1
        return $collection;
40
    }
41
42
    /**
43
     * Return raw array representation of data.
44
     *
45
     * @param array $scopes
46
     *
47
     * @return array
48
     */
49 1
    public function getData(array $scopes = [])
50
    {
51 1
        $data = [];
52
53 1
        foreach ($this->getIterator() as $entity) {
54 1
            $data[] = $entity->getData($scopes);
55 1
        }
56
57 1
        return $data;
58
    }
59
60
    /**
61
     * Adds an entity to the collection.
62
     *
63
     * @param \Percy\Entity\EntityInterface $entity
64
     *
65
     * @return self
66
     */
67 5
    public function addEntity(EntityInterface $entity)
68
    {
69 5
        $this->entities[] = $entity;
70
71 5
        return $this;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 6
    public function getIterator()
78
    {
79 6
        $count = count($this);
80
81 6
        for ($i = 0; $i < $count; $i++) {
82 5
            yield $this->entities[$i];
83 4
        }
84 5
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 6
    public function count()
90
    {
91 6
        return count($this->entities);
92
    }
93
94
    /**
95
     * Set the total number of results relating to the collection.
96
     *
97
     * @param integer $total
98
     *
99
     * @return self
100
     */
101 4
    public function setTotal($total)
102
    {
103 4
        $this->total = $total;
104
105 4
        return $this;
106
    }
107
108
    /**
109
     * Get the total number of results relating to the collection.
110
     *
111
     * @return integer
112
     */
113 5
    public function getTotal()
114
    {
115 5
        return ($this->total === 0) ? count($this) : $this->total;
116
    }
117
}
118