1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Admin\Console\Commands\User; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Admin\Orm\UserRepo; |
8
|
|
|
use AbterPhp\Framework\Authorization\CacheManager; |
9
|
|
|
use Opulence\Console\Commands\Command; |
10
|
|
|
use Opulence\Console\Requests\Argument; |
11
|
|
|
use Opulence\Console\Requests\ArgumentTypes; |
12
|
|
|
use Opulence\Console\Requests\Option; |
13
|
|
|
use Opulence\Console\Requests\OptionTypes; |
14
|
|
|
use Opulence\Console\Responses\IResponse; |
15
|
|
|
use Opulence\Console\StatusCodes; |
16
|
|
|
use Opulence\Orm\IUnitOfWork; |
17
|
|
|
|
18
|
|
|
class Delete extends Command |
19
|
|
|
{ |
20
|
|
|
public const COMMAND_NAME = 'user:delete'; |
21
|
|
|
public const COMMAND_DESCRIPTION = 'Deletes an existing user'; |
22
|
|
|
public const COMMAND_SUCCESS = '<success>Existing user is deleted.</success>'; |
23
|
|
|
public const COMMAND_DRY_RUN_MESSAGE = '<info>Dry run prevented deleting existing user.</info>'; |
24
|
|
|
|
25
|
|
|
public const ARGUMENT_IDENTIFIER = 'identifier'; |
26
|
|
|
|
27
|
|
|
public const OPTION_DRY_RUN = 'dry-run'; |
28
|
|
|
public const SHORTENED_DRY_RUN = 'd'; |
29
|
|
|
|
30
|
|
|
protected UserRepo $userRepo; |
31
|
|
|
|
32
|
|
|
protected IUnitOfWork $unitOfWork; |
33
|
|
|
|
34
|
|
|
protected CacheManager $cacheManager; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* CreateUserCommand constructor. |
38
|
|
|
* |
39
|
|
|
* @param UserRepo $userRepo |
40
|
|
|
* @param IUnitOfWork $unitOfWork |
41
|
|
|
* @param CacheManager $cacheManager |
42
|
|
|
*/ |
43
|
|
|
public function __construct(UserRepo $userRepo, IUnitOfWork $unitOfWork, CacheManager $cacheManager) |
44
|
|
|
{ |
45
|
|
|
$this->userRepo = $userRepo; |
46
|
|
|
$this->unitOfWork = $unitOfWork; |
47
|
|
|
$this->cacheManager = $cacheManager; |
48
|
|
|
|
49
|
|
|
parent::__construct(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritdoc |
54
|
|
|
*/ |
55
|
|
|
protected function define() |
56
|
|
|
{ |
57
|
|
|
$this->setName(static::COMMAND_NAME) |
58
|
|
|
->setDescription(static::COMMAND_DESCRIPTION) |
59
|
|
|
->addArgument( |
60
|
|
|
new Argument( |
61
|
|
|
static::ARGUMENT_IDENTIFIER, |
62
|
|
|
ArgumentTypes::REQUIRED, |
63
|
|
|
'Identifier (Email or Username)' |
64
|
|
|
) |
65
|
|
|
) |
66
|
|
|
->addOption( |
67
|
|
|
new Option( |
68
|
|
|
static::OPTION_DRY_RUN, |
69
|
|
|
static::SHORTENED_DRY_RUN, |
70
|
|
|
OptionTypes::OPTIONAL_VALUE, |
71
|
|
|
'Dry run (default: 0)', |
72
|
|
|
'0' |
73
|
|
|
) |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @inheritdoc |
79
|
|
|
*/ |
80
|
|
|
protected function doExecute(IResponse $response) |
81
|
|
|
{ |
82
|
|
|
$identifier = $this->getArgumentValue(static::ARGUMENT_IDENTIFIER); |
83
|
|
|
$dryRun = $this->getOptionValue(static::OPTION_DRY_RUN); |
84
|
|
|
|
85
|
|
|
$entity = $this->userRepo->find($identifier); |
86
|
|
|
if (!$entity) { |
87
|
|
|
$response->writeln(sprintf('<fatal>User not found</fatal>')); |
88
|
|
|
|
89
|
|
|
return StatusCodes::ERROR; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->userRepo->delete($entity); |
93
|
|
|
|
94
|
|
|
if ($dryRun) { |
95
|
|
|
$this->unitOfWork->dispose(); |
96
|
|
|
$response->writeln(static::COMMAND_DRY_RUN_MESSAGE); |
97
|
|
|
|
98
|
|
|
return StatusCodes::OK; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
try { |
102
|
|
|
$this->unitOfWork->commit(); |
103
|
|
|
$this->cacheManager->clearAll(); |
104
|
|
|
} catch (\Exception $e) { |
105
|
|
|
if ($e->getPrevious()) { |
106
|
|
|
$response->writeln(sprintf('<error>%s</error>', $e->getPrevious()->getMessage())); |
107
|
|
|
} |
108
|
|
|
$response->writeln(sprintf('<fatal>%s</fatal>', $e->getMessage())); |
109
|
|
|
|
110
|
|
|
return StatusCodes::FATAL; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$response->writeln(static::COMMAND_SUCCESS); |
114
|
|
|
|
115
|
|
|
return StatusCodes::OK; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|