Passed
Branch feature/first-release (aef455)
by Andrea Marco
01:55
created

Psr7Request::response()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 2
cts 3
cp 0.6667
crap 1.037
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
        return new Psr7Message($this->response(), $this->config);
35
    }
36
37
    /**
38
     * Retrieve the response of the PSR-7 request
39
     *
40
     * @return ResponseInterface
41
     * @throws \Cerbero\JsonParser\Exceptions\GuzzleRequiredException
42
     */
43 1
    protected function response(): ResponseInterface
44
    {
45 1
        $this->requireGuzzle();
46
47
        return $this->response ??= $this->sendRequest($this->source);
48
    }
49
50
    /**
51
     * Determine whether the JSON source can be handled
52
     *
53
     * @return bool
54
     */
55 1
    public function matches(): bool
56
    {
57 1
        return $this->source instanceof RequestInterface;
58
    }
59
60
    /**
61
     * Retrieve the calculated size of the JSON source
62
     *
63
     * @return int|null
64
     */
65
    protected function calculateSize(): ?int
66
    {
67
        return $this->response()->getBody()->getSize();
68
    }
69
}
70