Completed
Pull Request — master (#1)
by Patryk
10:48
created

GuzzleClient::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace KaLLoSz\Twig\Extension\Client;
4
5
use GuzzleHttp\ClientInterface;
6
use KaLLoSz\Twig\Extension\IframelyClientInterface;
7
use KaLLoSz\Twig\Extension\IframelyDTO;
8
9
/**
10
 * Class GuzzleClient
11
 * @package KaLLoSz\Twig\Extension\Client
12
 *
13
 * @author Patryk Kala <[email protected]>
14
 */
15
class GuzzleClient implements IframelyClientInterface
16
{
17
    /**
18
     * @var null|string
19
     */
20
    private $apiKey;
21
22
    /**
23
     * @var ClientInterface
24
     */
25
    private $client;
26
27
    /**
28
     * Class constructor.
29
     *
30
     * @param string|null $apiKey API key.
31
     * @param ClientInterface $client Guzzle Client
32
     */
33
    public function __construct(string $apiKey, ClientInterface $client)
34
    {
35
        $this->apiKey = $apiKey;
36
        $this->client = $client;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getUrlData(string $url): IframelyDTO
43
    {
44
        $response = $this->client->request(
45
            'GET',
46
            IframelyClientInterface::API_BASE_URI,
47
            ['query' => ['api_key' => $this->apiKey, 'url' => $url, 'html' => 1]]
48
        );
49
50
        return new IframelyDTO(json_decode($response->getBody()->getContents(), true));
51
    }
52
}
53