AbstractCollection   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 11
c 2
b 0
f 0
dl 0
loc 47
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 5 1
A get() 0 7 2
A getIterator() 0 4 2
A __construct() 0 3 1
A count() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Polidog\Chatwork\Entity\Collection;
6
7
use Polidog\Chatwork\Entity\EntityInterface;
8
use Polidog\Chatwork\Exception\OutOfBoundsException;
9
10
abstract class AbstractCollection implements \IteratorAggregate, \Countable, CollectionInterface
11
{
12
    protected $entities = [];
13
14
    /**
15
     * @param array $entities
16
     */
17
    public function __construct(array $entities = [])
18
    {
19
        $this->entities = $entities;
20
    }
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function add(EntityInterface $entity)
26
    {
27
        $this->entities[] = $entity;
28
29
        return $this;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function get($idx)
36
    {
37
        if (!array_key_exists($idx, $this->entities)) {
38
            throw new OutOfBoundsException('index not found, index:'.$idx);
39
        }
40
41
        return $this->entities[$idx];
42
    }
43
44
    /**
45
     * @return \ArrayIterator
46
     */
47
    public function getIterator()
48
    {
49
        foreach ($this->entities as $key => $entity) {
50
            yield $key => $entity;
0 ignored issues
show
Bug Best Practice introduced by
The expression yield $key => $entity returns the type Generator which is incompatible with the documented return type ArrayIterator.
Loading history...
51
        }
52
    }
53
54
    public function count()
55
    {
56
        return count($this->entities);
57
    }
58
}
59