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 |
|
|
|
|
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)); |
|
|
|
|
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
|
|
|
|