Completed
Push — master ( b69bd3...8db10c )
by Karsten
01:49
created

DefaultSolver   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 3
dl 0
loc 86
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B solve() 0 33 6
C solveIntermediateOperationChain() 0 40 7
1
<?php
2
/**
3
 * File was created 06.05.2015 22:56
4
 *
5
 * @author Karsten J. Gerber <[email protected]>
6
 */
7
8
namespace PeekAndPoke\Component\Psi;
9
10
/**
11
 * @api
12
 *
13
 * @author Karsten J. Gerber <[email protected]>
14
 */
15
class DefaultSolver implements Solver
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20 115
    public function solve(\Iterator $operations, \Iterator $items)
21
    {
22
        // search for all UnaryOperations
23 115
        $tempResult = $items;
24 115
        $unaryChain = new \ArrayIterator();
25
26 115
        foreach ($operations as $operation) {
27
28
            // collect all unary operators
29 115
            if ($operation instanceof IntermediateOperation) {
30 86
                $unaryChain->append($operation);
31
            } else {
32
                // execute all the collected unary operations
33
34 34
                if ($unaryChain->count() > 0) {
35 3
                    $tempResult = $this->solveIntermediateOperationChain($unaryChain, $tempResult);
36 3
                    $unaryChain = new \ArrayIterator();
37
                }
38
39
                // execute full set operations (like sort)
40 34
                if ($operation instanceof FullSetOperation) {
41 115
                    $tempResult = $operation->apply($tempResult);
42
                }
43
            }
44
        }
45
46
        // resolve the rest of the unary operation
47 115
        if ($unaryChain->count() > 0) {
48 83
            $tempResult = $this->solveIntermediateOperationChain($unaryChain, $tempResult);
49
        }
50
51 115
        return $tempResult;
52
    }
53
54
    /**
55
     * @param \Iterator $operatorChain
56
     * @param \Iterator $input
57
     *
58
     * @return \ArrayIterator
59
     */
60 86
    private function solveIntermediateOperationChain(\Iterator $operatorChain, \Iterator $input)
61
    {
62 86
        $results = new \ArrayIterator();
63
64 86
        $input->rewind();
65
66 86
        $context              = new Solver\IntermediateContext();
67 86
        $continueWithNextItem = true;
68
69 86
        while ($continueWithNextItem && $input->valid()) {
70
71 74
            $result              = $input->current();
72 74
            $context->outUseItem = true;
73
74
            // do the whole intermediate operation chain for the current input
75 74
            $operatorChain->rewind();
76
77 74
            while ($context->outUseItem && $operatorChain->valid()) {
78
79
                /** @var IntermediateOperation $current */
80 74
                $current = $operatorChain->current();
81
                // apply intermediate operations
82 74
                $result = $current->apply($result, $input->key(), $context);
83
                // track the continuation flags
84 74
                $continueWithNextItem = $continueWithNextItem && $context->outCanContinue;
85
86
                // iterate
87 74
                $operatorChain->next();
88
            }
89
90 74
            if ($context->outUseItem) {
91 74
                $results->offsetSet($input->key(), $result);
92
            }
93
94
            // goto next item
95 74
            $input->next();
96
        }
97
98 86
        return $results;
99
    }
100
}
101