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

Endpoint   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 53
ccs 11
cts 11
cp 1
rs 10
c 1
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handles() 0 3 2
A handle() 0 10 1
A __construct() 0 7 3
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