Passed
Push — master ( d13d8d...dbf2b1 )
by Karel
11:09 queued 11s
created

HttpClient::getMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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