Passed
Pull Request — master (#9)
by
unknown
17:20
created

HttpClient   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 130
rs 10
c 0
b 0
f 0
wmc 12

8 Methods

Rating   Name   Duplication   Size   Complexity  
A sendRequest() 0 11 3
A setMethod() 0 11 2
A setUrl() 0 9 2
A setRequestParameters() 0 5 1
A getRequestParameters() 0 3 1
A getUrl() 0 3 1
A getMethod() 0 3 1
A __construct() 0 4 1
1
<?php
2
3
namespace Chuckbe\Chuckcms\Chuck\Matomo;
4
5
use GuzzleHttp\ClientInterface;
6
7
/**
8
 * A wrapper class that provides request options for the HTTP client.
9
 */
10
class HttpClient implements HttpClientInterface
11
{
12
13
    /**
14
     * The Guzzle HTTP client.
15
     *
16
     * @var \GuzzleHttp\ClientInterface
17
     */
18
    protected $httpClient;
19
20
    /**
21
     * The PSR7 request factory.
22
     *
23
     * @var \Chuckbe\Chuckcms\Chuck\Matomo\RequestFactoryInterface
24
     */
25
    protected $requestFactory;
26
27
    /**
28
     * The parameters to pass to the request.
29
     *
30
     * @var array
31
     */
32
    protected $requestParams = [];
33
34
    /**
35
     * The request method.
36
     *
37
     * @var string
38
     */
39
    protected $method = 'GET';
40
41
    /**
42
     * The request url.
43
     *
44
     * @var string;
45
     */
46
    protected $url;
47
48
    /**
49
     * Constructs a new HttpClient object.
50
     *
51
     * @param \GuzzleHttp\ClientInterface $httpClient
52
     *   The Guzzle HTTP client.
53
     * @param \Chuckbe\Chuckcms\Chuck\Matomo\RequestFactoryInterface $requestFactory
54
     *   The PSR7 request factory.
55
     */
56
    public function __construct(ClientInterface $httpClient, RequestFactoryInterface $requestFactory)
57
    {
58
        $this->httpClient = $httpClient;
59
        $this->requestFactory = $requestFactory;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function setRequestParameters(array $requestParams)
66
    {
67
        $this->requestParams = $requestParams;
68
69
        return $this;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getRequestParameters()
76
    {
77
        return $this->requestParams;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function getMethod()
84
    {
85
        return $this->method;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function setMethod($method)
92
    {
93
        // Currently, only GET and POST requests are supported.
94
        if (!in_array($method, ['GET', 'POST'], true)) {
95
            throw new \InvalidArgumentException(
96
                'Only GET and POST requests are allowed.'
97
            );
98
        }
99
        $this->method = $method;
100
101
        return $this;
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function getUrl()
108
    {
109
        return $this->url;
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function setUrl($url)
116
    {
117
        if (filter_var($url, FILTER_VALIDATE_URL) === false) {
118
            throw new \InvalidArgumentException('Invalid URL.');
119
        }
120
121
        $this->url = $url;
122
123
        return $this;
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function sendRequest()
130
    {
131
        if (empty($this->getUrl())) {
132
            throw new \Exception('Request url is not set.');
133
        }
134
135
        $request = $this->requestFactory->getRequest($this->getMethod(), $this->getUrl());
136
        $param_type = $this->getMethod() === 'GET' ? 'query' : 'form_params';
137
        $options = [$param_type => $this->getRequestParameters()];
138
139
        return $this->httpClient->send($request, $options);
140
    }
141
}
142