Passed
Branch query-builder (144da5)
by Csaba
02:37
created

Finder   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 6
dl 0
loc 107
ccs 60
cts 60
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setLimit() 0 6 2
A setOffset() 0 6 2
A setOrderBy() 0 6 2
A setSelect() 0 6 2
A setWhere() 0 6 2
B get() 0 25 3
A createClient() 0 5 1
A addWhereGroup() 0 10 1
A parseWhereGroup() 0 9 3
A parseWhere() 0 16 4
1
<?php
2
namespace Fathomminds\Rest\Database\Clusterpoint;
3
4
use Fathomminds\Rest\Exceptions\RestException;
5
use Fathomminds\Rest\Database\Finder as BaseFinder;
6
use Clusterpoint\Client;
7
8
class Finder extends BaseFinder
9
{
10 4
    protected function setLimit($collection)
11
    {
12 4
        if ($this->queryConfiguration->limit !== null) {
13 1
            $collection->limit($this->queryConfiguration->limit);
14
        }
15 4
    }
16
17 4
    protected function setOffset($collection)
18
    {
19 4
        if ($this->queryConfiguration->offset !== null) {
20 1
            $collection->offset($this->queryConfiguration->offset);
21
        }
22 4
    }
23
24 4
    protected function setOrderBy($collection)
25
    {
26 4
        foreach ($this->queryConfiguration->orderBy as $orderBy) {
27 1
            $collection->orderBy(key($orderBy), current($orderBy));
28
        }
29 4
    }
30
31 4
    protected function setSelect($collection)
32
    {
33 4
        if ($this->queryConfiguration->select !== '*') {
34 1
            $collection->select($this->queryConfiguration->select);
35
        }
36 4
    }
37
38 4
    public function setWhere($collection)
39
    {
40 4
        if (!empty($this->queryConfiguration->where)) {
41 3
            $this->parseWhere($collection, $this->queryConfiguration->where, $this->mainLogical);
42
        }
43 4
    }
44
45 5
    public function get()
46
    {
47 5
        if (!($this->client instanceof Client)) {
48 1
            throw new RestException(
49 1
                'Clusterpoint Finder can be used with Clusterpoint Client only',
50 1
                ['type' => get_class($this->client)]
51
            );
52
        }
53 4
        $collection = $this->client->database(
54 4
            $this->queryConfiguration->databaseName.
55 4
            '.'.
56 4
            $this->queryConfiguration->from
57
        );
58 4
        $this->setLimit($collection);
59 4
        $this->setOffset($collection);
60 4
        $this->setOrderBy($collection);
61 4
        $this->setSelect($collection);
62 4
        $this->setWhere($collection);
63 4
        $items = json_decode(json_encode($collection->get()->toArray()));
64 4
        $c = count($items);
65 4
        for ($i=0; $i<$c; $i++) {
66 4
            $this->resultSet[] = $items[$i];
67
        }
68 4
        return $this;
69
    }
70
71 1
    protected function createClient()
72
    {
73 1
        $this->client = new Client;
74 1
        return $this;
75
    }
76
77
    protected function addWhereGroup($collection, $conditions, $logical)
0 ignored issues
show
Unused Code introduced by
The parameter $collection is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
78
    {
79
      /**
80
      * @codeCoverageIgnore
81
      * Passing anonymous function to Clusterpoint API
82
      */
83
        return function ($collection) use ($conditions, $logical) {
84
            $this->parseWhere($collection, $conditions, $logical);
85
        };
86
    }
87
88 3
    protected function parseWhereGroup($logical, $collection, $condition, $nextLogical)
89
    {
90 3
        if ($logical === '||') {
91 1
            $collection->orWhere($this->addWhereGroup($collection, $condition, $nextLogical));
92
        }
93 3
        if ($logical === '&&') {
94 2
            $collection->where($this->addWhereGroup($collection, $condition, $nextLogical));
95
        }
96 3
    }
97
98 3
    private function parseWhere($collection, $conditions, $logical)
99
    {
100 3
        foreach ($conditions as $key => $condition) {
101 3
            switch (strtoupper($key)) {
102 3
                case 'AND':
103 2
                    $this->parseWhereGroup($logical, $collection, $condition, '&&');
104 2
                    break;
105 3
                case 'OR':
106 3
                    $this->parseWhereGroup($logical, $collection, $condition, '||');
107 3
                    break;
108
                default:
109 2
                    list($fieldName, $operator, $value) = $condition;
110 3
                    $collection->where($fieldName, $operator, $value, $logical);
111
            }
112
        }
113 3
    }
114
}
115