WordsStoragePopulate::wordProcessing()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 5
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Dictionary\Features\PopulateStorage\Populate;
6
7
use App\Dictionary\Features\PopulateStorage\FileReader\FileReaderInterface;
8
use App\Dictionary\Features\PopulateStorage\Populate\Message\SearchWordDefinitionMessage;
9
use Psr\Log\LoggerInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Log\LoggerInterface 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 Symfony\Component\Messenger\MessageBusInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Messenger\MessageBusInterface 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 WordsStoragePopulate
14
{
15
    private const MIN_WORD_LENGTH = 3;
16
17
    private LoggerInterface $logger;
18
    private FileReaderInterface $fileReader;
19
    private MessageBusInterface $messageBus;
20
21
    public function __construct(
22
        FileReaderInterface $fileReader,
23
        MessageBusInterface $messageBus,
24
        LoggerInterface $logger
25
    ) {
26
        $this->logger = $logger;
27
        $this->fileReader = $fileReader;
28
        $this->messageBus = $messageBus;
29
    }
30
31 1
    public function execute(WordsStoragePopulateCriteria $criteria): int
32
    {
33
        try {
34 1
            return $this->doExecute($criteria);
35
        } catch (Throwable $exception) {
36
            $this->logger->error($exception->getMessage());
37
        }
38
39
        return 0;
40
    }
41
42
    private function doExecute(WordsStoragePopulateCriteria $criteria): int
43
    {
44
        $count = 0;
45
        foreach ($this->fileReader->read($criteria->filePath()) as $row) {
46
            if (null !== $word = $this->wordProcessing((string) $row)) {
47
                $this->messageBus->dispatch(new SearchWordDefinitionMessage($word, $criteria->language()));
48
                $count++;
49
            }
50
        }
51
52
        return $count;
53
    }
54
55
    private function wordProcessing(string $word): ?string
56
    {
57
        $word = mb_strtolower(str_replace('\r\n', '', trim($word)));
58
        if (strlen($word) < self::MIN_WORD_LENGTH) {
59
            return null;
60
        }
61
62
        return $word;
63
    }
64
}
65