ListModelsCommand::groupModelsByFirstLetter()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 14
rs 9.9666
c 0
b 0
f 0
ccs 9
cts 9
cp 1
crap 3
1
<?php
2
/**
3
 * File: ListModelsCommand.php
4
 *
5
 * @author      Maciej Sławik <[email protected]>
6
 * Github:      https://github.com/maciejslawik
7
 */
8
9
namespace MSlwk\Otomoto\Cli\Command;
10
11
use MSlwk\Otomoto\App\Manufacturer\Data\ManufacturerDTO;
12
use MSlwk\Otomoto\App\Model\Data\ModelDTOArray;
13
use MSlwk\Otomoto\Middleware\App\Model\ModelFactory;
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
/**
20
 * Class ListModelsCommand
21
 * @package MSlwk\Otomoto\Cli\Command
22
 */
23
class ListModelsCommand extends Command
24
{
25
    const COMMAND_NAME = 'app:manufacturer-models';
26
    const COMMAND_DESC = 'List all available models for a manufacturer';
27
28
    const MANUFACTURER_ARG_NAME = 'manufacturer';
29
    const MANUFACTURER_ARG_DESC = 'Full manufacturer name';
30
31
    /**
32
     * @var ModelFactory
33
     */
34
    private $modelMiddlewareFactory;
35
36
    /**
37
     * ListModelsCommand constructor.
38
     * @param ModelFactory|null $modelMiddlewareFactory
39
     * @param null $name
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $name is correct as it would always require null to be passed?
Loading history...
40
     */
41 6
    public function __construct(ModelFactory $modelMiddlewareFactory = null, $name = null)
42
    {
43 6
        parent::__construct($name);
44 6
        $this->modelMiddlewareFactory = $modelMiddlewareFactory ?? new ModelFactory();
45 6
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50 6
    protected function configure()
51
    {
52 6
        $this->setName(self::COMMAND_NAME)
53 6
            ->setDescription(self::COMMAND_DESC)
54 6
            ->addArgument(
55 6
                self::MANUFACTURER_ARG_NAME,
56 6
                InputArgument::REQUIRED,
57 6
                self::MANUFACTURER_ARG_DESC
58
            );
59
60 6
        parent::configure();
61 6
    }
62
63
    /**
64
     * @param InputInterface $input
65
     * @param OutputInterface $output
66
     * @return void
67
     */
68 2
    protected function execute(InputInterface $input, OutputInterface $output)
69
    {
70 2
        $manufacturer = new ManufacturerDTO($input->getArgument(self::MANUFACTURER_ARG_NAME));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument(self::MANUFACTURER_ARG_NAME) can also be of type null and string[]; however, parameter $name of MSlwk\Otomoto\App\Manufa...turerDTO::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
        $manufacturer = new ManufacturerDTO(/** @scrutinizer ignore-type */ $input->getArgument(self::MANUFACTURER_ARG_NAME));
Loading history...
71 2
        $modelMiddleware = $this->modelMiddlewareFactory->create();
72 2
        $models = $modelMiddleware->getModels($manufacturer);
73
74 2
        foreach ($this->groupModelsByFirstLetter($models) as $model) {
75 2
            $output->writeln("<info>{$model}</info>");
76
        }
77 2
    }
78
79
    /**
80
     * @param ModelDTOArray $modelDTOArray
81
     * @return array
82
     */
83 3
    private function groupModelsByFirstLetter(ModelDTOArray $modelDTOArray): array
84
    {
85 3
        $modelsNames = [];
86 3
        foreach ($modelDTOArray as $modelDTO) {
87 3
            $firstLetter = substr($modelDTO->getName(), 0, 1);
88 3
            if (!isset($modelsNames[$firstLetter])) {
89 3
                $modelsNames[$firstLetter] = "{$firstLetter}: {$modelDTO->getName()}";
90
            } else {
91 3
                $modelsNames[$firstLetter] .= ", {$modelDTO->getName()}";
92
            }
93
        }
94
95 3
        ksort($modelsNames);
96 3
        return $modelsNames;
97
    }
98
}
99