Passed
Push — master ( 3581f7...005bcc )
by De Cramer
05:10 queued 03:31
created

SimpleHttpOperation::processData()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 19
rs 9.8666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Oliverde8\Component\PhpEtl\ChainOperation\Transformer;
6
7
use oliverde8\AssociativeArraySimplified\AssociativeArray;
8
use Oliverde8\Component\PhpEtl\ChainOperation\AbstractChainOperation;
9
use Oliverde8\Component\PhpEtl\ChainOperation\DataChainOperationInterface;
10
use Oliverde8\Component\PhpEtl\Item\AsyncHttpClientResponseItem;
11
use Oliverde8\Component\PhpEtl\Item\DataItem;
12
use Oliverde8\Component\PhpEtl\Item\DataItemInterface;
13
use Oliverde8\Component\PhpEtl\Item\ItemInterface;
14
use Oliverde8\Component\PhpEtl\Model\ExecutionContext;
15
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
16
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...
17
18
class SimpleHttpOperation extends AbstractChainOperation implements DataChainOperationInterface
19
{
20
    private HttpClientInterface $client;
21
22
    private string $method = "GET";
23
    private string $url;
24
25
    private bool $responseIsJson;
26
27
    private ?string $optionsKey;
28
29
    protected ?string $responseKey;
30
31
    private ExpressionLanguage $expressionLanguage;
32
33
    public function __construct(
34
        HttpClientInterface $client,
35
        string $method,
36
        string $url,
37
        bool $responseIsJson,
38
        ?string $optionsKey,
39
        ?string $responseKey
40
    ) {
41
        $this->client = $client;
42
        $this->method = $method;
43
        $this->url = $url;
44
        $this->responseIsJson = $responseIsJson;
45
        $this->optionsKey = $optionsKey;
46
        $this->responseKey = $responseKey;
47
48
        $this->expressionLanguage = new ExpressionLanguage();
49
    }
50
51
52
    public function processData(DataItemInterface $item, ExecutionContext $context): ItemInterface
53
    {
54
        $data = $item->getData();
55
        if ($this->optionsKey) {
56
            $options = AssociativeArray::getFromKey($data, $this->optionsKey, []);
57
        } else {
58
            $options = $data;
59
        }
60
61
        $url = $this->url;
62
        if (strpos($url, "@") === 0) {
63
            $url = ltrim($url, '@');
64
            $url = $this->expressionLanguage->evaluate($url, ['data' => $data]);
65
        }
66
67
        $response = $this->client->request($this->method, $url, $options);
68
        $response->getInfo();
69
70
        return new AsyncHttpClientResponseItem($this->client, $response, $this->responseIsJson, $this->responseKey, $data);
71
    }
72
}
73