Completed
Push — master ( e9cf55...72c4df )
by Brian
03:18
created

DeviceStates   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 14.29 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 93.33%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
c 2
b 0
f 0
lcom 1
cbo 6
dl 11
loc 77
ccs 28
cts 30
cp 0.9333
rs 10

4 Methods

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

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 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
36
    /**
37
     * List all ARI controlled device states.
38
     *
39
     * @return DeviceState[]
40
     */
41 1
    public function getDeviceStates()
42
    {
43 1
        $uri = '/deviceStates';
44 1
        $response = $this->client->getEndpoint()->get($uri);
45
46 1
        $deviceStates = [];
47 1
        foreach ((array)$response as $deviceState) {
48 1
            $deviceStates[] = new DeviceState($this->client, $deviceState);
49 1
        }
50
51 1
        return $deviceStates;
52
    }
53
54
    /**
55
     * Retrieve the current state of a device.
56
     *
57
     * @param string $deviceName
58
     * @return DeviceState
59
     */
60 3
    public function getDeviceState($deviceName)
61
    {
62 3
        $uri = "/deviceStates/$deviceName";
63 3
        $response = $this->client->getEndpoint()->get($uri);
64
65 3
        return new DeviceState($this->client, $response);
66
    }
67
68
    /**
69
     * Change the state of a device controlled by ARI. (Note - implicitly creates the device state).
70
     *
71
     * @param string $deviceName Name of the device
72
     * @param string $deviceState (required) Device state value
73
     * @throws ConflictException
74
     * @throws NotFoundException
75
     */
76 2
    public function updateDeviceState($deviceName, $deviceState)
77
    {
78 2
        $uri = "/deviceStates/$deviceName";
79
        try {
80 2
            $this->client->getEndpoint()->put($uri, array(
81 2
                'deviceState' => $deviceState,
82 2
            ));
83 2
        } catch (Pest_NotFound $e) {
84
            throw new NotFoundException($e);
85 1
        } catch (Pest_Conflict $e) {
86 1
            throw new ConflictException($e);
87
        }
88 1
    }
89
90
    /**
91
     * Destroy a device-state controlled by ARI.
92
     *
93
     * @param string $deviceName Name of the device
94
     * @throws ConflictException
95
     * @throws NotFoundException
96
     */
97 2 View Code Duplication
    public function deleteDeviceState($deviceName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
98
    {
99 2
        $uri = "/deviceStates/$deviceName";
100
        try {
101 2
            $this->client->getEndpoint()->delete($uri);
102 2
        } catch (Pest_NotFound $e) {
103
            throw new NotFoundException($e);
104 1
        } catch (Pest_Conflict $e) {
105 1
            throw new ConflictException($e);
106
        }
107 1
    }
108
109
}
110