1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Speicher210\KontaktIO\Resource; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception\ClientException; |
6
|
|
|
use Speicher210\KontaktIO\AbstractResource; |
7
|
|
|
use Speicher210\KontaktIO\Model\Device as DeviceModel; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Device resource. |
11
|
|
|
*/ |
12
|
|
|
class Device extends AbstractResource |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Get a device. |
16
|
|
|
* |
17
|
|
|
* @param string $uniqueId The device unique ID. |
18
|
|
|
* @return DeviceModel |
19
|
|
|
*/ |
20
|
|
View Code Duplication |
public function getDevice($uniqueId) |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
try { |
23
|
|
|
$response = $this->client->get('/device/'.$uniqueId); |
24
|
|
|
|
25
|
|
|
return $this->serializer->deserialize($response->getBody(), DeviceModel::class, 'json'); |
26
|
|
|
} catch (ClientException $e) { |
27
|
|
|
throw $this->createApiException($e); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Update devices. |
33
|
|
|
* |
34
|
|
|
* @param string|array $uniqueId The unique IDs to update. |
35
|
|
|
* @param string $deviceType The device type. One of the DEVICE_TYPE_* Device model constants. |
36
|
|
|
* @param DeviceModel $values The values for the device(s). |
37
|
|
|
* @return boolean |
38
|
|
|
*/ |
39
|
|
|
public function update($uniqueId, $deviceType, DeviceModel $values) |
40
|
|
|
{ |
41
|
|
|
$jsonSerialized = $this->serializer->serialize($values, 'json'); |
42
|
|
|
$values = \GuzzleHttp\json_decode($jsonSerialized, true); |
43
|
|
|
|
44
|
|
|
$values['uniqueId'] = implode(',', (array)$uniqueId); |
45
|
|
|
$values['deviceType'] = $deviceType; |
46
|
|
|
|
47
|
|
|
try { |
48
|
|
|
$response = $this->client->post( |
49
|
|
|
'/device/update', |
50
|
|
|
[ |
51
|
|
|
'form_params' => $values, |
52
|
|
|
'headers' => [ |
53
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded', |
54
|
|
|
], |
55
|
|
|
] |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
return $response->getStatusCode() == 200; |
59
|
|
|
} catch (ClientException $e) { |
60
|
|
|
throw $this->createApiException($e); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
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.