Completed
Push — master-4.0 ( 71618b...2eaeb5 )
by Krzysztof
03:06 queued 03:01
created

ChainSearch::validateCells()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 19
ccs 11
cts 11
cp 1
rs 8.8571
cc 5
eloc 10
nc 4
nop 1
crap 5
1
<?php
2
3
namespace KGzocha\Searcher\Chain;
4
5
use KGzocha\Searcher\Chain\Collection\CellCollectionInterface;
6
use KGzocha\Searcher\Criteria\Collection\CriteriaCollectionInterface;
7
use KGzocha\Searcher\Result\ResultCollection;
8
use KGzocha\Searcher\SearcherInterface;
9
10
/**
11
 * This class represents whole chain of searchers, but it behaves like regular searcher.
12
 * It will perform all (not skipped) sub-searches, transform theirs results into CriteriaCollection
13
 * and pass those criteria to next sub-search.
14
 * At the end it will return collection of all the results from all the steps.
15
 *
16
 * @author Krzysztof Gzocha <[email protected]>
17
 */
18
class ChainSearch implements SearcherInterface
19
{
20
    /**
21
     * @var CellCollectionInterface
22
     */
23
    private $cellCollection;
24
25
    /**
26
     * @param CellCollectionInterface $cellCollection
27
     */
28 8
    public function __construct(CellCollectionInterface $cellCollection)
29
    {
30 8
        $this->cellCollection = $cellCollection;
31 8
    }
32
33
    /**
34
     * Will perform multiple sub-searches.
35
     * Results from first search will be transformed and passed as CriteriaCollection
36
     * to another sub-search.
37
     * Whole process will return collection of results from each sub-search.
38
     *
39
     * @param CriteriaCollectionInterface $criteriaCollection
40
     *
41
     * @return ResultCollection
42
     */
43 8
    public function search(
44
        CriteriaCollectionInterface $criteriaCollection
45
    ) {
46 8
        $previousCriteria = $criteriaCollection;
47 8
        $previousResults = null;
48 8
        $result = new ResultCollection();
49
50
        /** @var CellInterface $cell */
51 8
        foreach ($this->cellCollection as $name => $cell) {
52 6
            if ($cell->getTransformer()->skip($previousResults)) {
53 2
                continue;
54
            }
55
56 6
            $previousResults = $cell->getSearcher()->search($previousCriteria);
57 6
            $previousCriteria = $this->getNewCriteria(
58
                $cell,
59
                $previousCriteria,
60
                $previousResults
61
            );
62
63 6
            $result->addNamedItem($name, $previousResults);
64
        }
65
66 6
        return $result;
67
    }
68
69
    /**
70
     * If $cell has transformer then it will be used to return new criteria.
71
     * If not old criteria will be returned.
72
     *
73
     * @param CellInterface               $cell
74
     * @param CriteriaCollectionInterface $criteria
75
     * @param mixed                       $results
76
     *
77
     * @return CriteriaCollectionInterface
78
     */
79 6
    private function getNewCriteria(
80
        CellInterface $cell,
81
        CriteriaCollectionInterface $criteria,
82
        $results
83
    ) {
84 6
        if (!$cell->hasTransformer()) {
85 4
            return $criteria;
86
        }
87
88
        return $cell
89 6
            ->getTransformer()
90 6
            ->transform($results, $criteria);
91
    }
92
}
93