|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace VSV\GVQ_API\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Ramsey\Uuid\Uuid; |
|
6
|
|
|
use Symfony\Component\Console\Command\Command; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
11
|
|
|
use VSV\GVQ_API\Question\Models\Categories; |
|
12
|
|
|
use VSV\GVQ_API\Question\Models\Category; |
|
13
|
|
|
use VSV\GVQ_API\Question\Repositories\CategoryRepository; |
|
14
|
|
|
use VSV\GVQ_API\Question\ValueObjects\NotEmptyString; |
|
15
|
|
|
|
|
16
|
|
|
class SeedCategoriesCommand extends Command |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var CategoryRepository |
|
20
|
|
|
*/ |
|
21
|
|
|
private $categoriesRepository; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param CategoryRepository $categoriesRepository |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct(CategoryRepository $categoriesRepository) |
|
27
|
|
|
{ |
|
28
|
|
|
parent::__construct(); |
|
29
|
|
|
$this->categoriesRepository = $categoriesRepository; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
protected function configure(): void |
|
33
|
|
|
{ |
|
34
|
|
|
$this->setName('gvq:seed-categories') |
|
35
|
|
|
->setDescription('Upload all categories') |
|
36
|
|
|
->addArgument('categories_file', InputArgument::OPTIONAL, 'Yaml file with categories'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @inheritdoc |
|
41
|
|
|
*/ |
|
42
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): void |
|
43
|
|
|
{ |
|
44
|
|
|
$output->writeln('Seeding categories...'); |
|
45
|
|
|
|
|
46
|
|
|
foreach ($this->getCategories($input) as $category) { |
|
47
|
|
|
$output->writeln('Seeding category: '.$category->getName()->toNative()); |
|
48
|
|
|
$this->categoriesRepository->save($category); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$output->writeln('Seeding finished.'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param InputInterface $input |
|
56
|
|
|
* @return Categories |
|
57
|
|
|
*/ |
|
58
|
|
|
private function getCategories(InputInterface $input): Categories |
|
59
|
|
|
{ |
|
60
|
|
|
$categoriesFile = $input->getArgument('categories_file'); |
|
61
|
|
|
if (!$categoriesFile) { |
|
62
|
|
|
$categoriesFile = __DIR__ . '/categories.yaml'; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$categoriesAsYml = Yaml::parseFile($categoriesFile); |
|
66
|
|
|
$categories = $this->createCategoriesFromYml($categoriesAsYml); |
|
67
|
|
|
|
|
68
|
|
|
return $categories; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param array $categoriesAsYml |
|
73
|
|
|
* @return Categories |
|
74
|
|
|
*/ |
|
75
|
|
|
private function createCategoriesFromYml(array $categoriesAsYml): Categories |
|
76
|
|
|
{ |
|
77
|
|
|
return new Categories( |
|
78
|
|
|
...array_map( |
|
79
|
|
|
function (array $categoryAsYml) { |
|
80
|
|
|
return new Category( |
|
81
|
|
|
Uuid::fromString($categoryAsYml['id']), |
|
82
|
|
|
new NotEmptyString($categoryAsYml['name']) |
|
83
|
|
|
); |
|
84
|
|
|
}, |
|
85
|
|
|
$categoriesAsYml |
|
86
|
|
|
) |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|