Completed
Push — master ( 85994b...9d10cb )
by Dmitry
02:11
created

Specification::applyWhereTo()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
ccs 0
cts 14
cp 0
rs 8.6737
cc 6
eloc 11
nc 6
nop 1
crap 42
1
<?php
2
3
namespace hiapi\query;
4
5
use hiapi\validators\FieldValidationException;
6
use hiapi\validators\FieldValidator;
7
use hiapi\validators\FieldValidatorFactory;
8
use yii\base\InvalidParamException;
9
use yii\db\QueryTrait;
10
11
class Specification
12
{
13
    use QueryTrait;
14
15
    public $requestedRelations = [];
16
17
    /**
18
     * @var FieldValidatorFactory
19
     */
20
    private $fieldValidatorFactory;
21
22
    function __construct(FieldValidatorFactory $fieldValidatorFactory)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
23
    {
24
        $this->fieldValidatorFactory = $fieldValidatorFactory;
25
    }
26
27
    public function requestRelation($name)
28
    {
29
        $this->requestedRelations[$name] = true; // todo: specification for relation
30
    }
31
32
    /**
33
     * @param Query $query
34
     * @return Query
35
     */
36
    public function applyTo($query)
37
    {
38
        if ($this->where) {
39
            $this->applyWhereTo($query);
40
        }
41
42
        if ($this->limit) {
43
            $query->limit($this->limit);
44
        }
45
46
        return $query;
47
    }
48
49
    /**
50
     * @param Query $query
51
     * @throws FieldValidationException
52
     */
53
    public function applyWhereTo($query)
54
    {
55
        $conditions = [];
56
57
        foreach ($this->where as $key => $condition) {
0 ignored issues
show
Bug introduced by
The expression $this->where of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
58
            foreach ($query->getFields() as $field) {
59
                if (is_array($condition)) {
60
                    throw new InvalidParamException('Condition ' . json_encode($condition) . ' is not supported yet.');
61
                }
62
63
                if ($field->nameEquals($key)) {
64
                    if (!$this->fieldValidatorFactory->createFor($field, 'eq')->validate($condition)) {
65
                        throw new FieldValidationException('Value ' . $condition . ' is not valid');
66
                    }
67
68
                    $conditions[$field->getSql()] = $condition;
69
                }
70
            }
71
        }
72
73
        $query->andWhere($conditions);
74
    }
75
}
76