1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Speicher210\Estimote\Resource; |
6
|
|
|
|
7
|
|
|
use GuzzleHttp\Exception\ClientException; |
8
|
|
|
use Speicher210\Estimote\AbstractResource; |
9
|
|
|
use Speicher210\Estimote\Model\Device as DeviceModel; |
10
|
|
|
use Speicher210\Estimote\Model\Device\Update as BeaconUpdate; |
11
|
|
|
|
12
|
|
|
class Device extends AbstractResource |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param string $identifier |
16
|
|
|
* @return DeviceModel |
17
|
|
|
*/ |
18
|
|
View Code Duplication |
public function getDevice(string $identifier): DeviceModel |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
try { |
21
|
|
|
$response = $this->client->get('/v2/devices/' . $identifier); |
22
|
|
|
|
23
|
|
|
return $this->serializer->deserialize( |
24
|
|
|
(string)$response->getBody(), |
25
|
|
|
DeviceModel::class, |
26
|
|
|
'json' |
27
|
|
|
); |
28
|
|
|
} catch (ClientException $e) { |
29
|
|
|
throw $this->createApiException($e); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return DeviceModel[] |
35
|
|
|
*/ |
36
|
|
View Code Duplication |
public function getDevices(): array |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
try { |
39
|
|
|
$response = $this->client->get('/v2/devices'); |
40
|
|
|
|
41
|
|
|
return $this->serializer->deserialize( |
42
|
|
|
(string)$response->getBody(), |
43
|
|
|
'array<Speicher210\Estimote\Model\Device>', |
44
|
|
|
'json' |
45
|
|
|
); |
46
|
|
|
} catch (ClientException $e) { |
47
|
|
|
throw $this->createApiException($e); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $identifier The device identifier. |
53
|
|
|
* @param BeaconUpdate $data The data to send. |
54
|
|
|
* @return boolean |
55
|
|
|
*/ |
56
|
|
|
public function updateDevice(string $identifier, BeaconUpdate $data): bool |
57
|
|
|
{ |
58
|
|
|
$updatePayload = $this->serializer->serialize($data, 'json'); |
59
|
|
|
|
60
|
|
|
try { |
61
|
|
|
$response = $this->client->post( |
62
|
|
|
'v2/devices/' . $identifier, |
63
|
|
|
[ |
64
|
|
|
'headers' => [ |
65
|
|
|
'Content-Type' => 'application/json' |
66
|
|
|
], |
67
|
|
|
'body' => $updatePayload |
68
|
|
|
] |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
$json = \GuzzleHttp\json_decode($response->getBody(), true); |
72
|
|
|
|
73
|
|
|
return isset($json['success']) ? ($json['success'] === true) : false; |
74
|
|
|
} catch (ClientException $e) { |
75
|
|
|
throw $this->createApiException($e); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
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.