1 | <?php |
||
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) |
|
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 = []) |
|
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() |
|
124 | } |
||
125 |