|
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; |
|
|
|
|
|
|
10
|
|
|
use Psr\Http\Client\ClientInterface; |
|
|
|
|
|
|
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
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths