1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command; |
4
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\RelationsGenerator; |
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
|
11
|
|
|
class SetRelationCommand extends AbstractCommand |
12
|
|
|
{ |
13
|
|
|
public const OPT_ENTITY1 = 'entity1'; |
14
|
|
|
public const OPT_ENTITY1_SHORT = 'm'; |
15
|
|
|
|
16
|
|
|
public const OPT_HAS_TYPE = 'hasType'; |
17
|
|
|
public const OPT_HAS_TYPE_SHORT = 't'; |
18
|
|
|
|
19
|
|
|
public const OPT_ENTITY2 = 'entity2'; |
20
|
|
|
public const OPT_ENTITY2_SHORT = 'i'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var RelationsGenerator |
24
|
|
|
*/ |
25
|
|
|
protected $relationsGenerator; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* SetRelationCommand constructor. |
29
|
|
|
* |
30
|
|
|
* @param RelationsGenerator $relationsGenerator |
31
|
|
|
* @param null|string $name |
32
|
|
|
* |
33
|
|
|
* @throws DoctrineStaticMetaException |
34
|
|
|
*/ |
35
|
2 |
|
public function __construct( |
36
|
|
|
RelationsGenerator $relationsGenerator, |
37
|
|
|
?string $name = null |
38
|
|
|
) { |
39
|
2 |
|
parent::__construct($name); |
40
|
2 |
|
$this->relationsGenerator = $relationsGenerator; |
41
|
2 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @throws DoctrineStaticMetaException |
45
|
|
|
*/ |
46
|
2 |
|
public function configure(): void |
47
|
|
|
{ |
48
|
|
|
try { |
49
|
2 |
|
$this->setName(AbstractCommand::COMMAND_PREFIX . 'set:relation') |
50
|
2 |
|
->setDefinition( |
51
|
|
|
[ |
52
|
2 |
|
new InputOption( |
53
|
2 |
|
self::OPT_ENTITY1, |
54
|
2 |
|
self::OPT_ENTITY1_SHORT, |
55
|
2 |
|
InputOption::VALUE_REQUIRED, |
56
|
2 |
|
'First entity in relation' |
57
|
|
|
), |
58
|
2 |
|
new InputOption( |
59
|
2 |
|
self::OPT_HAS_TYPE, |
60
|
2 |
|
self::OPT_HAS_TYPE_SHORT, |
61
|
2 |
|
InputOption::VALUE_REQUIRED, |
62
|
|
|
'What type of relation is it? ' |
63
|
2 |
|
. 'Must be one of ' . RelationsGenerator::class . '::RELATION_TYPES' |
64
|
|
|
), |
65
|
2 |
|
new InputOption( |
66
|
2 |
|
self::OPT_ENTITY2, |
67
|
2 |
|
self::OPT_ENTITY2_SHORT, |
68
|
2 |
|
InputOption::VALUE_REQUIRED, |
69
|
2 |
|
'Second entity in relation' |
70
|
|
|
), |
71
|
2 |
|
$this->getProjectRootPathOption(), |
72
|
2 |
|
$this->getProjectRootNamespaceOption(), |
73
|
2 |
|
$this->getSrcSubfolderOption(), |
74
|
|
|
] |
75
|
2 |
|
)->setDescription( |
76
|
|
|
'Set a relation between 2 entities. The relation must be one of ' |
77
|
2 |
|
. RelationsGenerator::class . '::RELATION_TYPES' |
78
|
|
|
); |
79
|
|
|
} catch (\Exception $e) { |
80
|
|
|
throw new DoctrineStaticMetaException( |
81
|
|
|
'Exception in ' . __METHOD__ . ': ' . $e->getMessage(), |
82
|
|
|
$e->getCode(), |
83
|
|
|
$e |
84
|
|
|
); |
85
|
|
|
} |
86
|
2 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param InputInterface $input |
90
|
|
|
* @param OutputInterface $output |
91
|
|
|
* |
92
|
|
|
* @return int|null|void |
93
|
|
|
* @throws DoctrineStaticMetaException |
94
|
|
|
*/ |
95
|
2 |
|
public function execute(InputInterface $input, OutputInterface $output) |
96
|
|
|
{ |
97
|
|
|
try { |
98
|
2 |
|
$output->writeln( |
99
|
|
|
'<comment>Setting relation: ' |
100
|
2 |
|
. $input->getOption(static::OPT_ENTITY1) |
101
|
2 |
|
. ' ' . $input->getOption(static::OPT_HAS_TYPE) |
102
|
2 |
|
. ' ' . $input->getOption(static::OPT_ENTITY2) |
103
|
2 |
|
. '</comment>' |
104
|
|
|
); |
105
|
2 |
|
$this->checkOptions($input); |
106
|
2 |
|
$hasType = $input->getOption(static::OPT_HAS_TYPE); |
107
|
2 |
|
if (!\in_array($hasType, RelationsGenerator::HAS_TYPES, true)) { |
108
|
1 |
|
$hasType = RelationsGenerator::PREFIX_OWNING . $hasType; |
109
|
1 |
|
if (!\in_array($hasType, RelationsGenerator::HAS_TYPES, true)) { |
110
|
|
|
throw new DoctrineStaticMetaException( |
111
|
|
|
'Invalid hasType ' . $input->getOption(static::OPT_HAS_TYPE) |
112
|
|
|
. ' Must be one of ' . print_r(RelationsGenerator::HAS_TYPES, true) |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
} |
116
|
2 |
|
$this->relationsGenerator |
117
|
2 |
|
->setPathToProjectRoot($input->getOption(AbstractCommand::OPT_PROJECT_ROOT_PATH)) |
118
|
2 |
|
->setProjectRootNamespace($input->getOption(AbstractCommand::OPT_PROJECT_ROOT_NAMESPACE)); |
119
|
|
|
|
120
|
2 |
|
$this->relationsGenerator->setEntityHasRelationToEntity( |
121
|
2 |
|
$input->getOption(static::OPT_ENTITY1), |
122
|
2 |
|
$hasType, |
123
|
2 |
|
$input->getOption(static::OPT_ENTITY2) |
124
|
|
|
); |
125
|
2 |
|
$output->writeln('<info>completed</info>'); |
126
|
|
|
} catch (\Exception $e) { |
127
|
|
|
throw new DoctrineStaticMetaException( |
128
|
|
|
'Exception in ' . __METHOD__ . ': ' . $e->getMessage(), |
129
|
|
|
$e->getCode(), |
130
|
|
|
$e |
131
|
|
|
); |
132
|
|
|
} |
133
|
2 |
|
} |
134
|
|
|
} |
135
|
|
|
|