Passed
Branch feature/first-release (43e0cc)
by Andrea Marco
10:48
created

Endpoint::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.125

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
ccs 2
cts 4
cp 0.5
crap 1.125
rs 10
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 1
    public function getIterator(): Traversable
37
    {
38 1
        $this->requireGuzzle();
39
40
        $this->response = $this->getJson($this->source);
41
42
        return new Psr7Message($this->response, $this->config);
43
    }
44
45
    /**
46
     * Determine whether the JSON source can be handled
47
     *
48
     * @return bool
49
     */
50 128
    public function matches(): bool
51
    {
52
        // @phpstan-ignore-next-line
53 128
        return (is_string($this->source) || $this->source instanceof UriInterface) && $this->isEndpoint($this->source);
54
    }
55
56
    /**
57
     * Retrieve the calculated size of the JSON source
58
     *
59
     * @return int|null
60
     */
61
    protected function calculateSize(): ?int
62
    {
63
        return $this->response?->getBody()->getSize();
64
    }
65
}
66