WordDefinitionWikipediaApiGateway::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 8
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Dictionary\Infrastructure\Gateway\Wikipedia;
6
7
use App\Dictionary\Infrastructure\Gateway\AbstractWordDefinition;
8
use App\Dictionary\Infrastructure\HttpClient\ResponseDataExtractorInterface;
9
use GuzzleHttp\Psr7\Request;
0 ignored issues
show
Bug introduced by
The type GuzzleHttp\Psr7\Request 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
use Psr\Http\Client\ClientInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Client\ClientInterface 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...
11
use Throwable;
12
13
final class WordDefinitionWikipediaApiGateway extends AbstractWordDefinition
14
{
15
    private const URI = 'https://%s.%s?action=query&format=json&titles=%s&prop=extracts&exintro&explaintext';
16
17
    private string $wikipediaApiHost;
18
    private ClientInterface $client;
19
    private ResponseDataExtractorInterface $responseDataExtractor;
20
21
    public function __construct(
22
        ClientInterface $client,
23
        ResponseDataExtractorInterface $responseDataExtractor,
24
        string $wikipediaApiHost
25
    ) {
26
        $this->wikipediaApiHost = $wikipediaApiHost;
27
        $this->client = $client;
28
        $this->responseDataExtractor = $responseDataExtractor;
29
    }
30
31
    public function search(string $word, string $language): string
32
    {
33
        $uri = sprintf(self::URI, $language, $this->wikipediaApiHost, $word);
34
        try {
35
            $response = $this->client->sendRequest(new Request('GET', $uri));
36
            $payload = $this->responseDataExtractor->extract($response);
37
            $wikipediaWordDefinitionDto = new WikipediaWordDefinitionDto($payload);
38
39
            return null === $wikipediaWordDefinitionDto->word() ?
40
                parent::search($word, $language) :
41
                str_replace($word, '___', $wikipediaWordDefinitionDto->word());
42
        } catch (Throwable) {
43
            return parent::search($word, $language);
44
        }
45
    }
46
}
47