DeviceStates   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 83
Duplicated Lines 22.89 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 19
loc 83
ccs 0
cts 26
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDeviceStates() 12 12 2
A getDeviceState() 7 7 1
A updateDeviceState() 0 13 2
A deleteDeviceState() 0 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 GuzzleHttp\Exception\RequestException;
22
use phparia\Client\AriClientAware;
23
use phparia\Exception\ConflictException;
24
use phparia\Exception\NotFoundException;
25
use phparia\Resources\DeviceState;
26
27
/**
28
 * DeviceStates API
29
 *
30
 * @author Brian Smith <[email protected]>
31
 */
32
class DeviceStates extends AriClientAware
33
{
34
    const DEVICE_STATE_UNKNOWN = 'UNKNOWN';
35
    const DEVICE_STATE_NOT_INUSE = 'NOT_INUSE';
36
    const DEVICE_STATE_INUSE = 'INUSE';
37
    const DEVICE_STATE_BUSY = 'BUSY';
38
    const DEVICE_STATE_INVALID = 'INVALID';
39
    const DEVICE_STATE_UNAVAILABLE = 'UNAVAILABLE';
40
    const DEVICE_STATE_RINGING = 'RINGING';
41
    const DEVICE_STATE_RINGINUSE = 'RINGINUSE';
42
    const DEVICE_STATE_ONHOLD = 'ONHOLD';
43
44
    /**
45
     * List all ARI controlled device states.
46
     *
47
     * @return DeviceState[]
48
     */
49 View Code Duplication
    public function getDeviceStates()
50
    {
51
        $uri = 'deviceStates';
52
        $response = $this->client->getEndpoint()->get($uri);
53
54
        $deviceStates = [];
55
        foreach (\GuzzleHttp\json_decode($response->getBody()) as $deviceState) {
56
            $deviceStates[] = new DeviceState($this->client, $deviceState);
57
        }
58
59
        return $deviceStates;
60
    }
61
62
    /**
63
     * Retrieve the current state of a device.
64
     *
65
     * @param string $deviceName
66
     * @return DeviceState
67
     */
68 View Code Duplication
    public function getDeviceState($deviceName)
69
    {
70
        $uri = "deviceStates/$deviceName";
71
        $response = $this->client->getEndpoint()->get($uri);
72
73
        return new DeviceState($this->client, \GuzzleHttp\json_decode($response->getBody()));
74
    }
75
76
    /**
77
     * Change the state of a device controlled by ARI. (Note - implicitly creates the device state).
78
     *
79
     * @param string $deviceName Name of the device
80
     * @param string $deviceState (required) Device state value
81
     * @throws ConflictException
82
     * @throws NotFoundException
83
     */
84
    public function updateDeviceState($deviceName, $deviceState)
85
    {
86
        $uri = "deviceStates/$deviceName";
87
        try {
88
            $this->client->getEndpoint()->put($uri, [
89
                'form_params' => [
90
                    'deviceState' => $deviceState,
91
                ]
92
            ]);
93
        } catch (RequestException $e) {
94
            $this->processRequestException($e);
95
        }
96
    }
97
98
    /**
99
     * Destroy a device-state controlled by ARI.
100
     *
101
     * @param string $deviceName Name of the device
102
     * @throws ConflictException
103
     * @throws NotFoundException
104
     */
105
    public function deleteDeviceState($deviceName)
106
    {
107
        $uri = "deviceStates/$deviceName";
108
        try {
109
            $this->client->getEndpoint()->delete($uri);
110
        } catch (RequestException $e) {
111
            $this->processRequestException($e);
112
        }
113
    }
114
}
115