GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( ecf505...a03304 )
by Dragos
12:40
created

Device   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 26.32 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 20
loc 76
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDevice() 10 10 2
A getDeviceCredentials() 10 10 2
A update() 0 8 2
A updatePhysical() 0 23 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
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
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...
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
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...
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