PopulateCommand   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 55
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
A __construct() 0 7 1
A validateInput() 0 13 3
A doExecute() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Dictionary\Features\PopulateStorage\Populate\Console;
6
7
use App\Dictionary\Features\PopulateStorage\Populate\FileAssert;
8
use App\Dictionary\Features\PopulateStorage\Populate\WordsStoragePopulate;
9
use App\Dictionary\Features\PopulateStorage\Populate\WordsStoragePopulateCriteria;
10
use RuntimeException;
11
use Symfony\Component\Console\Input\InputArgument;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputArgument 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...
12
use Symfony\Component\Console\Input\InputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputInterface 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...
13
use Symfony\Component\Console\Input\InputOption;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputOption 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...
14
15
final class PopulateCommand extends AbstractCommand
16
{
17
    protected static $defaultName = 'dictionary:populate';
18
19
    private string $filePath;
20
    private array $dictionaryList;
21
    private WordsStoragePopulate $wordsStoragePopulate;
22
23
    public function __construct(WordsStoragePopulate $wordsStoragePopulate, array $dictionaryList)
24
    {
25
        parent::__construct();
26
27
        $this->filePath = '';
28
        $this->wordsStoragePopulate = $wordsStoragePopulate;
29
        $this->dictionaryList = $dictionaryList;
30
    }
31
32
    protected function configure(): void
33
    {
34
        $this->setDescription('Populate a new words to the storage.');
35
        $this->addArgument('language', InputArgument::REQUIRED, 'Language code');
36
        $this->addOption('file-path', null, InputOption::VALUE_OPTIONAL, 'File path');
37
        $this->setHelp(
38
            <<<HELP
39
The command populate a new words to the storage 
40
    <info>php %command.full_name% ua</info>
41
    <info>php %command.full_name% ua --file-path=file.txt</info>
42
HELP
43
        );
44
    }
45
46
    protected function doExecute(InputInterface $input): string
47
    {
48
        $criteria = new WordsStoragePopulateCriteria((string) $input->getArgument('language'), $this->filePath);
49
        $count = $this->wordsStoragePopulate->execute($criteria);
50
51
        return sprintf('Populate %s words.', $count);
52
    }
53
54
    /**
55
     * @psalm-suppress PossiblyInvalidCast
56
     */
57
    protected function validateInput(InputInterface $input): void
58
    {
59
        if ($this->filePath = (string) $input->getOption('file-path')) {
60
            FileAssert::assertTxtFile((string) $input->getOption('file-path'));
61
62
            return;
63
        }
64
65
        if (!array_key_exists((string) $input->getArgument('language'), $this->dictionaryList)) {
66
            throw new RuntimeException('Dictionary is not found for words populate.');
67
        }
68
69
        $this->filePath = $this->dictionaryList[(string) $input->getArgument('language')];
70
    }
71
}
72