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.

Application   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 165
Duplicated Lines 20.61 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 34
loc 165
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A batch() 0 22 2
A getUsers() 17 17 1
A getUserById() 17 17 1
B deleteUserById() 0 29 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
namespace Choccybiccy\HumanApi;
4
5
use Choccybiccy\HumanApi\Exception\UnexpectedResponseException;
6
use Choccybiccy\HumanApi\Exception\UnsupportedBatchEndpointException;
7
use GuzzleHttp\Exception\RequestException;
8
9
/**
10
 * Class Application
11
 * @package Choccybiccy\HumanApi
12
 */
13
class Application extends Api
14
{
15
16
    /**
17
     * @var string
18
     */
19
    protected $appKey;
20
21
    /**
22
     * @var string
23
     */
24
    protected $clientId;
25
26
    /**
27
     * Supported batch endpoints
28
     * @var array
29
     */
30
    protected $supportedBatchEndpoints = array(
31
        "activities",
32
        "activities/summaries",
33
        "heart_rates",
34
        "bmis",
35
        "body_fats",
36
        "heights",
37
        "weights",
38
        "blood_glucoses",
39
        "blood_oxygens",
40
        "sleeps",
41
        "blood_pressures",
42
        "genetic_traits",
43
        "locations",
44
        "food/meals",
45
    );
46
47
    /**
48
     * Constructor
49
     *
50
     * @param string $appKey HumanAPI app key
51
     * @param string $clientId HumanAPI client ID
52
     */
53
    public function __construct($appKey, $clientId)
54
    {
55
56
        $this->appKey = $appKey;
57
        $this->clientId = $clientId;
58
59
        parent::__construct();
60
61
    }
62
63
    /**
64
     * Batch get endpoint data
65
     *
66
     * @param string $endpoint
67
     * @param array $params
68
     * @return Collection
69
     * @throws UnsupportedBatchEndpointException
70
     */
71
    public function batch($endpoint, array $params = array())
72
    {
73
74
        if (!in_array($endpoint, $this->supportedBatchEndpoints)) {
75
            throw new UnsupportedBatchEndpointException("Batch endpoint '" . $endpoint . "' is not supported");
76
        }
77
78
        $response = $this->get(
79
            $this->buildUrlParts(array($this->clientId, "users", $endpoint), "apps"),
80
            array(
81
                "auth" => array(
82
                    $this->appKey,
83
                    ""
84
                ),
85
                "query" => $params
86
            )
87
        );
88
89
        $responseJson = json_decode($response->getBody(), true);
90
        return $this->buildCollection($responseJson);
91
92
    }
93
94
    /**
95
     * Get users
96
     *
97
     * @return Collection
98
     */
99 View Code Duplication
    public function getUsers()
100
    {
101
102
        $response = $this->get(
103
            $this->buildUrlParts(array($this->clientId, "users"), "apps"),
104
            array(
105
                "auth" => array(
106
                    $this->appKey,
107
                    ""
108
                )
109
            )
110
        );
111
112
        $responseJson = json_decode($response->getBody(), true);
113
        return $this->buildCollection($responseJson);
114
115
    }
116
117
    /**
118
     * Get user by human ID
119
     *
120
     * @param string $humanId Human ID
121
     * @return Model
122
     */
123 View Code Duplication
    public function getUserById($humanId)
124
    {
125
126
        $response = $this->get(
127
            $this->buildUrlParts(array($this->clientId, "users", $humanId), "apps"),
128
            array(
129
                "auth" => array(
130
                    $this->appKey,
131
                    ""
132
                )
133
            )
134
        );
135
136
        $responseJson = json_decode($response->getBody(), true);
137
        return new Model($responseJson, $this);
138
139
    }
140
141
    /**
142
     * Delete a user by human ID
143
     *
144
     * @param string $humanId Human ID
145
     * @return bool
146
     * @throws \Exception
147
     */
148
    public function deleteUserById($humanId)
149
    {
150
151
        try {
152
            $response = $this->delete(
153
                $this->buildUrlParts(array($this->clientId, "users", $humanId), "apps"),
154
                array(
155
                    "auth" => array(
156
                        $this->appKey,
157
                        ""
158
                    )
159
                )
160
            );
161
162
            if ($response->getStatusCode() != 204) {
163
                throw new UnexpectedResponseException(
164
                    "Couldn't delete the user, response from API was: "
165
                    . $response->getReasonPhrase()
166
                );
167
            }
168
169
            return true;
170
171
        } catch (RequestException $e) {
172
            throw new UnexpectedResponseException("Couldn't delete the user. Does the human ID exist?");
173
174
        }
175
176
    }
177
}
178