Completed
Push — abstract-collection ( 5120af...458de3 )
by Krzysztof
04:53
created

ChainSearch::validateCells()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.2
cc 4
eloc 10
nc 4
nop 1
crap 4
1
<?php
2
3
namespace KGzocha\Searcher\Chain;
4
5
use KGzocha\Searcher\Criteria\Collection\CriteriaCollectionInterface;
6
use KGzocha\Searcher\Result\ResultCollection;
7
use KGzocha\Searcher\SearcherInterface;
8
9
/**
10
 * This class represents whole chain of searchers, but it behaves like regular searcher.
11
 * It will perform all (not skipped) sub-searches, transform theirs results into CriteriaCollection
12
 * and pass those criteria to next sub-search.
13
 * At the end it will return collection of all the results from all the steps.
14
 *
15
 * @author Krzysztof Gzocha <[email protected]>
16
 */
17
class ChainSearch implements SearcherInterface
18
{
19
    const MINIMUM_CELLS = 2;
20
21
    /**
22
     * @var CellInterface[]
23
     */
24
    private $cells;
25
26
    /**
27
     * @param CellInterface[] $cells
28
     */
29 9
    public function __construct(array $cells)
30
    {
31 9
        $this->validateCells($cells);
32 6
        $this->cells = $cells;
33 6
    }
34
35
    /**
36
     * Will perform multiple sub-searches.
37
     * Results from first search will be transformed and passed as CriteriaCollection
38
     * to another sub-search.
39
     * Whole process will return collection of results from each sub-search.
40
     *
41
     * @param CriteriaCollectionInterface $criteriaCollection
42
     *
43
     * @return ResultCollection
44
     */
45 4
    public function search(
46
        CriteriaCollectionInterface $criteriaCollection
47
    ) {
48 4
        $previousCriteria = $criteriaCollection;
49 4
        $previousResults = null;
50 4
        $result = new ResultCollection();
51
52 4
        foreach ($this->cells as $cell) {
53 4
            if ($cell->getTransformer()->skip($previousResults)) {
54 2
                continue;
55
            }
56
57 4
            $previousResults = $cell->getSearcher()->search($previousCriteria);
58 4
            if ($cell->hasTransformer()) {
59
                $previousCriteria = $cell
60 4
                    ->getTransformer()
61 4
                    ->transform($previousResults, $previousCriteria);
62
            }
63
64 4
            $this->addResult($result, $cell, $previousResults);
65
        }
66
67 4
        return $result;
68
    }
69
70
    /**
71
     * @param CellInterface[] $cells
72
     *
73
     * @throws \InvalidArgumentException
74
     */
75 9
    private function validateCells(array $cells)
76
    {
77 9
        if (self::MINIMUM_CELLS > count($cells)) {
78 2
            throw new \InvalidArgumentException(
79 2
                'At least two searchers are required to create a chain'
80
            );
81
        }
82
83 7
        foreach ($cells as $cell) {
84 7
            if ($cell instanceof CellInterface) {
85 7
                continue;
86
            }
87
88 1
            throw new \InvalidArgumentException(sprintf(
89 1
                'All cells passed to %s should be object and must implement CellInterface',
90
                get_class($this)
91
            ));
92
        }
93 6
    }
94
95
    /**
96
     * @param ResultCollection $result
97
     * @param CellInterface    $cell
98
     * @param mixed            $previousResults
99
     *
100
     * @return ResultCollection
101
     */
102 4
    private function addResult(
103
        ResultCollection $result,
104
        CellInterface $cell,
105
        $previousResults
106
    ) {
107 4
        if ($cell->getName()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $cell->getName() of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
108 3
            return $result->addNamedItem($cell->getName(), $previousResults);
109
        }
110
111 1
        return $result->addItem($previousResults);
112
    }
113
}
114