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

ChainSearch::getNewCriteria()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 3
crap 2
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