Completed
Push — master ( 06f814...f23c49 )
by Tobias
03:33
created

HttpClient::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Happyr\GoogleAnalyticsBundle\Http;
4
5
use Http\Client\HttpClient as HttplugClient;
6
use Http\Message\MessageFactory;
7
use Psr\Http\Client\ClientInterface;
8
use Psr\Http\Message\RequestFactoryInterface;
9
10
/**
11
 * This is an adapter for Httplug.
12
 *
13
 * @author Tobias Nyholm <[email protected]>
14
 */
15
class HttpClient implements AnalyticsClientInterface
16
{
17
    /**
18
     * @var string endpoint
19
     *
20
     * Where to POST the requests
21
     */
22
    private $endpoint;
23
24
    /**
25
     * @var ClientInterface
26
     */
27
    private $client;
28
29
    /**
30
     * @var RequestFactoryInterface
31
     */
32
    private $requestFactory;
33
34
35 1
    public function __construct(ClientInterface $client, RequestFactoryInterface $requestFactory, string $endpoint)
36
    {
37 1
        $this->endpoint = $endpoint;
38 1
        $this->client = $client;
39 1
        $this->requestFactory = $requestFactory;
40 1
    }
41
42
    /**
43
     * Send a post request to the endpoint.
44
     */
45
    public function send(array $data = []): bool
46
    {
47
        $request = $this->requestFactory->createRequest('POST', $this->endpoint);
48
        $request = $request->withAddedHeader('User-Agent', 'happyr-google-analytics/5.0');
49
        $request->getBody()->write(http_build_query($data));
50
        $response = $this->client->sendRequest($request);
51
52
        return $response->getStatusCode() === 200;
53
    }
54
}
55