groupManufacturersByFirstLetter()   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: ListManufacturersCommand.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\ManufacturerDTOArray;
12
use MSlwk\Otomoto\Middleware\App\Manufacturer\ManufacturerFactory;
13
use Symfony\Component\Console\Command\Command;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
/**
18
 * Class ListManufacturersCommand
19
 * @package MSlwk\Otomoto\Cli\Command
20
 */
21
class ListManufacturersCommand extends Command
22
{
23
    const COMMAND_NAME = 'app:manufacturer-list';
24
    const COMMAND_DESC = 'List all available manufacturers';
25
26
    /**
27
     * @var ManufacturerFactory
28
     */
29
    private $manufacturerMiddlewareFactory;
30
31
    /**
32
     * ListManufacturersCommand constructor.
33
     * @param ManufacturerFactory|null $manufacturerFactory
34
     * @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...
35
     */
36 5
    public function __construct(ManufacturerFactory $manufacturerFactory = null, $name = null)
37
    {
38 5
        parent::__construct($name);
39 5
        $this->manufacturerMiddlewareFactory = $manufacturerFactory ?? new ManufacturerFactory();
40 5
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45 5
    protected function configure()
46
    {
47 5
        $this->setName(self::COMMAND_NAME)
48 5
            ->setDescription(self::COMMAND_DESC);
49
50 5
        parent::configure();
51 5
    }
52
53
    /**
54
     * @param InputInterface $input
55
     * @param OutputInterface $output
56
     * @return void
57
     */
58 2
    protected function execute(InputInterface $input, OutputInterface $output)
59
    {
60 2
        $manufacturerMiddleware = $this->manufacturerMiddlewareFactory->create();
61 2
        $manufacturers = $manufacturerMiddleware->getManufacturers();
62
63 2
        foreach ($this->groupManufacturersByFirstLetter($manufacturers) as $manufacturerLine) {
64 2
            $output->writeln("<info>{$manufacturerLine}</info>");
65
        }
66 2
    }
67
68
    /**
69
     * @param ManufacturerDTOArray $manufacturerDTOArray
70
     * @return array
71
     */
72 3
    private function groupManufacturersByFirstLetter(ManufacturerDTOArray $manufacturerDTOArray): array
73
    {
74 3
        $manufacturersNames = [];
75 3
        foreach ($manufacturerDTOArray as $manufacturerDTO) {
76 3
            $firstLetter = substr($manufacturerDTO->getName(), 0, 1);
77 3
            if (!isset($manufacturersNames[$firstLetter])) {
78 3
                $manufacturersNames[$firstLetter] = "{$firstLetter}: {$manufacturerDTO->getName()}";
79
            } else {
80 3
                $manufacturersNames[$firstLetter] .= ", {$manufacturerDTO->getName()}";
81
            }
82
        }
83
84 3
        ksort($manufacturersNames);
85 3
        return $manufacturersNames;
86
    }
87
}
88