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

Psr7Request   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 50%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 44
ccs 4
cts 8
cp 0.5
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A calculateSize() 0 3 1
A matches() 0 3 1
A getIterator() 0 7 1
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