Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
12 | class Client extends \GuzzleHttp\Client |
||
13 | { |
||
14 | /** |
||
15 | * @const string Version number |
||
16 | */ |
||
17 | const VERSION = '0.0.1'; |
||
18 | |||
19 | /** |
||
20 | * Defaults to expecting that Apache Tomcat runs on port 8080 on localhost |
||
21 | * (127.0.0.1). |
||
22 | * |
||
23 | * @var array[] |
||
24 | */ |
||
25 | protected $options = [ |
||
26 | 'scheme' => 'http', |
||
27 | 'hostname' => 'localhost', |
||
28 | 'port' => '8080', |
||
29 | ]; |
||
30 | |||
31 | /** |
||
32 | * @param $options array |
||
33 | * |
||
34 | * @return void |
||
|
|||
35 | */ |
||
36 | 2 | public function __construct($options = []) |
|
60 | |||
61 | /** |
||
62 | * Fetches the Dealer Balance from SmartCall. |
||
63 | * |
||
64 | * @throws Exception |
||
65 | * |
||
66 | * @return array |
||
67 | */ |
||
68 | 1 | View Code Duplication | public function getDealerBalance() |
86 | |||
87 | /** |
||
88 | * Fetches the Dealer Balance from SmartCall. |
||
89 | * |
||
90 | * @throws Exception |
||
91 | * |
||
92 | * @return array |
||
93 | */ |
||
94 | 1 | View Code Duplication | public function isDealerRegistered($msisdn) |
95 | { |
||
96 | try { |
||
97 | 1 | $response = $this->get( |
|
98 | 1 | sprintf( |
|
99 | 1 | '/SmartcallRestfulProxy/registered/%s', |
|
100 | $msisdn |
||
101 | 1 | ) |
|
102 | 1 | ); |
|
103 | |||
104 | return [ |
||
105 | 1 | 'status' => 'ok', |
|
106 | 1 | 'http_code' => $response->getStatusCode(), |
|
107 | 1 | 'body' => (string) $response->getBody(), |
|
108 | 1 | ]; |
|
109 | } catch (\GuzzleHttp\Exception\ServerException $e) { |
||
110 | return [ |
||
111 | 'status' => 'error', |
||
112 | 'http_code' => $e->getResponse()->getStatusCode(), |
||
113 | 'body' => (string) $e->getResponse()->getBody(), |
||
114 | ]; |
||
115 | } |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Fetches the Product from SmartCall. |
||
120 | * |
||
121 | * @param int $productId Product Identifier |
||
122 | * |
||
123 | * @throws Exception |
||
124 | * |
||
125 | * @return array |
||
126 | */ |
||
127 | 2 | public function getProduct($productId) |
|
128 | { |
||
129 | try { |
||
130 | 2 | $response = $this->get( |
|
131 | 2 | sprintf( |
|
132 | 2 | '/SmartcallRestfulProxy/product_js/%d', |
|
133 | $productId |
||
134 | 2 | ) |
|
135 | 2 | ); |
|
136 | |||
137 | return [ |
||
138 | 1 | 'status' => 'ok', |
|
139 | 1 | 'http_code' => $response->getStatusCode(), |
|
140 | 1 | 'body' => (string) $response->getBody(), |
|
141 | 1 | ]; |
|
142 | 1 | } catch (\GuzzleHttp\Exception\ServerException $e) { |
|
143 | 1 | $body = (string) $e->getResponse()->getBody(); |
|
144 | |||
145 | 1 | preg_match('!<p><b>type</b> Exception report</p><p><b>message</b> <u>(.*[^</u>])</u></p><p><b>description</b>!', $body, $matches); |
|
146 | |||
147 | return [ |
||
148 | 1 | 'status' => 'error', |
|
149 | 1 | 'http_code' => $e->getResponse()->getStatusCode(), |
|
150 | 1 | 'body' => $matches['1'], |
|
151 | 1 | ]; |
|
152 | } |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Fetches the Product List from SmartCall. |
||
157 | * |
||
158 | * @throws Exception |
||
159 | * |
||
160 | * @return array |
||
161 | */ |
||
162 | 1 | View Code Duplication | public function getProducts() |
180 | |||
181 | /** |
||
182 | * Fetches the details of the last transaction processed from SmartCall. |
||
183 | * |
||
184 | * @throws Exception |
||
185 | * |
||
186 | * @return array |
||
187 | */ |
||
188 | 1 | View Code Duplication | public function getLastTransaction() |
206 | |||
207 | /** |
||
208 | * Fetches the Product List by the specified network identifier from SmartCall. |
||
209 | * |
||
210 | * @param int $networkId identifier for the network |
||
211 | * |
||
212 | * @throws Exception |
||
213 | * |
||
214 | * @return array |
||
215 | */ |
||
216 | 1 | View Code Duplication | public function getProductsByNetwork($networkId) |
217 | { |
||
218 | try { |
||
219 | 1 | $response = $this->get( |
|
220 | 1 | sprintf( |
|
221 | 1 | '/SmartcallRestfulProxy/network_js/%d', |
|
222 | $networkId |
||
223 | 1 | ) |
|
224 | 1 | ); |
|
225 | |||
226 | return [ |
||
227 | 1 | 'status' => 'ok', |
|
228 | 1 | 'http_code' => $response->getStatusCode(), |
|
229 | 1 | 'body' => (string) $response->getBody(), |
|
230 | 1 | ]; |
|
231 | } catch (\GuzzleHttp\Exception\ServerException $e) { |
||
232 | return [ |
||
233 | 'status' => 'error', |
||
234 | 'http_code' => $e->getResponse()->getStatusCode(), |
||
235 | 'body' => (string) $e->getResponse()->getBody(), |
||
236 | ]; |
||
237 | } |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Purchase a voucher or do a pinless recharge on SmartCall. |
||
242 | * |
||
243 | * @param int $productId identifier for the product |
||
244 | * @param int $amount amount in rands of the product |
||
245 | * @param int $msisdn mobile number of the recipient of the product |
||
246 | * @param int $deviceId mobile number or meter number of the device being recharged |
||
247 | * @param string $clientRef Client Reference (use a UUID) |
||
248 | * @param bool $pinless true = device will be recharged via the network's IN platform | false = pinbased virtual voucher |
||
249 | * @param bool $sendSms true = SmartCall will send the voucher via SMS | false don't send the voucher via SMS |
||
250 | * |
||
251 | * @throws Exception |
||
252 | * |
||
253 | * @return array |
||
254 | */ |
||
255 | 1 | public function purchaseProduct($productId, $amount, $msisdn, $deviceId, $clientRef, $pinless = true, $sendSms = false) |
|
256 | { |
||
257 | try { |
||
258 | 1 | $response = $this->post( |
|
259 | 1 | sprintf( |
|
260 | 1 | '/SmartcallRestfulProxy/recharge_js/%s', |
|
261 | $clientRef |
||
262 | 1 | ), |
|
263 | [ |
||
264 | 'json' => [ |
||
265 | 1 | 'amount' => $amount, |
|
266 | 1 | 'deviceId' => $deviceId, |
|
267 | 1 | 'pinless' => $pinless, |
|
268 | 1 | 'productId' => $productId, |
|
269 | 1 | 'sendSms' => $sendSms, |
|
270 | 1 | 'smsRecipientMsisdn' => $msisdn, |
|
271 | 1 | ], |
|
272 | ] |
||
273 | 1 | ); |
|
274 | |||
275 | return [ |
||
276 | 1 | 'status' => 'ok', |
|
277 | 1 | 'http_code' => $response->getStatusCode(), |
|
278 | 1 | 'body' => (string) $response->getBody(), |
|
279 | 1 | ]; |
|
280 | } catch (\GuzzleHttp\Exception\ServerException $e) { |
||
281 | return [ |
||
282 | 'status' => 'error', |
||
283 | 'http_code' => $e->getResponse()->getStatusCode(), |
||
284 | 'body' => (string) $e->getResponse()->getBody(), |
||
285 | ]; |
||
286 | } |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * Searches SmartCall for a specified transaction using a specified key and string to search |
||
291 | * against at SmartCall. |
||
292 | * |
||
293 | * @param string $field client_ref | msisdn | order_reference |
||
294 | * @param string $query_string Client Reference when client_ref or a users MSISDN when msisdn |
||
295 | * |
||
296 | * @throws Exception |
||
297 | * |
||
298 | * @return array |
||
299 | */ |
||
300 | 3 | public function searchTransaction($field, $search) |
|
333 | } |
||
334 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.