Passed
Branch query-builder (d1edb7)
by Csaba
02:43
created

Finder   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 23
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 112
ccs 63
cts 63
cp 1
rs 10

11 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
A configQuery() 0 8 1
A get() 0 21 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
    protected 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 4
    protected function configQuery($collection)
46
    {
47 4
        $this->setLimit($collection);
48 4
        $this->setOffset($collection);
49 4
        $this->setOrderBy($collection);
50 4
        $this->setSelect($collection);
51 4
        $this->setWhere($collection);
52 4
    }
53
54 5
    public function get()
55
    {
56 5
        if (!($this->client instanceof Client)) {
57 1
            throw new RestException(
58 1
                'Clusterpoint Finder can be used with Clusterpoint Client only',
59 1
                ['type' => get_class($this->client)]
60
            );
61
        }
62 4
        $collection = $this->client->database(
63 4
            $this->queryConfiguration->databaseName.
64 4
            '.'.
65 4
            $this->queryConfiguration->from
66
        );
67 4
        $this->configQuery($collection);
68 4
        $items = json_decode(json_encode($collection->get()->toArray()));
69 4
        $count = count($items);
70 4
        for ($idx=0; $idx<$count; $idx++) {
71 4
            $this->resultSet[] = $items[$idx];
72
        }
73 4
        return $this;
74
    }
75
76 1
    protected function createClient()
77
    {
78 1
        $this->client = new Client;
79 1
        return $this;
80
    }
81
82
    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...
83
    {
84
      /**
85
      * @codeCoverageIgnore
86
      * Passing anonymous function to Clusterpoint API
87
      */
88
        return function ($collection) use ($conditions, $logical) {
89
            $this->parseWhere($collection, $conditions, $logical);
90
        };
91
    }
92
93 3
    protected function parseWhereGroup($logical, $collection, $condition, $nextLogical)
94
    {
95 3
        if ($logical === '||') {
96 1
            $collection->orWhere($this->addWhereGroup($collection, $condition, $nextLogical));
97
        }
98 3
        if ($logical === '&&') {
99 2
            $collection->where($this->addWhereGroup($collection, $condition, $nextLogical));
100
        }
101 3
    }
102
103 3
    private function parseWhere($collection, $conditions, $logical)
104
    {
105 3
        foreach ($conditions as $key => $condition) {
106 3
            switch (strtoupper($key)) {
107 3
                case 'AND':
108 2
                    $this->parseWhereGroup($logical, $collection, $condition, '&&');
109 2
                    break;
110 3
                case 'OR':
111 3
                    $this->parseWhereGroup($logical, $collection, $condition, '||');
112 3
                    break;
113
                default:
114 2
                    list($fieldName, $operator, $value) = $condition;
115 3
                    $collection->where($fieldName, $operator, $value, $logical);
116
            }
117
        }
118 3
    }
119
}
120