ReadWordsStorage   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
dl 0
loc 67
ccs 0
cts 27
cp 0
rs 10
c 1
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A doSearch() 0 12 2
A search() 0 34 3
A __construct() 0 3 1
A language() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Dictionary\Infrastructure\Repository\Elastic;
6
7
use App\Dictionary\Features\Languages\Storage\LanguageStorageInterface;
8
use App\Dictionary\Features\Languages\Storage\NotFoundSupportedLanguagesException;
9
use App\Dictionary\Features\WordsFinder\Mask\Mask;
10
use App\Dictionary\Features\WordsFinder\Storage\ReadWordsStorageInterface;
11
use App\Dictionary\Features\WordsFinder\Storage\WordNotFoundInStorageException;
12
use App\Dictionary\Features\WordsFinder\Word\Word;
13
use App\Dictionary\Features\WordsFinder\Word\WordDto;
14
use App\Dictionary\Features\WordsFinder\Word\WordDtoCollection;
15
use Elasticsearch\Client;
0 ignored issues
show
Bug introduced by
The type Elasticsearch\Client 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...
16
use Throwable;
17
18
final class ReadWordsStorage implements ReadWordsStorageInterface, LanguageStorageInterface
19
{
20
    private Client $client;
21
22
    public function __construct(ClientFactory $clientFactory)
23
    {
24
        $this->client = $clientFactory->create();
25
    }
26
27
    public function search(string $language, Mask $mask, int $limit): WordDtoCollection
28
    {
29
        $params = [
30
            'index' => $language,
31
            'body' => [
32
                'size' => $limit,
33
                'query' => [
34
                    'bool' => [
35
                        'must' => [
36
                            [
37
                                'regexp' => [
38
                                    'word' => $mask->query(),
39
                                ],
40
                            ],
41
                            [
42
                                'regexp' => [
43
                                    'word' => '.' . $mask->limit(),
44
                                ],
45
                            ],
46
                        ],
47
                    ],
48
                ],
49
            ],
50
        ];
51
52
        try {
53
            $collection = $this->doSearch($params, $limit);
54
            if ($collection->count()) {
55
                return $collection;
56
            }
57
58
            throw new WordNotFoundInStorageException((string) $mask, $language);
59
        } catch (Throwable) {
60
            throw new WordNotFoundInStorageException((string) $mask, $language);
61
        }
62
    }
63
64
    private function doSearch(array $params, int $limit): WordDtoCollection
65
    {
66
        $response = $this->client->search($params);
67
        shuffle($response['hits']['hits']);
68
69
        $words = [];
70
        $wordDtoCollection = new StorageWordDtoCollection(array_slice($response['hits']['hits'], 0, $limit));
71
        foreach ($wordDtoCollection as $word) {
72
            $words[] = new WordDto($word->language(), new Word($word->word(), $word->definition()));
73
        }
74
75
        return new WordDtoCollection(...$words);
76
    }
77
78
    public function language(): array
79
    {
80
        $indexes = $this->client->indices();
81
82
        $languages = array_keys($indexes->getSettings());
83
84
        return count($languages) ? $languages : throw new NotFoundSupportedLanguagesException();
85
    }
86
}
87