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

Psr7Request::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\GuzzleAware;
6
use Psr\Http\Message\RequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use Traversable;
9
10
/**
11
 * The PSR-7 request source.
12
 *
13
 * @property-read RequestInterface $source
14
 */
15
class Psr7Request extends Source
16
{
17
    use GuzzleAware;
18
19
    /**
20
     * The endpoint response.
21
     *
22
     * @var ResponseInterface|null
23
     */
24
    protected ?ResponseInterface $response;
25
26
    /**
27
     * Retrieve the JSON fragments
28
     *
29
     * @return Traversable<int, string>
30
     * @throws \Cerbero\JsonParser\Exceptions\GuzzleRequiredException
31
     */
32 1
    public function getIterator(): Traversable
33
    {
34 1
        $this->requireGuzzle();
35
36
        $this->response = $this->sendRequest($this->source);
37
38
        return new Psr7Message($this->response, $this->config);
39
    }
40
41
    /**
42
     * Determine whether the JSON source can be handled
43
     *
44
     * @return bool
45
     */
46 1
    public function matches(): bool
47
    {
48 1
        return $this->source instanceof RequestInterface;
49
    }
50
51
    /**
52
     * Retrieve the calculated size of the JSON source
53
     *
54
     * @return int|null
55
     */
56
    protected function calculateSize(): ?int
57
    {
58
        return $this->response?->getBody()->getSize();
59
    }
60
}
61