Client::get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Polidog\Chatwork\Client;
6
7
use GuzzleHttp\ClientInterface as HttpClientInterface;
8
use GuzzleHttp\Exception\GuzzleException;
9
use GuzzleHttp\Middleware;
10
use Polidog\Chatwork\Exception\ClientException;
11
12
final class Client implements ClientInterface
13
{
14
    /**
15
     * @var string
16
     */
17
    private $apiVersion;
18
19
    /**
20
     * @var HttpClientInterface
21
     */
22
    private $httpClient;
23
24
    /**
25
     * @param string              $chatworkToken
26
     * @param HttpClientInterface $httpClient
27
     */
28
    public function __construct(
29
        string $chatworkToken,
30
        string $apiVersion,
31
        ?HttpClientInterface $httpClient = null,
32
        array $httpOptions = []
33
    ) {
34
        if ($httpClient === null) {
35
            $httpClient = ClientFactory::createHttpClient($chatworkToken, [], $httpOptions);
0 ignored issues
show
Unused Code introduced by
The call to Polidog\Chatwork\Client\...ory::createHttpClient() has too many arguments starting with $httpOptions. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
            /** @scrutinizer ignore-call */ 
36
            $httpClient = ClientFactory::createHttpClient($chatworkToken, [], $httpOptions);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
36
        }
37
        $this->apiVersion = $apiVersion;
38
        $this->httpClient = $httpClient;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function get(string $path, array $query = []): array
45
    {
46
        return $this->request('get', $path, [
47
            'query' => $query,
48
        ]);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function post(string $path, array $data = []): array
55
    {
56
        return $this->request('post', $path, [
57
            'form_params' => $data,
58
        ]);
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function put(string $path, array $data = []): array
65
    {
66
        return $this->request('put', $path, [
67
            'form_params' => $data,
68
        ]);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function delete(string $path, array $query = []): array
75
    {
76
        return $this->request('delete', $path, [
77
            'query' => $query,
78
        ]);
79
    }
80
81
    private function request(string $method, string $path, array $options = []): array
82
    {
83
        $path = sprintf('/%s/%s', $this->apiVersion, $path);
84
        try {
85
            return json_decode($this->httpClient->request($method, $path, $options)->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);
86
        } catch (GuzzleException $e) {
87
            throw new ClientException(sprintf('request error. method = %s, path = %s', $method, $path), $e->getCode(), $e);
88
        } catch (\JsonException $e) {
89
            throw new ClientException(sprintf('json parse error. method = %s, path = %s', $method, $path), $e->getCode(), $e);
90
        }
91
    }
92
}
93