These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * Copyright 2014 Brian Smith <[email protected]>. |
||
5 | * |
||
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||
7 | * you may not use this file except in compliance with the License. |
||
8 | * You may obtain a copy of the License at |
||
9 | * |
||
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
11 | * |
||
12 | * Unless required by applicable law or agreed to in writing, software |
||
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
15 | * See the License for the specific language governing permissions and |
||
16 | * limitations under the License. |
||
17 | */ |
||
18 | |||
19 | namespace phparia\Api; |
||
20 | |||
21 | use Pest_Conflict; |
||
22 | use Pest_NotFound; |
||
23 | use phparia\Client\AriClientAware; |
||
24 | use phparia\Exception\ConflictException; |
||
25 | use phparia\Exception\NotFoundException; |
||
26 | use phparia\Resources\DeviceState; |
||
27 | |||
28 | /** |
||
29 | * DeviceStates API |
||
30 | * |
||
31 | * @author Brian Smith <[email protected]> |
||
32 | */ |
||
33 | class DeviceStates extends AriClientAware |
||
34 | { |
||
35 | const DEVICE_STATE_UNKNOWN = 'UNKNOWN'; |
||
36 | const DEVICE_STATE_NOT_INUSE = 'NOT_INUSE'; |
||
37 | const DEVICE_STATE_INUSE = 'INUSE'; |
||
38 | const DEVICE_STATE_BUSY = 'BUSY'; |
||
39 | const DEVICE_STATE_INVALID = 'INVALID'; |
||
40 | const DEVICE_STATE_UNAVAILABLE = 'UNAVAILABLE'; |
||
41 | const DEVICE_STATE_RINGING = 'RINGING'; |
||
42 | const DEVICE_STATE_RINGINUSE = 'RINGINUSE'; |
||
43 | const DEVICE_STATE_ONHOLD = 'ONHOLD'; |
||
44 | |||
45 | /** |
||
46 | * List all ARI controlled device states. |
||
47 | * |
||
48 | * @return DeviceState[] |
||
49 | */ |
||
50 | 1 | public function getDeviceStates() |
|
51 | { |
||
52 | 1 | $uri = '/deviceStates'; |
|
53 | 1 | $response = $this->client->getEndpoint()->get($uri); |
|
54 | |||
55 | 1 | $deviceStates = []; |
|
56 | 1 | foreach ((array)$response as $deviceState) { |
|
57 | 1 | $deviceStates[] = new DeviceState($this->client, $deviceState); |
|
58 | 1 | } |
|
59 | |||
60 | 1 | return $deviceStates; |
|
61 | } |
||
62 | |||
63 | /** |
||
64 | * Retrieve the current state of a device. |
||
65 | * |
||
66 | * @param string $deviceName |
||
67 | * @return DeviceState |
||
68 | */ |
||
69 | 3 | public function getDeviceState($deviceName) |
|
70 | { |
||
71 | 3 | $uri = "/deviceStates/$deviceName"; |
|
72 | 3 | $response = $this->client->getEndpoint()->get($uri); |
|
73 | |||
74 | 3 | return new DeviceState($this->client, $response); |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * Change the state of a device controlled by ARI. (Note - implicitly creates the device state). |
||
79 | * |
||
80 | * @param string $deviceName Name of the device |
||
81 | * @param string $deviceState (required) Device state value |
||
82 | * @throws ConflictException |
||
83 | * @throws NotFoundException |
||
84 | */ |
||
85 | 2 | public function updateDeviceState($deviceName, $deviceState) |
|
86 | { |
||
87 | 2 | $uri = "/deviceStates/$deviceName"; |
|
88 | try { |
||
89 | 2 | $this->client->getEndpoint()->put($uri, array( |
|
90 | 2 | 'deviceState' => $deviceState, |
|
91 | 2 | )); |
|
92 | 2 | } catch (Pest_NotFound $e) { |
|
93 | throw new NotFoundException($e); |
||
94 | 1 | } catch (Pest_Conflict $e) { |
|
95 | 1 | throw new ConflictException($e); |
|
96 | } |
||
97 | 1 | } |
|
98 | |||
99 | /** |
||
100 | * Destroy a device-state controlled by ARI. |
||
101 | * |
||
102 | * @param string $deviceName Name of the device |
||
103 | * @throws ConflictException |
||
104 | * @throws NotFoundException |
||
105 | */ |
||
106 | 2 | View Code Duplication | public function deleteDeviceState($deviceName) |
0 ignored issues
–
show
|
|||
107 | { |
||
108 | 2 | $uri = "/deviceStates/$deviceName"; |
|
109 | try { |
||
110 | 2 | $this->client->getEndpoint()->delete($uri); |
|
111 | 2 | } catch (Pest_NotFound $e) { |
|
112 | throw new NotFoundException($e); |
||
113 | 1 | } catch (Pest_Conflict $e) { |
|
114 | 1 | throw new ConflictException($e); |
|
115 | } |
||
116 | 1 | } |
|
117 | |||
118 | } |
||
119 |
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.