Passed
Branch feature/first-release (668b10)
by Andrea Marco
02:52
created

Endpoint::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Cerbero\LazyJson\Handlers;
4
5
use Cerbero\LazyJson\Concerns\EndpointAware;
6
use GuzzleHttp\Client;
7
use Traversable;
8
9
/**
10
 * The endpoint handler.
11
 *
12
 */
13
class Endpoint extends Psr7Message
14
{
15
    use EndpointAware;
16
17
    /**
18
     * The HTTP client.
19
     *
20
     * @var Client
21
     */
22
    private $client;
23
24
    /**
25
     * Instantiate the class.
26
     *
27
     * @param Client $client
28
     */
29 12
    public function __construct(Client $client = null)
30
    {
31 12
        if ($client === null && class_exists(Client::class)) {
32 10
            $client = new Client();
33
        }
34
35 12
        $this->client = $client;
36 12
    }
37
38
    /**
39
     * Determine whether the handler can handle the given source
40
     *
41
     * @param mixed $source
42
     * @return bool
43
     */
44 10
    public function handles($source): bool
45
    {
46 10
        return $this->isEndpoint($source) && class_exists(Client::class);
47
    }
48
49
    /**
50
     * Handle the given source
51
     *
52
     * @param mixed $source
53
     * @param string $path
54
     * @return Traversable
55
     */
56 2
    public function handle($source, string $path): Traversable
57
    {
58 2
        $response = $this->client->get($source, [
59
            'headers' => [
60 2
                'Accept' => 'application/json',
61
                'Content-Type' => 'application/json',
62
            ],
63
        ]);
64
65 2
        return parent::handle($response, $path);
66
    }
67
}
68