Finder::validateConditions()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
ccs 0
cts 7
cp 0
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 12
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 $queryConfiguration;
11
    protected $resultSet;
12
    protected $client;
13
    protected $mainLogical = '&&';
14
    protected $validGroups = [
15
        'AND',
16
        'OR',
17
    ];
18
    protected $validOperators = [
19
        '==',
20
        '!=',
21
        '<',
22
        '>',
23
        '<=',
24
        '>=',
25
    ];
26
27
    abstract protected function createClient();
28
    abstract public function get();
29
30
    private function validateConditions($conditions)
31
    {
32
        foreach ($conditions as $key => $condition) {
33
            if (in_array(strtoupper($key), $this->validGroups)) {
34
                $this->validateConditions($condition);
35
                return;
36
            }
37
            $this->validateCondition($condition);
38
        }
39
    }
40
41
    private function validateCondition($condition)
42
    {
43
        if (count($condition) === 3) {
44
            $operator = $condition[1];
45
            if (!in_array($operator, $this->validOperators)) {
46
                throw new RestException('Invalid operator in where condition', [
47
                    'operator' => $operator,
48
                ]);
49
            }
50
            return;
51
        }
52
        throw new RestException('Invalid where condition', [
53
            'condition' => $condition,
54
        ]);
55
    }
56
57
    public function __construct($client = null)
58
    {
59
        $this->queryConfiguration = new FinderQueryConfiguration;
60
        if ($client !== null) {
61
            $this->client = $client;
62
            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...
63
        }
64
        $this->createClient();
65
    }
66
67
    public function database($databaseName)
68
    {
69
        $this->queryConfiguration->databaseName = $databaseName;
70
        return $this;
71
    }
72
73
    public function select($fieldList)
74
    {
75
        $this->queryConfiguration->select = $fieldList;
76
        return $this;
77
    }
78
79
    public function from($collectionName)
80
    {
81
        $this->queryConfiguration->from = $collectionName;
82
        return $this;
83
    }
84
85
    public function where($conditions)
86
    {
87
        $this->validateConditions($conditions);
88
        $this->queryConfiguration->where = $conditions;
89
        return $this;
90
    }
91
92
    public function orderBy($fieldName, $sortMode = 'ASC')
93
    {
94
        $this->queryConfiguration->orderBy[] = [$fieldName => $sortMode];
95
        return $this;
96
    }
97
98
    public function limit($limit)
99
    {
100
        $this->queryConfiguration->limit = $limit;
101
        return $this;
102
    }
103
104
    public function offset($offset)
105
    {
106
        $this->queryConfiguration->offset = $offset;
107
        return $this;
108
    }
109
110
    public function first()
111
    {
112
        return isset($this->resultSet[0]) ? $this->resultSet[0] : null;
113
    }
114
115
    public function all()
116
    {
117
        return $this->resultSet;
118
    }
119
}
120