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.

WorkEtcClient   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 143
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A connect() 0 8 1
A login() 0 11 1
A registerDomain() 0 6 1
A invoke() 0 14 2
A buildUrl() 0 8 2
A checkErrors() 0 8 2
1
<?php
2
3
namespace WorkEtcClient;
4
5
/**
6
 * A thin HTTP client for WORK[etc]'s API.
7
 */
8
class WorkEtcClient
9
{
10
    /**
11
     * @var \WorkEtcClient\HttpInterface
12
     */
13
    protected $httpClient;
14
15
    /**
16
     * @var string
17
     */
18
    protected $domain = null;
19
20
    /**
21
     * @var string
22
     */
23
    protected $url = null;
24
25
    /**
26
     * @var string
27
     */
28
    protected $sessionKey = null;
29
30
    /**
31
     * @param \WorkEtcClient\HttpInterface
32
     */
33
    public function __construct(HttpInterface $httpClient)
34
    {
35
        $this->httpClient = $httpClient;
36
    }
37
38
    /**
39
     * Connect to WORK[etc] and return the connection.
40
     *
41
     * @param string $domain
42
     * @param string $email
43
     * @param string $password
44
     *
45
     * @return \WorkEtcClient\WorkEtcClient
46
     */
47
    public static function connect(string $domain, string $email, string $password): WorkEtcClient
48
    {
49
        $client = new static(new HttpfulClient);
50
51
        $client->login($domain, $email, $password);
52
53
        return $client;
54
    }
55
56
    /**
57
     * Authenticate into WORK[etc].
58
     *
59
     * @param string $domain
60
     * @param string $email
61
     * @param string $password
62
     *
63
     * @return void
64
     *
65
     * @throws \WorkEtcClient\WorkEtcException
66
     */
67
    public function login(string $domain, string $email, string $password)
68
    {
69
        $this->registerDomain($domain);
70
71
        $result = $this->invoke('AuthenticateWebSafe', [
72
            'email' => $email,
73
            'pass' => $password,
74
        ]);
75
76
        $this->sessionKey = $result['SessionKey'];
77
    }
78
79
    /**
80
     * @param string $domain
81
     *
82
     * @return void
83
     */
84
    protected function registerDomain(string $domain)
85
    {
86
        $this->domain = $domain;
87
88
        $this->url = 'https://' . $domain . '.worketc.com';
89
    }
90
91
    /**
92
     * Invoke the given method and return the result.
93
     *
94
     * @param string $endpoint
95
     * @param array  $params
96
     *
97
     * @return array|string
98
     *
99
     * @throws \WorkEtcClient\WorkEtcException
100
     */
101
    public function invoke(string $endpoint, array $params = [])
102
    {
103
        $url = $this->buildUrl($endpoint);
104
105
        $response = $this->httpClient->post($url, $params);
106
107
        $this->checkErrors($response);
0 ignored issues
show
Bug introduced by
It seems like $response defined by $this->httpClient->post($url, $params) on line 105 can also be of type string; however, WorkEtcClient\WorkEtcClient::checkErrors() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
108
109
        // Account for WORK[etc]'s random addition of a top-level associative
110
        // array
111
        $response = isset($response['d']) ? $response['d'] : $response;
112
113
        return $response;
114
    }
115
116
    /**
117
     * Build the URL from the registered domain and the endpoint.
118
     *
119
     * @param string $endpoint
120
     *
121
     * @return string
122
     */
123
    protected function buildUrl(string $endpoint): string
124
    {
125
        if ($this->sessionKey !== null) {
126
            $endpoint = $endpoint . '?VeetroSession=' . $this->sessionKey;
127
        }
128
129
        return $this->url . '/json/' . $endpoint;
130
    }
131
132
    /**
133
     * Check if the WORK[etc] response had errors and attempts to parse them if
134
     * so.
135
     *
136
     * @param array $response
137
     *
138
     * @return void
139
     *
140
     * @throws \WorkEtcClient\WorkEtcException if the HTTP response code is 400 or above.
141
     */
142
    protected function checkErrors(array $response)
143
    {
144
        if ($this->httpClient->hasErrors() === false) {
145
            return;
146
        }
147
148
        throw new WorkEtcException($response);
149
    }
150
}
151