Completed
Push — master ( ce46b7...20cdab )
by Andrii
02:21
created

Command   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 84.21%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 0
loc 107
ccs 16
cts 19
cp 0.8421
rs 10
c 1
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setRequest() 0 6 1
A search() 0 6 1
A getProfileCategory() 0 4 1
A send() 0 11 1
A update() 0 6 1
A delete() 0 6 1
A perform() 0 7 1
A insert() 0 6 1
1
<?php
2
/**
3
 * ActiveRecord for API
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;
14
15
/**
16
 * The Command class implements execution of request.
17
 */
18
class Command extends \yii\base\Component
19
{
20
    /**
21
     * @var AbstractConnection
22
     */
23
    public $db;
24
25
    /**
26
     * @var RequestInterface request object
27
     */
28
    protected $request;
29
30 2
    public function setRequest(RequestInterface $request)
31
    {
32 2
        $this->request = $request;
33
34 2
        return $this;
35
    }
36
37
    /**
38
     * Sends a request to retrieve data.
39
     * In API this could be get, search or list request.
40
     * @param array $options send options
41
     * @throws ResponseErrorException
42
     * @return mixed response data
43
     */
44 2
    public function search($options = [])
45
    {
46 2
        $this->request->getQuery()->addAction('search');
47
48 2
        return $this->send($options);
49
    }
50
51
    /**
52
     * Sends a request to create/insert data.
53
     * @param mixed $table entity to create
54
     * @param mixed $columns attributes of object to create
55
     * @param array $params request parameters
56
     * @return $this
57
     */
58
    public function insert($table, $columns, array $params = [])
59
    {
60
        $request = $this->db->getQueryBuilder()->insert($table, $columns, $params);
61
62
        return $this->setRequest($request);
63
    }
64
65
    /**
66
     * Sends a request to update data.
67
     * @param mixed $table entity to update
68
     * @param mixed $columns attributes of object to update
69
     * @param array $condition
70
     * @param array $params request parameters
71
     * @return $this
72
     */
73
    public function update($table, $columns, $condition = [], array $params = [])
74
    {
75
        $request = $this->db->getQueryBuilder()->update($table, $columns, $condition, $params);
76
77
        return $this->setRequest($request);
78
    }
79
80
    public function delete($table, $condition, array $params = [])
81
    {
82
        $request = $this->db->getQueryBuilder()->delete($table, $condition, $params);
83
84
        return $this->setRequest($request);
85
    }
86
87
    /**
88
     * Creates and executes request with given data.
89
     * @param string $action
90
     * @param string $table
91
     * @param mixed $body
92
     * @param array $params request parameters
93
     * @return mixed response data
94
     */
95
    public function perform($action, $table, $body = [], array $params = [])
96
    {
97
        $request = $this->db->getQueryBuilder()->perform($action, $table, $body, $params);
98
        $this->setRequest($request);
99
100
        return $this->send();
101
    }
102
103
    /**
104
     * Executes the request.
105
     * @param array $options send options
106
     * @return mixed response data
107
     */
108 2
    public function send($options = [])
109
    {
110 2
        $profile = serialize($this->request);
111 2
        $category = static::getProfileCategory();
112 2
        Yii::beginProfile($profile, $category);
113 2
        $response = $this->request->send($options);
114 2
        Yii::endProfile($profile, $category);
115 2
        $this->db->checkResponse($response);
116
117 2
        return $response->getData();
118
    }
119
120 2
    public static function getProfileCategory()
121
    {
122 2
        return __METHOD__;
123
    }
124
}
125