Passed
Branch query-builder (5554ca)
by Csaba
03:00
created

Finder::validateConditions()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 3
1
<?php
2
namespace Fathomminds\Rest\Database;
3
4
use Fathomminds\Rest\Contracts\IFinder;
5
use Fathomminds\Rest\Exceptions\RestException;
6
use Fathomminds\Rest\Database\Where;
7
8
abstract class Finder implements IFinder
9
{
10
    protected $databaseName;
11
    protected $select = '*';
12
    protected $from;
13
    protected $where;
14
    protected $orderBy = [];
15
    protected $limit;
16
    protected $offset;
17
    protected $resultSet;
18
    protected $client;
19
    protected $mainLogical = '&&';
20
    protected $validGroups = [
21
        'AND',
22
        'OR',
23
    ];
24
    protected $validOperators = [
25
        '==',
26
        '!=',
27
        '<',
28
        '>',
29
        '<=',
30
        '>=',
31
    ];
32
33
    abstract protected function createClient();
34
    abstract public function get();
35
36 8
    private function validateConditions($conditions)
37
    {
38 8
        foreach ($conditions as $key => $condition) {
39 6
            if (in_array(strtoupper($key), $this->validGroups)) {
40 5
                $this->validateConditions($condition);
41 4
                return;
42
            }
43 6
            $this->validateCondition($condition);
44
        }
45 6
    }
46
47 6
    private function validateCondition($condition)
48
    {
49 6
        if (count($condition) === 3) {
50 5
            $operator = $condition[1];
51 5
            if (!in_array($operator, $this->validOperators)) {
52 1
                throw new RestException('Invalid operator in where condition', [
53 1
                    'operator' => $operator,
54
                ]);
55
            }
56 4
            return;
57
        }
58 1
        throw new RestException('Invalid where condition', [
59 1
            'condition' => $condition,
60
        ]);
61
    }
62
63 25
    public function __construct($client = null)
64
    {
65 25
        if ($client !== null) {
66 24
            $this->client = $client;
67 24
            return $this;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
68
        }
69 2
        $this->createClient();
70 2
    }
71
72 6
    public function database($databaseName)
73
    {
74 6
        $this->databaseName = $databaseName;
75 6
        return $this;
76
    }
77
78 2
    public function select($fieldList)
79
    {
80 2
        $this->select = $fieldList;
81 2
        return $this;
82
    }
83
84 6
    public function from($collectionName)
85
    {
86 6
        $this->from = $collectionName;
87 6
        return $this;
88
    }
89
90 8
    public function where($conditions)
91
    {
92 8
        $this->validateConditions($conditions);
93 6
        $this->where = $conditions;
94 6
        return $this;
95
    }
96
97 2
    public function orderBy($fieldName, $sortMode = 'ASC')
98
    {
99 2
        $this->orderBy[] = [$fieldName => $sortMode];
100 2
        return $this;
101
    }
102
103 2
    public function limit($limit)
104
    {
105 2
        $this->limit = $limit;
106 2
        return $this;
107
    }
108
109 2
    public function offset($offset)
110
    {
111 2
        $this->offset = $offset;
112 2
        return $this;
113
    }
114
115 4
    public function first()
116
    {
117 4
        return isset($this->resultSet[0]) ? $this->resultSet[0] : null;
118
    }
119
120 1
    public function all()
121
    {
122 1
        return $this->resultSet;
123
    }
124
}
125