1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the zibios/sharep. |
7
|
|
|
* |
8
|
|
|
* (c) Zbigniew Ślązak |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace App\Command; |
12
|
|
|
|
13
|
|
|
use App\Entity\Access\User; |
14
|
|
|
use App\Enum\Functional\ApplicationEnum; |
15
|
|
|
use App\Repository\Entity\Account\UserRepository; |
16
|
|
|
use Symfony\Component\Console\Command\Command; |
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
18
|
|
|
use Symfony\Component\Console\Input\InputOption; |
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
20
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
21
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
22
|
|
|
|
23
|
|
|
abstract class AbstractCommand extends Command |
24
|
|
|
{ |
25
|
|
|
protected const CODE_SUCCESS = 0; |
26
|
|
|
protected const CODE_ERROR = 1; |
27
|
|
|
protected const ENV_PROD = 'prod'; |
28
|
|
|
protected const ENV_DEV = 'dev'; |
29
|
|
|
protected const ENV_TEST = 'test'; |
30
|
|
|
protected const OPTION_RUN = 'run'; |
31
|
|
|
protected const LINE_SEPARATOR = '-------------------------------------------------------------'; |
32
|
|
|
protected $environments = []; |
33
|
|
|
/** @var UserRepository */ |
34
|
|
|
private $userRepository; |
35
|
|
|
/** @var TokenStorageInterface */ |
36
|
|
|
private $tokenStorage; |
37
|
|
|
|
38
|
|
|
//------------------------------------------------------------------------------------------------------------------ |
39
|
|
|
|
40
|
4 |
|
public function __construct(UserRepository $userRepository, TokenStorageInterface $tokenStorage) |
41
|
|
|
{ |
42
|
4 |
|
parent::__construct(); |
43
|
4 |
|
$this->userRepository = $userRepository; |
44
|
4 |
|
$this->tokenStorage = $tokenStorage; |
45
|
4 |
|
} |
46
|
|
|
|
47
|
|
|
//------------------------------------------------------------------------------------------------------------------ |
48
|
|
|
|
49
|
4 |
|
protected function configure(): void |
50
|
|
|
{ |
51
|
4 |
|
$this->addOption( |
52
|
4 |
|
self::OPTION_RUN, |
53
|
4 |
|
null, |
54
|
4 |
|
InputOption::VALUE_REQUIRED, |
55
|
4 |
|
'Required to run command' |
56
|
|
|
); |
57
|
4 |
|
} |
58
|
|
|
|
59
|
|
|
abstract protected function executeBody(InputInterface $input, OutputInterface $output): int; |
60
|
|
|
|
61
|
6 |
|
protected function execute(InputInterface $input, OutputInterface $output): int |
62
|
|
|
{ |
63
|
6 |
|
$this->checkEnvironmentName($input); |
64
|
6 |
|
$this->checkAllRequiredOptionsAreNotEmpty($input); |
65
|
6 |
|
$this->printHeader($input, $output); |
66
|
|
|
|
67
|
6 |
|
return $this->executeBody($input, $output); |
68
|
|
|
} |
69
|
|
|
|
70
|
4 |
|
protected function logInAsSystemMember(): void |
71
|
|
|
{ |
72
|
|
|
/** @var User $user */ |
73
|
4 |
|
$user = $this->userRepository |
74
|
4 |
|
->findOneBy( |
75
|
|
|
[ |
76
|
4 |
|
'email' => ApplicationEnum::SYSTEM_MEMBER_EMAIL, |
77
|
|
|
] |
78
|
|
|
); |
79
|
|
|
|
80
|
4 |
|
$token = new UsernamePasswordToken( |
81
|
4 |
|
$user, |
82
|
4 |
|
null, |
83
|
4 |
|
'main', |
84
|
4 |
|
$user->getRoles() |
85
|
|
|
); |
86
|
4 |
|
$this->tokenStorage->setToken($token); |
87
|
4 |
|
} |
88
|
|
|
|
89
|
|
|
//------------------------------------------------------------------------------------------------------------------ |
90
|
|
|
|
91
|
6 |
|
private function checkAllRequiredOptionsAreNotEmpty(InputInterface $input): void |
92
|
|
|
{ |
93
|
6 |
|
$options = $this->getDefinition()->getOptions(); |
94
|
6 |
|
foreach ($options as $option) { |
95
|
6 |
|
$name = $option->getName(); |
96
|
6 |
|
$value = $input->getOption($name); |
97
|
6 |
|
if (empty($value) && $option->isValueRequired()) { |
98
|
|
|
throw new \InvalidArgumentException(sprintf('The required value for option "%s" is not set', $name)); |
99
|
|
|
} |
100
|
|
|
} |
101
|
6 |
|
} |
102
|
|
|
|
103
|
6 |
|
private function checkEnvironmentName(InputInterface $input): void |
104
|
|
|
{ |
105
|
|
|
/** @var string $env */ |
106
|
6 |
|
$env = $input->getOption('env'); |
107
|
6 |
|
if (false === \in_array($env, $this->environments, true)) { |
108
|
|
|
throw new \InvalidArgumentException( |
109
|
|
|
sprintf( |
110
|
|
|
'Environment "%s" not supported, only ["%s"] allowed!', |
111
|
|
|
$env, |
112
|
|
|
implode('","', $this->environments) |
113
|
|
|
) |
114
|
|
|
); |
115
|
|
|
} |
116
|
6 |
|
} |
117
|
|
|
|
118
|
6 |
|
private function printHeader(InputInterface $input, OutputInterface $output): void |
119
|
|
|
{ |
120
|
|
|
/** @var string $env */ |
121
|
6 |
|
$env = $input->getOption('env'); |
122
|
6 |
|
$output->writeln(self::LINE_SEPARATOR); |
123
|
6 |
|
$output->writeln('- Command : ' . $this->getName()); |
124
|
6 |
|
$output->writeln('- Environment: ' . $env); |
125
|
6 |
|
$output->writeln(self::LINE_SEPARATOR); |
126
|
6 |
|
} |
127
|
|
|
} |
128
|
|
|
|