ChainSearch::getNewCriteria()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

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