Completed
Push — master ( 1d7e0e...752c00 )
by Dmitry
10:25
created

Command::search()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.216

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 0
loc 8
ccs 2
cts 5
cp 0.4
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
crap 1.216
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-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\hiart;
13
14
use yii\base\Component;
15
use yii\helpers\ArrayHelper;
16
use yii\helpers\Inflector;
17
use yii\helpers\Json;
18
19
/**
20
 * The Command class implements the API for accessing REST API.
21
 */
22
class Command extends Component
23
{
24
    /**
25
     * @var Connection
26
     */
27
    public $db;
28
    /**
29
     * @var string|array the indexes to execute the query on. Defaults to null meaning all indexes
30
     */
31
    public $index;
32
    /**
33
     * @var string|array the types to execute the query on. Defaults to null meaning all types
34
     */
35
    public $type;
36
    /**
37
     * @var array list of arrays or json strings that become parts of a query
38
     */
39
    public $queryParts = [];
40
41
    /**
42
     * Sends a request to the _search API and returns the result.
43
     * @param array $options
44
     * @throws ErrorResponseException
45
     * @return mixed
46
     */
47 1
    public function search($options = [])
48
    {
49
        $url     = $this->index . Inflector::id2camel(ArrayHelper::remove($options, 'scenario', 'search'));
50 1
        $query   = $this->queryParts;
51
        $options = array_merge($query, $options);
52
53
        return $this->db->post($url, $options);
54
    }
55
56
    public function insert($action, $data, $id = null, $options = [])
57
    {
58
        $options = array_merge($data, $options);
59
60
        if ($id !== null) {
61
            return $this->db->put($action . 'Update', array_merge($options, ['id' => $id]));
62
        } else {
63
            return $this->db->post($action . 'Create', $options);
64
        }
65
    }
66
67
    public function get($modelName, $primaryKey, $options)
68
    {
69
        return $this->db->post($modelName . 'GetInfo', ArrayHelper::merge(['id' => $primaryKey], $options));
70
    }
71
72
    public function mget($index, $type, $ids, $options = [])
73
    {
74
        $body = Json::encode(['ids' => array_values($ids)]);
75
76
        return $this->db->post([$index, $type, '_mget'], $options, $body);
0 ignored issues
show
Documentation introduced by
array($index, $type, '_mget') is of type array<integer,?,{"0":"?","1":"?","2":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
77
    }
78
79
    public function exists($index, $type, $id)
80
    {
81
        return $this->db->head([$index, $type, $id]);
0 ignored issues
show
Documentation introduced by
array($index, $type, $id) is of type array<integer,?,{"0":"?","1":"?","2":"?"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
82
    }
83
84
    public function delete($index, $id, $options = [])
85
    {
86
        return $this->db->delete($index . 'Delete', array_merge($options, ['id' => $id]));
87
    }
88
89
    public function update($index, $id, $data, $options = [])
90
    {
91
        $options['id'] = $id;
92
93
        return $this->db->put($index . 'Update', array_merge($data, $options));
94
    }
95
96
    /**
97
     * @param $action
98
     * @param mixed $body request parameters
99
     * @return mixed
100
     */
101
    public function perform($action, $body = [])
102
    {
103
        return $this->db->post($action, [], $body);
104
    }
105
}
106