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.

HttplugConnection   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 5
dl 0
loc 79
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A send() 0 21 4
B getUrlParamString() 0 22 6
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * (c) Christian Gripp <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Core23\MatomoBundle\Connection;
13
14
use Core23\MatomoBundle\Exception\MatomoException;
15
use DateTime;
16
use Exception;
17
use Http\Client\Exception as ClientException;
18
use Http\Client\HttpClient;
19
use Http\Message\MessageFactory;
20
21
final class HttplugConnection implements ConnectionInterface
22
{
23
    /**
24
     * @var HttpClient
25
     */
26
    private $client;
27
28
    /**
29
     * @var MessageFactory
30
     */
31
    private $messageFactory;
32
33
    /**
34
     * @var string
35
     */
36
    private $apiUrl;
37
38
    /**
39
     * Initialize client.
40
     *
41
     * @param string $apiUrl base API URL
42
     */
43
    public function __construct(HttpClient $client, MessageFactory $messageFactory, string $apiUrl)
44
    {
45
        $this->apiUrl         = $apiUrl;
46
        $this->client         = $client;
47
        $this->messageFactory = $messageFactory;
48
    }
49
50
    public function send(array $params = []): string
51
    {
52
        $params['module'] = 'API';
53
54
        $url      = $this->apiUrl.'?'.$this->getUrlParamString($params);
55
        $request  = $this->messageFactory->createRequest('GET', $url);
56
57
        try {
58
            $response = $this->client->sendRequest($request);
59
        } catch (ClientException $exception) {
60
            throw new MatomoException('Error calling Matomo API.', $exception->getCode(), $exception);
61
        } catch (Exception $exception) {
62
            throw new MatomoException('Error calling Matomo API.', 500, $exception);
63
        }
64
65
        if (200 !== $response->getStatusCode()) {
66
            throw new MatomoException(sprintf('"%s" returned an invalid status code: "%s"', $url, $response->getStatusCode()));
67
        }
68
69
        return $response->getBody()->getContents();
70
    }
71
72
    /**
73
     * Converts a set of parameters to a query string.
74
     *
75
     * @return string query string
76
     */
77
    private function getUrlParamString(array $params): string
78
    {
79
        $query = [];
80
        foreach ($params as $key => $val) {
81
            if (\is_array($val)) {
82
                $val = implode(',', $val);
83
            } elseif ($val instanceof DateTime) {
84
                $val = $val->format('Y-m-d');
85
            } elseif (\is_bool($val)) {
86
                if ($val) {
87
                    $val = 1;
88
                } else {
89
                    continue;
90
                }
91
            } else {
92
                $val = urlencode((string) $val);
93
            }
94
            $query[] = $key.'='.$val;
95
        }
96
97
        return implode('&', $query);
98
    }
99
}
100