FilterHandler::checkValue()   D
last analyzed

Complexity

Conditions 10
Paths 10

Size

Total Lines 27
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 32.5531

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 9
cts 23
cp 0.3913
rs 4.8196
c 0
b 0
f 0
nc 10
cc 10
eloc 23
nop 3
crap 32.5531

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Nayjest\Querying\Handler\PostProcess;
4
5
use InvalidArgumentException;
6
use Nayjest\Querying\Handler\AbstractHandler;
7
use Nayjest\Querying\Handler\Priority;
8
use Nayjest\Querying\Operation\FilterOperation;
9
use Nayjest\Querying\Row\RowInterface;
10
11
/**
12
 * Class FilterHandler
13
 */
14
class FilterHandler extends AbstractHandler
15
{
16
    const PRIORITY = Priority::POST_PROCESS;
17
18 1
    public function getPriority()
19
    {
20 1
        return self::PRIORITY;
21
    }
22
    /**
23
     * Applies operation to source and returns modified source.
24
     *
25
     */
26 1
    public function apply($dataSource)
27
    {
28
        /** @var FilterOperation $operation */
29 1
        $operation = $this->operation;
30 1
        foreach ($dataSource as $row) {
31
            /** @var RowInterface $row */
32 1
            $testedValue = $row->get($operation->getField());
33 1
            $argument = $operation->getValue();
34 1
            $operator = $operation->getOperator();
35 1
            if ($this->checkValue($testedValue, $operator, $argument)) {
36 1
                yield $row;
37 1
            }
38 1
        }
39 1
    }
40
41
    /**
42
     * @param $testedValue
43
     * @param string $operator
44
     * @param $argument
45
     * @return bool
46
     * @throws InvalidArgumentException
47
     *
48
     */
49 1
    protected function checkValue($testedValue, $operator, $argument)
50
    {
51
        switch ($operator) {
52 1
            case FilterOperation::OPERATOR_EQ:
53
                return $testedValue == $argument;
54 1
            case FilterOperation::OPERATOR_GT:
55
                return $testedValue > $argument;
56 1
            case FilterOperation::OPERATOR_GTE:
57
                return $testedValue >= $argument;
58 1
            case FilterOperation::OPERATOR_LT:
59
                return $testedValue < $argument;
60 1
            case FilterOperation::OPERATOR_LTE:
61 1
                return $testedValue <= $argument;
62 1
            case FilterOperation::OPERATOR_NOT_EQ:
63 1
                return $testedValue != $argument;
64
            case FilterOperation::OPERATOR_STR_CONTAINS:
65
                return strpos($testedValue, $argument) !== false;
66
            case FilterOperation::OPERATOR_STR_STARTS_WITH:
67
                return strpos($testedValue, $argument) === 0;
68
            case FilterOperation::OPERATOR_STR_ENDS_WITH:
69
                return strpos($testedValue, $argument, strlen($testedValue) - strlen($argument)) !== false;
70
            default:
71
                throw new InvalidArgumentException(
72
                    'Unsupported operator ' . $operator
73
                );
74
        }
75
    }
76
}
77