1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlexeyKuperhstokh\LocationBundle\Client; |
4
|
|
|
|
5
|
|
|
use AlexeyKuperhstokh\LocationBundle\Exceptions\MalformedDataException; |
6
|
|
|
use AlexeyKuperhstokh\LocationBundle\Exceptions\ServerErrorException; |
7
|
|
|
use AlexeyKuperhstokh\LocationBundle\Location\Location; |
8
|
|
|
use GuzzleHttp\ClientInterface; |
9
|
|
|
use Psr\Http\Message\RequestInterface; |
10
|
|
|
use Symfony\Component\Serializer\Encoder\JsonEncoder; |
11
|
|
|
use Symfony\Component\Serializer\Serializer; |
12
|
|
|
|
13
|
|
|
class Client |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var ClientInterface |
17
|
|
|
*/ |
18
|
|
|
protected $httpClient; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var RequestInterface |
22
|
|
|
*/ |
23
|
|
|
protected $request; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Client constructor. |
27
|
|
|
* @param ClientInterface $httpClient |
28
|
|
|
* @param RequestInterface $request |
29
|
|
|
*/ |
30
|
|
|
public function __construct(ClientInterface $httpClient, RequestInterface $request) |
31
|
|
|
{ |
32
|
|
|
$this->httpClient = $httpClient; |
33
|
|
|
$this->request = $request; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return Location[] |
38
|
|
|
*/ |
39
|
|
|
public function getLocations() |
40
|
|
|
{ |
41
|
|
|
$body = $this->sendRequest(); |
42
|
|
|
$serializer = new Serializer([], [new JsonEncoder()]); |
43
|
|
|
$decoded = $serializer->decode($body, 'json'); |
44
|
|
|
|
45
|
|
|
$this->validateResponse($decoded); |
46
|
|
|
|
47
|
|
|
$locations = array(); |
48
|
|
|
foreach ($decoded['data']['locations'] as $jsonLocation) { |
49
|
|
|
$locations[] = new Location($jsonLocation); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $locations; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param $decoded |
57
|
|
|
* @throws MalformedDataException |
58
|
|
|
* @throws ServerErrorException |
59
|
|
|
*/ |
60
|
|
|
public function validateResponse($decoded) |
61
|
|
|
{ |
62
|
|
View Code Duplication |
if (!isset($decoded['success'], $decoded['data']) || !is_array($decoded['data'])) { |
|
|
|
|
63
|
|
|
throw new MalformedDataException(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (!$decoded['success']) { |
67
|
|
|
$message = isset($decoded['data']['message']) ? $decoded['data']['message'] : null; |
68
|
|
|
$code = isset($decoded['data']['code']) ? $decoded['data']['code'] : null; |
69
|
|
|
throw new ServerErrorException($message, $code); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
View Code Duplication |
if (!isset($decoded['data']['locations']) || !is_array($decoded['data']['locations'])) { |
|
|
|
|
73
|
|
|
throw new MalformedDataException(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function sendRequest() |
81
|
|
|
{ |
82
|
|
|
$response = $this->httpClient->send($this->request); |
83
|
|
|
return $response->getBody()->getContents(); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.