1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Skobkin\Bundle\PointToolsBundle\Command; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Psr\Log\LoggerInterface; |
7
|
|
|
use Skobkin\Bundle\PointToolsBundle\Entity\User; |
8
|
|
|
use Skobkin\Bundle\PointToolsBundle\Exception\Api\UserNotFoundException; |
9
|
|
|
use Skobkin\Bundle\PointToolsBundle\Repository\UserRepository; |
10
|
|
|
use Skobkin\Bundle\PointToolsBundle\Service\Api\UserApi; |
11
|
|
|
use Symfony\Component\Console\Command\Command; |
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
|
15
|
|
|
class RestoreRemovedUsersCommand extends Command |
16
|
|
|
{ |
17
|
|
|
/** @var LoggerInterface */ |
18
|
|
|
private $logger; |
19
|
|
|
|
20
|
|
|
/** @var EntityManagerInterface */ |
21
|
|
|
private $em; |
22
|
|
|
|
23
|
|
|
/** @var UserRepository */ |
24
|
|
|
private $userRepo; |
25
|
|
|
|
26
|
|
|
/** @var UserApi */ |
27
|
|
|
private $userApi; |
28
|
|
|
|
29
|
|
|
/** @var int */ |
30
|
|
|
private $delay; |
31
|
|
|
|
32
|
|
|
public function __construct(LoggerInterface $logger, EntityManagerInterface $em, UserRepository $userRepo, UserApi $userApi, int $apiDelay) |
33
|
|
|
{ |
34
|
|
|
parent::__construct(); |
35
|
|
|
|
36
|
|
|
$this->logger = $logger; |
37
|
|
|
$this->em = $em; |
38
|
|
|
$this->userRepo = $userRepo; |
39
|
|
|
$this->userApi = $userApi; |
40
|
|
|
$this->delay = $apiDelay; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
protected function configure() |
47
|
|
|
{ |
48
|
|
|
$this |
49
|
|
|
->setName('point:users:restore') |
50
|
|
|
->setDescription('Check removed users status and restore if user was deleted by error.') |
51
|
|
|
; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
58
|
|
|
{ |
59
|
|
|
/** @var User $removedUser */ |
60
|
|
|
foreach ($this->userRepo->findBy(['removed' => true]) as $removedUser) { |
61
|
|
|
usleep($this->delay); |
62
|
|
|
|
63
|
|
|
try { |
64
|
|
|
/** @var User $remoteUser */ |
65
|
|
|
$remoteUser = $this->userApi->getUserById($removedUser->getId()); |
66
|
|
|
|
67
|
|
|
if ($remoteUser->getId() === $removedUser->getId()) { |
68
|
|
|
$this->logger->info('Restoring user', [ |
69
|
|
|
'id' => $removedUser->getId(), |
70
|
|
|
'login' => $removedUser->getLogin(), |
71
|
|
|
]); |
72
|
|
|
$removedUser->restore(); |
73
|
|
|
|
74
|
|
|
$this->em->flush(); |
75
|
|
|
} |
76
|
|
|
} catch (UserNotFoundException $e) { |
77
|
|
|
$this->logger->debug('User is really removed. Keep going.', [ |
78
|
|
|
'id' => $removedUser->getId(), |
79
|
|
|
'login' => $removedUser->getLogin(), |
80
|
|
|
]); |
81
|
|
|
|
82
|
|
|
continue; |
83
|
|
|
} catch (\Exception $e) { |
84
|
|
|
$this->logger->error('Error while trying to restore user', [ |
85
|
|
|
'user_id' => $removedUser->getId(), |
86
|
|
|
'user_login' => $removedUser->getLogin(), |
87
|
|
|
'exception' => get_class($e), |
88
|
|
|
'message' => $e->getMessage(), |
89
|
|
|
'file' => $e->getFile(), |
90
|
|
|
'line' => $e->getLine(), |
91
|
|
|
]); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->em->flush(); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|