|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Console\Command\User; |
|
4
|
|
|
|
|
5
|
|
|
use App\Model\User; |
|
6
|
|
|
use Ronanchilvers\Orm\Orm; |
|
7
|
|
|
use RuntimeException; |
|
8
|
|
|
use Symfony\Component\Console\Command\Command; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Command to create users |
|
16
|
|
|
* |
|
17
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class StatusCommand extends Command |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
public function configure() |
|
25
|
|
|
{ |
|
26
|
|
|
$this |
|
27
|
|
|
->setName('user:status') |
|
28
|
|
|
->setDescription('Update the status for an existing user') |
|
29
|
|
|
->addArgument( |
|
30
|
|
|
'email', |
|
31
|
|
|
InputArgument::REQUIRED, |
|
32
|
|
|
'The email address of the user' |
|
33
|
|
|
) |
|
34
|
|
|
->addArgument( |
|
35
|
|
|
'action', |
|
36
|
|
|
InputArgument::REQUIRED, |
|
37
|
|
|
'The action to take - one of \'activate\' or \'deactivate\'' |
|
38
|
|
|
) |
|
39
|
|
|
; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
46
|
|
|
{ |
|
47
|
|
|
$email = $input->getArgument('email'); |
|
48
|
|
|
$email = trim($email); |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
$action = $input->getArgument('action'); |
|
51
|
|
|
$action = trim($action); |
|
52
|
|
|
|
|
53
|
|
|
if (!in_array($action, ['activate', 'deactivate'])) { |
|
54
|
|
|
throw new RuntimeException('Invalid action ' . $action); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$output->writeln('Email : ' . $email); |
|
58
|
|
|
$output->writeln('Action : ' . $action); |
|
59
|
|
|
|
|
60
|
|
|
$user = Orm::finder(User::class)->select() |
|
61
|
|
|
->where(User::prefix('email'), $email) |
|
62
|
|
|
->one(); |
|
63
|
|
|
if (!$user instanceof User) { |
|
64
|
|
|
throw new RuntimeException('User not found for email ' . $email); |
|
65
|
|
|
} |
|
66
|
|
|
$user->status = ($action == 'activate') ? User::STATUS_ACTIVE : User::STATUS_INACTIVE; |
|
67
|
|
|
if (!$user->saveWithValidation()) { |
|
68
|
|
|
$errors = []; |
|
69
|
|
|
foreach ($user->getErrors() as $fieldErrors) { |
|
70
|
|
|
$errors = array_merge($errors, $fieldErrors); |
|
71
|
|
|
} |
|
72
|
|
|
$errors = implode(',', $errors); |
|
73
|
|
|
throw new RuntimeException('Unable to save user - ' . $errors); |
|
74
|
|
|
} |
|
75
|
|
|
$output->writeln("User {$email} updated"); |
|
76
|
|
|
|
|
77
|
|
|
return 0; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|