Endpoint::guzzleIsLoaded()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 Cerbero\LazyJson\Exceptions\LazyJsonException;
7
use GuzzleHttp\Client;
8
use Traversable;
9
10
/**
11
 * The endpoint handler.
12
 *
13
 */
14
class Endpoint extends Psr7Message
15
{
16
    use EndpointAware;
17
18
    /**
19
     * Determine whether the handler can handle the given source
20
     *
21
     * @param mixed $source
22
     * @return bool
23
     */
24 10
    public function handles($source): bool
25
    {
26 10
        return $this->isEndpoint($source);
27
    }
28
29
    /**
30
     * Handle the given source
31
     *
32
     * @param mixed $source
33
     * @param string $path
34
     * @return Traversable
35
     */
36 3
    public function handle($source, string $path): Traversable
37
    {
38 3
        if (!$this->guzzleIsLoaded()) {
39 1
            throw new LazyJsonException('Guzzle is required to load JSON from endpoints');
40
        }
41
42 2
        $response = (new Client())->get($source, [
43
            'headers' => [
44 2
                'Accept' => 'application/json',
45
                'Content-Type' => 'application/json',
46
            ],
47
        ]);
48
49 2
        return parent::handle($response, $path);
50
    }
51
52
    /**
53
     * Determine whether Guzzle is loaded, useful for testing
54
     *
55
     * @return bool
56
     */
57 2
    protected function guzzleIsLoaded(): bool
58
    {
59 2
        return class_exists(Client::class);
60
    }
61
}
62