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

Endpoint::handleSource()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 13
ccs 0
cts 5
cp 0
rs 10
cc 2
nc 2
nop 0
crap 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A Endpoint::handles() 0 3 2
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