Completed
Branch master (0e0537)
by Krzysztof
02:41
created

ChainSearch   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 75
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B search() 0 25 3
A getNewCriteria() 0 13 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