Completed
Push — master ( 729f46...ea06ac )
by Andrii
02:49
created

Query::searchOne()   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 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * Tools to use API as ActiveRecord for Yii2
4
 *
5
 * @link      https://github.com/hiqdev/yii2-hiart
6
 * @package   yii2-hiart
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\hiart;
12
13
use yii\db\QueryInterface;
14
15
/**
16
 * Query represents API query in a way that is independent from a concrete API.
17
 * Holds API query information:
18
 * - general query data
19
 *      - action: action to be performed with this query, e.g. search, insert, update, delete
20
 *      - options: other additional options, like
21
 *          - raw: do not decode response
22
 *          - batch: batch(bulk) request
23
 *          - timeout, ...
24
 * - insert/update query data
25
 *      - body: insert or update data
26
 * - select query data
27
 *      - select: fields to select
28
 *      - count: marks count query
29
 *      - from: entity being queried, e.g. user
30
 *      - join: data how to join with other entities
31
 * - other standard query options provided with QueryTrait:
32
 *      - where, limit, offset, orderBy, indexBy.
33
 */
34
class Query extends \yii\db\Query implements QueryInterface
35
{
36
    /**
37
     * @var string action that this query performs
38
     */
39
    public $action;
40
41
    /**
42
     * @var array query options e.g. raw, batch
43
     */
44
    public $options = [];
45
46
    public $count;
47
48
    public $body = [];
49
50 1
    public static function instantiate($action, $from, array $options = [])
51
    {
52 1
        $query = new static();
53
54 1
        return $query->action($action)->from($from)->options($options);
55
    }
56
57
    public function createCommand($db = null)
58
    {
59
        if ($db === null) {
60
            throw new \Exception('no db given to Query::createCommand');
61
        }
62
63
        $commandConfig = $db->getQueryBuilder()->build($this);
64
65
        return $db->createCommand($commandConfig);
66
    }
67
68
    public function one($db = null)
69
    {
70
        return $this->searchOne($db);
71
    }
72
73
    public function searchOne($db = null)
0 ignored issues
show
Unused Code introduced by
The parameter $db 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...
74
    {
75
        return $this->limit(1)->addOption('batch', false)->search();
76
    }
77
78
    public function all($db = null)
79
    {
80
        $rows = $this->searchAll();
81
82
        if (!empty($rows) && $this->indexBy !== null) {
83
            $result = [];
84
            foreach ($rows as $row) {
85 View Code Duplication
                if ($this->indexBy instanceof \Closure) {
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...
86
                    $key = call_user_func($this->indexBy, $row);
87
                } else {
88
                    $key = $row[$this->indexBy];
89
                }
90
                $result[$key] = $row;
91
            }
92
            $rows = $result;
93
        }
94
95
        return $rows;
96
    }
97
98 2
    public function searchAll($db = null)
0 ignored issues
show
Unused Code introduced by
The parameter $db 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...
99
    {
100 2
        return $this->addOption('batch', true)->search();
101
    }
102
103 2
    public function search($db = null)
104
    {
105 2
        return $this->createCommand($db)->search();
106
    }
107
108
    public function delete($db = null, $options = [])
109
    {
110
        return $this->createCommand($db)->deleteByQuery($options);
111
    }
112
113
    public function count($q = '*', $db = null)
114
    {
115
        $this->count = $q;
116
117
        return (int) $this->searchAll();
118
    }
119
120
    public function exists($db = null)
121
    {
122
        return !empty(self::one($db));
123
    }
124
125 1
    public function action($action)
126
    {
127 1
        $this->action = $action;
128
129 1
        return $this;
130
    }
131
132 2
    public function addAction($action)
133
    {
134 2
        if (empty($this->action)) {
135 2
            $this->action = $action;
136 2
        }
137
138 2
        return $this;
139
    }
140
141 2
    public function addOption($name, $value)
142
    {
143 2
        if (!isset($this->options[$name])) {
144 2
            $this->options[$name] = $value;
145 2
        }
146
147 2
        return $this;
148
    }
149
150
    public function getOption($name)
151
    {
152
        return isset($this->options[$name]) ? $this->options[$name] : null;
153
    }
154
155 1
    public function options($options)
156
    {
157 1
        $this->options = $options;
158
159 1
        return $this;
160
    }
161
162
    public function addOptions($options)
163
    {
164
        if (!empty($options)) {
165
            $this->options = array_merge($this->options, $options);
166
        }
167
168
        return $this;
169
    }
170
171 1
    public function body($body)
172
    {
173 1
        $this->body = $body;
174
175 1
        return $this;
176
    }
177
178
    public function innerJoin($table, $on = '', $params = [])
179
    {
180
        $this->join[] = (array) $table;
181
182
        return $this;
183
    }
184
185 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...
186
    {
187
        if (is_array($fields) || $fields === null) {
188
            $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...
189
        } else {
190
            $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...
191
        }
192
193
        return $this;
194
    }
195
196 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...
197
    {
198
        if (is_array($source) || $source === null) {
199
            $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...
200
        } else {
201
            $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...
202
        }
203
204
        return $this;
205
    }
206
}
207