CellCollection::addNamedCell()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace KGzocha\Searcher\Chain\Collection;
5
6
use KGzocha\Searcher\AbstractCollection;
7
use KGzocha\Searcher\Chain\CellInterface;
8
9
/**
10
 * Collection of CellInterface objects. It will throw \InvalidArgumentException if tried to use it with less
11
 * than MINIMUM_CELLS.
12
 *
13
 * @author Krzysztof Gzocha <[email protected]>
14
 */
15
class CellCollection extends AbstractCollection implements CellCollectionInterface
16
{
17
    const MINIMUM_CELLS = 2;
18
19
    /**
20
     * @inheritDoc
21
     */
22 13
    protected function isItemValid($item): bool
23
    {
24 13
        return $item instanceof CellInterface;
25
    }
26
27
    /**
28
     * @inheritdoc
29
     */
30 8
    public function getIterator(): \Iterator
31
    {
32 8
        $this->validateNumberOfCells();
33
34 6
        return parent::getIterator();
35
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40 4
    public function getCells()
41
    {
42 4
        $this->validateNumberOfCells();
43
44 4
        return $this->getItems();
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50 1
    public function getNamedCell(string $name)
51
    {
52 1
        return $this->getNamedItem($name);
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58 1
    public function addCell(CellInterface $item): CellCollectionInterface
59
    {
60 1
        return $this->addItem($item);
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66 2
    public function addNamedCell(string $name, CellInterface $cell): CellCollectionInterface
67
    {
68 2
        return $this->addNamedItem($name, $cell);
69
    }
70
71
    /**
72
     * @throws \InvalidArgumentException
73
     */
74 12
    private function validateNumberOfCells()
75
    {
76 12
        $count = $this->count();
77 12
        if (self::MINIMUM_CELLS <= $count) {
78 10
            return;
79
        }
80
81 2
        throw new \InvalidArgumentException(sprintf(
82 2
            'At last %d cells are required, but there are only %d in collection %s',
83 2
            self::MINIMUM_CELLS,
84 2
            $count,
85 2
            get_class($this)
86
        ));
87
    }
88
}
89