Completed
Pull Request — master (#3)
by
unknown
03:50
created

Query::setLimit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * Tools to use API as ActiveRecord for Yii2
5
 *
6
 * @link      https://github.com/hiqdev/yii2-hiart
7
 * @package   yii2-hiart
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\hiart;
13
14
use Yii;
15
use yii\base\Component;
16
use yii\db\QueryInterface;
17
use yii\db\QueryTrait;
18
19
class Query extends Component implements QueryInterface
20
{
21
    use QueryTrait;
22
23
    public $index;
24
25
    public $type;
26
27
    public $select;
28
    public $join;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function init()
34
    {
35
        parent::init();
36
        // setting the default limit according to api defaults
37
        if ($this->limit === null) {
38
            $this->limit = 'ALL';
0 ignored issues
show
Documentation Bug introduced by
The property $limit was declared of type integer, but 'ALL' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
39
        }
40
    }
41
42
    public function createCommand($db = null)
43
    {
44
        if ($db === null) {
45
            $db = Yii::$app->get('hiresource');
46
        }
47
48
        $commandConfig = $db->getQueryBuilder()->build($this);
49
50
        return $db->createCommand($commandConfig);
51
    }
52
53
    public function join($type)
54
    {
55
        $this->join[] = $type;
56
57
        return $this;
58
    }
59
60
    public function all($db = null)
61
    {
62
        $result = $this->createCommand($db)->search();
63
        if (empty($result['hits']['hits'])) {
64
            return [];
65
        }
66
        $rows = $result['hits']['hits'];
67
        if ($this->indexBy === null) {
68
            return $rows;
69
        }
70
        $models = [];
71
        foreach ($rows as $key => $row) {
72 View Code Duplication
            if ($this->indexBy !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
                if (is_string($this->indexBy)) {
74
                    $key = isset($row['fields'][$this->indexBy]) ? reset($row['fields'][$this->indexBy]) : $row['_source'][$this->indexBy];
75
                } else {
76
                    $key = call_user_func($this->indexBy, $row);
77
                }
78
            }
79
            $models[$key] = $row;
80
        }
81
82
        return $models;
83
    }
84
85
    public function one($db = null)
86
    {
87
        $result = $this->createCommand($db)->search(['limit' => 1]);
88
        if (empty($result)) {
89
            return false;
90
        }
91
        $record = reset($result);
92
93
        return $record;
94
    }
95
96
    public function search($db = null, $options = [])
97
    {
98
        $result = $this->createCommand($db)->search($options);
99
        if (!empty($result) && $this->indexBy !== null) {
100
            $rows = [];
101
            foreach ($result as $key => $row) {
102 View Code Duplication
                if (is_string($this->indexBy)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
                    $key = isset($row['fields'][$this->indexBy]) ? $row['fields'][$this->indexBy] : $row['_source'][$this->indexBy];
104
                } else {
105
                    $key = call_user_func($this->indexBy, $row);
106
                }
107
                $rows[$key] = $row;
108
            }
109
            $result = $rows;
110
        }
111
112
        return $result;
113
    }
114
115
    public function delete($db = null, $options = [])
116
    {
117
        return $this->createCommand($db)->deleteByQuery($options);
118
    }
119
120
    public function scalar($field, $db = null)
121
    {
122
        $record = self::one($db);
123
        if ($record !== false) {
124
            if ($field === '_id') {
125
                return $record['_id'];
126
            } elseif (isset($record['_source'][$field])) {
127
                return $record['_source'][$field];
128
            } elseif (isset($record['fields'][$field])) {
129
                return count($record['fields'][$field]) === 1 ? reset($record['fields'][$field]) : $record['fields'][$field];
130
            }
131
        }
132
133
        return;
134
    }
135
136
    public function column($field, $db = null)
137
    {
138
        $command                        = $this->createCommand($db);
139
        $command->queryParts['_source'] = [$field];
140
        $result                         = $command->search();
141
        if (empty($result['hits']['hits'])) {
142
            return [];
143
        }
144
        $column = [];
145
        foreach ($result['hits']['hits'] as $row) {
146
            if (isset($row['fields'][$field])) {
147
                $column[] = $row['fields'][$field];
148
            } elseif (isset($row['_source'][$field])) {
149
                $column[] = $row['_source'][$field];
150
            } else {
151
                $column[] = null;
152
            }
153
        }
154
155
        return $column;
156
    }
157
158
    public function count($q = '*', $db = null)
159
    {
160
        $options          = [];
161
        $options['count'] = 1;
162
163
        return $this->createCommand($db)->search($options);
164
    }
165
166
    public function exists($db = null)
167
    {
168
        return self::one($db) !== false;
169
    }
170
171
    public function stats($groups)
172
    {
173
        $this->stats = $groups;
0 ignored issues
show
Documentation introduced by
The property stats does not exist on object<hiqdev\hiart\Query>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
174
175
        return $this;
176
    }
177
178
    public function highlight($highlight)
179
    {
180
        $this->highlight = $highlight;
0 ignored issues
show
Documentation introduced by
The property highlight does not exist on object<hiqdev\hiart\Query>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
181
182
        return $this;
183
    }
184
185
    public function addAggregation($name, $type, $options)
186
    {
187
        $this->aggregations[$name] = [$type => $options];
0 ignored issues
show
Documentation introduced by
The property aggregations does not exist on object<hiqdev\hiart\Query>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
188
189
        return $this;
190
    }
191
192
    public function addAgg($name, $type, $options)
193
    {
194
        return $this->addAggregation($name, $type, $options);
195
    }
196
197
    public function addSuggester($name, $definition)
198
    {
199
        $this->suggest[$name] = $definition;
0 ignored issues
show
Documentation introduced by
The property suggest does not exist on object<hiqdev\hiart\Query>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
200
201
        return $this;
202
    }
203
204
    public function query($query)
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
205
    {
206
        $this->query = $query;
0 ignored issues
show
Documentation introduced by
The property query does not exist on object<hiqdev\hiart\Query>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
207
208
        return $this;
209
    }
210
211
    public function filter($filter)
212
    {
213
        $this->filter = $filter;
0 ignored issues
show
Documentation introduced by
The property filter does not exist on object<hiqdev\hiart\Query>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
214
215
        return $this;
216
    }
217
218
    public function from($index, $type = null)
219
    {
220
        $this->index = $index;
221
        $this->type  = $type;
222
223
        return $this;
224
    }
225
226 View Code Duplication
    public function fields($fields)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
227
    {
228
        if (is_array($fields) || $fields === null) {
229
            $this->fields = $fields;
0 ignored issues
show
Documentation introduced by
The property fields does not exist on object<hiqdev\hiart\Query>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
230
        } else {
231
            $this->fields = func_get_args();
0 ignored issues
show
Documentation introduced by
The property fields does not exist on object<hiqdev\hiart\Query>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
232
        }
233
234
        return $this;
235
    }
236
237 View Code Duplication
    public function source($source)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
238
    {
239
        if (is_array($source) || $source === null) {
240
            $this->source = $source;
0 ignored issues
show
Documentation introduced by
The property source does not exist on object<hiqdev\hiart\Query>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
241
        } else {
242
            $this->source = func_get_args();
0 ignored issues
show
Documentation introduced by
The property source does not exist on object<hiqdev\hiart\Query>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
243
        }
244
245
        return $this;
246
    }
247
248
    public function timeout($timeout)
249
    {
250
        $this->timeout = $timeout;
0 ignored issues
show
Documentation introduced by
The property timeout does not exist on object<hiqdev\hiart\Query>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
251
252
        return $this;
253
    }
254
}
255