Completed
Push — abstract-collection ( 5b4c23...bd6f66 )
by Krzysztof
02:40
created

CellCollection::isItemValid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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