1 | <?php |
||
12 | class CommunicationAdapter |
||
13 | { |
||
14 | const RESPONSE_NEW_LINE = 0; |
||
15 | const RESPONSE_URL_ENCODED = 1; |
||
16 | |||
17 | /** |
||
18 | * @var Client |
||
19 | */ |
||
20 | protected $client; |
||
21 | |||
22 | /** |
||
23 | * @var Account |
||
24 | */ |
||
25 | protected $account; |
||
26 | |||
27 | /** |
||
28 | * @param \Checkdomain\Comodo\Model\Account $account |
||
29 | * |
||
30 | * @return CommunicationAdapter |
||
31 | */ |
||
32 | public function setAccount($account) |
||
38 | |||
39 | /** |
||
40 | * @return \Checkdomain\Comodo\Model\Account |
||
41 | */ |
||
42 | public function getAccount() |
||
46 | |||
47 | /** |
||
48 | * Constructs a communication adapter with an account |
||
49 | * |
||
50 | * @param Account $account |
||
51 | */ |
||
52 | public function __construct(Account $account = null) |
||
56 | |||
57 | /** |
||
58 | * @param \GuzzleHttp\Client $client |
||
59 | * |
||
60 | * @return CommunicationAdapter |
||
61 | */ |
||
62 | public function setClient($client) |
||
68 | |||
69 | /** |
||
70 | * @return \GuzzleHttp\Client |
||
71 | */ |
||
72 | public function getClient() |
||
80 | |||
81 | /** |
||
82 | * Sends a query to the provided url and return the response body. |
||
83 | * |
||
84 | * @param string $url |
||
85 | * @param array $params |
||
86 | * |
||
87 | * @return string |
||
88 | */ |
||
89 | public function sendToWebsite($url, array $params) |
||
90 | { |
||
91 | // Sending request |
||
92 | $client = $this->getClient(); |
||
93 | $response = $client->request('POST', $url, [ |
||
94 | 'query' => http_build_query($params, '', '&') |
||
95 | ]); |
||
96 | |||
97 | // Getting response body |
||
98 | $responseString = $response->getBody()->getContents(); |
||
99 | $responseString = trim($responseString); |
||
100 | |||
101 | return $responseString; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Send a request to the comodo API, and decodes the response as given |
||
106 | * |
||
107 | * @param string $url |
||
108 | * @param array $params |
||
109 | * @param int $responseType |
||
110 | * @param array|null $notDecode |
||
111 | * @param array $forceArray |
||
112 | * |
||
113 | * @return array|bool |
||
114 | */ |
||
115 | public function sendToApi($url, array $params, $responseType = self::RESPONSE_NEW_LINE, array $notDecode = null, $forceArray = array()) |
||
116 | { |
||
117 | $this->preSendToApiCheck(); |
||
118 | |||
119 | // Merging post-data |
||
120 | $fields = array(); |
||
121 | $fields["loginName"] = $this->getAccount()->getLoginName(); |
||
122 | $fields["loginPassword"] = $this->getAccount()->getLoginPassword(); |
||
123 | $fields = array_merge($fields, $params); |
||
124 | |||
125 | // Sending request |
||
126 | $client = $this->getClient(); |
||
127 | $response = $client->request('POST', $url, [ |
||
128 | 'form_params' => $fields |
||
129 | ]); |
||
130 | $query = http_build_query($params); |
||
131 | |||
132 | // Getting response body |
||
133 | $responseString = $response->getBody()->getContents(); |
||
134 | $responseString = trim($responseString); |
||
135 | |||
136 | // Decoding and returning response |
||
137 | if ($responseType == self::RESPONSE_NEW_LINE) { |
||
138 | return $this->decodeNewLineEncodedResponse($responseString, $query, $forceArray); |
||
139 | } else { |
||
140 | return $this->decodeUrlEncodedResponse($responseString, $query, $notDecode, $forceArray); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Checks, if a valid account has been provided |
||
146 | * |
||
147 | * @return bool |
||
148 | * @throws \Exception |
||
149 | */ |
||
150 | protected function preSendToApiCheck() |
||
162 | |||
163 | /** |
||
164 | * Decodes a responseString, separated by new lines and returns an response array |
||
165 | * |
||
166 | * @param string $responseString |
||
167 | * @param string $requestQuery |
||
168 | * @param array $forceArray |
||
169 | * |
||
170 | * @return array |
||
171 | */ |
||
172 | protected function decodeNewLineEncodedResponse($responseString, $requestQuery, array $forceArray = array()) |
||
231 | |||
232 | /** |
||
233 | * Decodes a responseString, encoded in query-string-format and returns an response array |
||
234 | * |
||
235 | * @param string $responseString |
||
236 | * @param string $requestQuery |
||
237 | * @param array $notDecode |
||
238 | * @param string[] $forceArray |
||
239 | * |
||
240 | * @return mixed |
||
241 | */ |
||
242 | protected function decodeUrlEncodedResponse( |
||
283 | } |
||
284 |