Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
apclient.php
1<?php
3
13
15{
16 const SERVER_ENCODING = 'utf-8';
17 const ERROR_WRONG_ANSWER = 'WRONG_ANWSER';
18
19 const METHOD_BATCH = 'batch';
20
23
24 protected $connection = null;
25 protected $errorCollection = null;
26
27 protected static $requiredKeys = array('ENDPOINT');
28
29 public static function init()
30 {
32 if($connection)
33 {
34 return new self($connection);
35 }
36
37 return false;
38 }
39
40 public static function initById($connectionId)
41 {
42 $dbRes = ApTable::getById($connectionId);
43 $connection = $dbRes->fetch();
44
45 if($connection)
46 {
47 return new self($connection);
48 }
49
50 return false;
51 }
52
53 public function __construct(array $connection)
54 {
55 $this->errorCollection = new ErrorCollection();
56
57 if($this->checkConnection($connection))
58 {
59 $this->connection = $connection;
60 }
61 }
62
63 public function getConnection()
64 {
65 return $this->connection;
66 }
67
80 public function call($methodName, $additionalParams = null)
81 {
82 if($this->checkConnection($this->connection))
83 {
84 $request = $this->prepareRequest($additionalParams);
85
86 $httpClient = $this->getHttpClient();
87
88 $response = $httpClient->post(
89 $this->getRequestUrl($methodName),
90 $request
91 );
92
93 $result = $this->prepareResponse($response);
94
95 if(!$result)
96 {
97 $this->errorCollection->add(
98 array(
99 new Error("Wrong answer from service", static::ERROR_WRONG_ANSWER)
100 )
101 );
102 }
103
104 return $result;
105 }
106 else
107 {
108 throw new SystemException("No connection credentials");
109 }
110 }
111
112 public function batch($actions)
113 {
114 $batch = array();
115
116 if(is_array($actions))
117 {
118 foreach($actions as $queryKey => $cmdData)
119 {
120 if(is_array($cmdData))
121 {
122 list($cmd, $cmdParams) = array_values($cmdData);
123 $batch['cmd'][$queryKey] = $cmd.(is_array($cmdParams) ? '?'.http_build_query($this->prepareRequestData($cmdParams)) : '');
124 }
125 else
126 {
127 $batch['cmd'][$queryKey] = $cmdData;
128 }
129 }
130 }
131
132 return $this->call(static::METHOD_BATCH, $batch);
133 }
134
135 public function getErrorCollection()
136 {
138 }
139
140 protected function getHttpClient()
141 {
142 return new HttpClient(array(
143 'socketTimeout' => static::HTTP_SOCKET_TIMEOUT,
144 'streamTimeout' => static::HTTP_STREAM_TIMEOUT,
145 ));
146 }
147
148 protected function getRequestUrl($methodName)
149 {
150 return $this->connection['ENDPOINT'].$methodName;
151 }
152
153 protected function prepareRequestData($additionalParams)
154 {
155 if(!is_array($additionalParams))
156 {
157 $additionalParams = array();
158 }
159 else
160 {
161 $additionalParams = Encoding::convertEncoding($additionalParams, LANG_CHARSET, static::SERVER_ENCODING);
162 }
163
164 return $additionalParams;
165 }
166
167 protected function prepareRequest($additionalParams)
168 {
169 $additionalParams = $this->prepareRequestData($additionalParams);
170
171 return $additionalParams;
172 }
173
174 protected function prepareResponse($result)
175 {
176 try
177 {
178 return Json::decode($result);
179 }
180 catch(ArgumentException $e)
181 {
182 return false;
183 }
184 }
185
186 protected function checkConnection(array $connection)
187 {
188 foreach(static::$requiredKeys as $key)
189 {
190 if(empty($connection[$key]))
191 {
192 throw new ArgumentNullException('connection['.$key.']');
193 }
194 }
195
196 $endpoint = new Uri($connection['ENDPOINT']);
197 if(!$endpoint->getHost())
198 {
199 throw new ArgumentException('Invalid connection[ENDPOINT] value');
200 }
201
202 return true;
203 }
204}
checkConnection(array $connection)
Definition apclient.php:186
call($methodName, $additionalParams=null)
Definition apclient.php:80
prepareRequest($additionalParams)
Definition apclient.php:167
prepareRequestData($additionalParams)
Definition apclient.php:153
static initById($connectionId)
Definition apclient.php:40
__construct(array $connection)
Definition apclient.php:53