AsyncHttpClientResponseItem   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 69
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isRunning() 0 12 4
A __construct() 0 12 1
A getItem() 0 19 3
1
<?php
2
3
namespace Oliverde8\Component\PhpEtl\Item;
4
5
use oliverde8\AssociativeArraySimplified\AssociativeArray;
6
use Symfony\Component\HttpClient\Exception\TimeoutException;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpCl...eption\TimeoutException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Symfony\Component\HttpClient\Response\AsyncResponse;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpCl...\Response\AsyncResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Symfony\Contracts\HttpClient\HttpClientInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Contracts\HttpClient\HttpClientInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Symfony\Contracts\HttpClient\ResponseInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Contracts\HttpClient\ResponseInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
11
class AsyncHttpClientResponseItem implements AsyncItemInterface
12
{
13
    private HttpClientInterface $client;
14
15
    private ResponseInterface $response;
16
17
    private bool $responseIsJson;
18
19
    private ?string $responseKey;
20
21
    private array $baseData;
22
23
    private bool $isRunning = true;
0 ignored issues
show
introduced by
The private property $isRunning is not used, and could be removed.
Loading history...
24
25
    /**
26
     * @param ResponseInterface $response
27
     * @param bool $responseIsJson
28
     * @param string|null $responseKey
29
     * @param array $baseData
30
     */
31
    public function __construct(
32
        HttpClientInterface $client,
33
        ResponseInterface $response,
34
        bool $responseIsJson,
35
        ?string $responseKey,
36
        array $baseData
37
    ) {
38
        $this->client = $client;
39
        $this->response = $response;
40
        $this->responseIsJson = $responseIsJson;
41
        $this->responseKey = $responseKey;
42
        $this->baseData = $baseData;
43
    }
44
45
46
    public function isRunning(): bool
47
    {
48
        try {
49
            foreach ($this->client->stream($this->response, 0.01) as $chunk) {
50
                if ($chunk->isLast()) {
51
                    return false;
52
                }
53
            }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return boolean. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
54
        } catch (TimeoutException $exception) {
55
            // This is normal, we have used a very low stream timeout because we wish to continue processing
56
            // other items while this item is being downloaded.
57
            return true;
58
        }
59
    }
60
61
    public function getItem(): ItemInterface
62
    {
63
        $responseData = [
64
            'content' => $this->response->getContent(),
65
            'headers' => $this->response->getHeaders(),
66
            'status_code' => $this->response->getStatusCode(),
67
        ];
68
        if ($this->responseIsJson) {
69
            $responseData['content'] = $this->response->toArray();
70
        }
71
72
        $data = $this->baseData;
73
        if ($this->responseKey) {
74
            AssociativeArray::setFromKey($data, $this->responseKey, $responseData);
75
        } else {
76
            $data = $responseData;
77
        }
78
79
        return new DataItem($data);
80
    }
81
}
82