Passed
Push — master ( f13532...bf7dba )
by Andrea Marco
02:44 queued 12s
created

GuzzleAware::guzzle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Cerbero\JsonParser\Concerns;
4
5
use Cerbero\JsonParser\Exceptions\GuzzleRequiredException;
6
use GuzzleHttp\Client;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\UriInterface;
10
11
/**
12
 * The Guzzle-aware trait.
13
 *
14
 */
15
trait GuzzleAware
16
{
17
    /**
18
     * Abort if Guzzle is not loaded
19
     *
20
     * @return void
21
     * @throws GuzzleRequiredException
22
     */
23 5
    protected function requireGuzzle(): void
24
    {
25 5
        if (!$this->guzzleIsInstalled()) {
26 2
            throw new GuzzleRequiredException();
27
        }
28
    }
29
30
    /**
31
     * Determine whether Guzzle is installed
32
     *
33
     * @return bool
34
     */
35 3
    protected function guzzleIsInstalled(): bool
36
    {
37 3
        return class_exists(Client::class);
38
    }
39
40
    /**
41
     * Retrieve the JSON response of the given URL
42
     *
43
     * @param UriInterface|string $url
44
     * @return ResponseInterface
45
     */
46 1
    protected function getJson(UriInterface|string $url): ResponseInterface
47
    {
48 1
        return $this->guzzle()->get($url, [
49 1
            'headers' => [
50 1
                'Accept' => 'application/json',
51 1
                'Content-Type' => 'application/json',
52 1
            ],
53 1
        ]);
54
    }
55
56
    /**
57
     * Retrieve the Guzzle client
58
     *
59
     * @codeCoverageIgnore
60
     * @return Client
61
     */
62
    protected function guzzle(): Client
63
    {
64
        return new Client();
65
    }
66
67
    /**
68
     * Retrieve the JSON response of the given request
69
     *
70
     * @param RequestInterface $request
71
     * @return ResponseInterface
72
     */
73 2
    protected function sendRequest(RequestInterface $request): ResponseInterface
74
    {
75 2
        return $this->guzzle()->sendRequest($request);
76
    }
77
}
78