Endpoint   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 66
ccs 11
cts 11
cp 1
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getIterator() 0 3 1
A matches() 0 4 3
A calculateSize() 0 3 1
A response() 0 5 1
A fetchResponse() 0 3 1
1
<?php
2
3
namespace Cerbero\JsonParser\Sources;
4
5
use Cerbero\JsonParser\Concerns\DetectsEndpoints;
6
use Cerbero\JsonParser\Concerns\GuzzleAware;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\UriInterface;
9
use Traversable;
10
11
use function is_string;
12
13
/**
14
 * The endpoint source.
15
 *
16
 * @property-read UriInterface|string $source
17
 */
18
class Endpoint extends Source
19
{
20
    use DetectsEndpoints;
21
    use GuzzleAware;
22
23
    /**
24
     * The endpoint response.
25
     *
26
     * @var ResponseInterface|null
27
     */
28
    protected ?ResponseInterface $response;
29
30
    /**
31
     * Retrieve the JSON fragments
32
     *
33
     * @return Traversable<int, string>
34
     * @throws \Cerbero\JsonParser\Exceptions\GuzzleRequiredException
35
     */
36 5
    public function getIterator(): Traversable
37
    {
38 5
        return new Psr7Message($this->response(), $this->config);
39
    }
40
41
    /**
42
     * Retrieve the endpoint response
43
     *
44
     * @return ResponseInterface
45
     * @throws \Cerbero\JsonParser\Exceptions\GuzzleRequiredException
46
     */
47 5
    protected function response(): ResponseInterface
48
    {
49 5
        $this->requireGuzzle();
50
51 3
        return $this->response ??= $this->fetchResponse();
52
    }
53
54
    /**
55
     * Retrieve the fetched HTTP response
56
     *
57
     * @return ResponseInterface
58
     */
59 1
    protected function fetchResponse(): ResponseInterface
60
    {
61 1
        return $this->getJson($this->source);
62
    }
63
64
    /**
65
     * Determine whether the JSON source can be handled
66
     *
67
     * @return bool
68
     */
69 348
    public function matches(): bool
70
    {
71
        /** @phpstan-ignore-next-line */
72 348
        return (is_string($this->source) || $this->source instanceof UriInterface) && $this->isEndpoint($this->source);
73
    }
74
75
    /**
76
     * Retrieve the calculated size of the JSON source
77
     *
78
     * @return int|null
79
     * @throws \Cerbero\JsonParser\Exceptions\GuzzleRequiredException
80
     */
81 3
    protected function calculateSize(): ?int
82
    {
83 3
        return $this->response()->getBody()->getSize();
84
    }
85
}
86