Test Setup Failed
Push — master ( 013fb3...ad1945 )
by Alexey
13:09
created

RestoreRemovedUsersCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 92
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A configure() 0 7 1
B execute() 0 40 5
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
    /**
18
     * @var LoggerInterface
19
     */
20
    private $logger;
21
    /**
22
     * @var EntityManagerInterface
23
     */
24
    private $em;
25
26
    /**
27
     * @var UserRepository
28
     */
29
    private $userRepo;
30
31
    /**
32
     * @var UserApi
33
     */
34
    private $userApi;
35
36
    /**
37
     * @var int
38
     */
39
    private $delay;
40
41
    public function __construct(LoggerInterface $logger, EntityManagerInterface $em, UserRepository $userRepo, UserApi $userApi, int $delay)
42
    {
43
        parent::__construct();
44
45
        $this->logger = $logger;
46
        $this->em = $em;
47
        $this->userRepo = $userRepo;
48
        $this->userApi = $userApi;
49
        $this->delay = $delay;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    protected function configure()
56
    {
57
        $this
58
            ->setName('point:users:restore')
59
            ->setDescription('Check removed users status and restore if user was deleted by error.')
60
        ;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    protected function execute(InputInterface $input, OutputInterface $output)
67
    {
68
        /** @var User $removedUser */
69
        foreach ($this->userRepo->findBy(['removed' => true]) as $removedUser) {
70
            usleep($this->delay);
71
72
            try {
73
                /** @var User $remoteUser */
74
                $remoteUser = $this->userApi->getUserById($removedUser->getId());
75
76
                if ($remoteUser->getId() === $removedUser->getId()) {
77
                    $this->logger->info('Restoring user', [
78
                        'id' => $removedUser->getId(),
79
                        'login' => $removedUser->getLogin(),
80
                    ]);
81
                    $removedUser->restore();
82
83
                    $this->em->flush();
84
                }
85
            } catch (UserNotFoundException $e) {
86
                $this->logger->debug('User is really removed. Keep going.', [
87
                    'id' => $removedUser->getId(),
88
                    'login' => $removedUser->getLogin(),
89
                ]);
90
91
                continue;
92
            } catch (\Exception $e) {
93
                $this->logger->error('Error while trying to restore user', [
94
                    'user_id' => $removedUser->getId(),
95
                    'user_login' => $removedUser->getLogin(),
96
                    'exception' => get_class($e),
97
                    'message' => $e->getMessage(),
98
                    'file' => $e->getFile(),
99
                    'line' => $e->getLine(),
100
                ]);
101
            }
102
        }
103
104
        $this->em->flush();
105
    }
106
}
107