1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Speicher210\KontaktIO\Resource; |
6
|
|
|
|
7
|
|
|
use GuzzleHttp\Exception\ClientException; |
8
|
|
|
use Speicher210\KontaktIO\AbstractResource; |
9
|
|
|
use Speicher210\KontaktIO\Model\Device as DeviceModel; |
10
|
|
|
use Speicher210\KontaktIO\Model\Device\Credentials as DeviceCredentialsModel; |
11
|
|
|
use Speicher210\KontaktIO\Model\Device\Update as DeviceUpdate; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Device resource. |
15
|
|
|
*/ |
16
|
|
|
class Device extends AbstractResource |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Get a device. |
20
|
|
|
* |
21
|
|
|
* @param string $uniqueId The device unique ID. |
22
|
|
|
* @return DeviceModel |
23
|
|
|
* @throws \Speicher210\KontaktIO\Exception\ApiException |
24
|
|
|
*/ |
25
|
|
View Code Duplication |
public function getDevice(string $uniqueId): DeviceModel |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
try { |
28
|
|
|
$response = $this->client->get('/device/' . $uniqueId); |
29
|
|
|
|
30
|
|
|
return $this->serializer->deserialize($response->getBody(), DeviceModel::class, 'json'); |
31
|
|
|
} catch (ClientException $e) { |
32
|
|
|
throw $this->createApiException($e); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get the credentials of a device. |
38
|
|
|
* |
39
|
|
|
* @param string $uniqueId The device unique ID. |
40
|
|
|
* @return DeviceCredentialsModel |
41
|
|
|
*/ |
42
|
|
View Code Duplication |
public function getDeviceCredentials(string $uniqueId): DeviceCredentialsModel |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
try { |
45
|
|
|
$response = $this->client->get('/device/' . $uniqueId . '/credentials'); |
46
|
|
|
|
47
|
|
|
return $this->serializer->deserialize($response->getBody(), DeviceCredentialsModel::class, 'json'); |
48
|
|
|
} catch (ClientException $e) { |
49
|
|
|
throw $this->createApiException($e); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Update devices. |
55
|
|
|
* |
56
|
|
|
* @param DeviceUpdate $deviceUpdate |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
public function update(DeviceUpdate $deviceUpdate): bool |
60
|
|
|
{ |
61
|
|
|
if ($deviceUpdate->physical() !== null) { |
62
|
|
|
return $this->updatePhysical($deviceUpdate->uniqueId(), $deviceUpdate->physical()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return true; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function updatePhysical(string $uniqueId, DeviceUpdate\Physical $physical): bool |
69
|
|
|
{ |
70
|
|
|
$jsonSerialized = $this->serializer->serialize($physical, 'json'); |
71
|
|
|
$values = \GuzzleHttp\json_decode($jsonSerialized, true); |
72
|
|
|
|
73
|
|
|
$values['uniqueId'] = $uniqueId; |
74
|
|
|
|
75
|
|
|
try { |
76
|
|
|
$response = $this->client->post( |
77
|
|
|
'/config/create', |
78
|
|
|
[ |
79
|
|
|
'form_params' => $values, |
80
|
|
|
'headers' => [ |
81
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded' |
82
|
|
|
] |
83
|
|
|
] |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
return $response->getStatusCode() === 200; |
87
|
|
|
} catch (ClientException $e) { |
88
|
|
|
throw $this->createApiException($e); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
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.