1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ivory Google Map package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ivory\GoogleMap\Service\TimeZone; |
13
|
|
|
|
14
|
|
|
use Http\Client\HttpClient; |
15
|
|
|
use Http\Message\MessageFactory; |
16
|
|
|
use Ivory\GoogleMap\Service\AbstractService; |
17
|
|
|
use Ivory\GoogleMap\Service\TimeZone\Request\TimeZoneRequestInterface; |
18
|
|
|
use Ivory\GoogleMap\Service\TimeZone\Response\TimeZoneResponse; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author GeLo <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class TimeZoneService extends AbstractService |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @param HttpClient $client |
27
|
|
|
* @param MessageFactory $messageFactory |
28
|
|
|
*/ |
29
|
|
|
public function __construct(HttpClient $client, MessageFactory $messageFactory) |
30
|
|
|
{ |
31
|
|
|
parent::__construct($client, $messageFactory, 'http://maps.googleapis.com/maps/api/timezone'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
public function setHttps($https) |
38
|
|
|
{ |
39
|
|
|
if (!$https) { |
40
|
|
|
throw new \InvalidArgumentException('The http scheme is not supported.'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
parent::setHttps($https); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param TimeZoneRequestInterface $request |
48
|
|
|
* |
49
|
|
|
* @return TimeZoneResponse |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function process(TimeZoneRequestInterface $request) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$response = $this->getClient()->sendRequest($this->createRequest($request->build())); |
54
|
|
|
$data = $this->parse((string) $response->getBody()); |
55
|
|
|
|
56
|
|
|
return $this->buildResponse($data); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $data |
61
|
|
|
* |
62
|
|
|
* @return mixed[] |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
private function parse($data) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
if ($this->getFormat() === self::FORMAT_JSON) { |
67
|
|
|
return json_decode($data, true); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $this->getXmlParser()->parse($data, [], true); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param mixed[] $data |
75
|
|
|
* |
76
|
|
|
* @return TimeZoneResponse |
77
|
|
|
*/ |
78
|
|
|
private function buildResponse(array $data) |
79
|
|
|
{ |
80
|
|
|
$response = new TimeZoneResponse(); |
81
|
|
|
$response->setStatus($data['status']); |
82
|
|
|
$response->setDstOffset((int) $data['dstOffset']); |
83
|
|
|
$response->setRawOffset((int) $data['rawOffset']); |
84
|
|
|
$response->setTimeZoneId($data['timeZoneId']); |
85
|
|
|
$response->setTimeZoneName($data['timeZoneName']); |
86
|
|
|
|
87
|
|
|
return $response; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
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.