Test Setup Failed
Push — master ( 3336bf...41079a )
by Alexey
03:09
created

RestoreRemovedUsersCommand::execute()   B

Complexity

Conditions 5
Paths 11

Size

Total Lines 38
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 8.439
c 0
b 0
f 0
cc 5
eloc 24
nc 11
nop 2
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 setDependencies(LoggerInterface $logger, EntityManagerInterface $em, UserRepository $userRepo, UserApi $userApi, int $delay): void
42
    {
43
        $this->logger = $logger;
44
        $this->em = $em;
45
        $this->userRepo = $userRepo;
46
        $this->userApi = $userApi;
47
        $this->delay = $delay;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    protected function configure()
54
    {
55
        $this
56
            ->setName('point:users:restore')
57
            ->setDescription('Check removed users status and restore if user was deleted by error.')
58
        ;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    protected function execute(InputInterface $input, OutputInterface $output)
65
    {
66
        /** @var User $removedUser */
67
        foreach ($this->userRepo->findBy(['removed' => true]) as $removedUser) {
68
            usleep($this->delay);
69
70
            try {
71
                /** @var User $remoteUser */
72
                $remoteUser = $this->userApi->getUserById($removedUser->getId());
73
74
                if ($remoteUser->getId() === $removedUser->getId()) {
75
                    $this->logger->info('Restoring user', [
76
                        'id' => $removedUser->getId(),
77
                        'login' => $removedUser->getLogin(),
78
                    ]);
79
                    $removedUser->restore();
80
                }
81
            } catch (UserNotFoundException $e) {
82
                $this->logger->debug('User is really removed. Keep going.', [
83
                    'id' => $removedUser->getId(),
84
                    'login' => $removedUser->getLogin(),
85
                ]);
86
87
                continue;
88
            } catch (\Exception $e) {
89
                $this->logger->error('Error while trying to restore user', [
90
                    'user_id' => $removedUser->getId(),
91
                    'user_login' => $removedUser->getLogin(),
92
                    'exception' => get_class($e),
93
                    'message' => $e->getMessage(),
94
                    'file' => $e->getFile(),
95
                    'line' => $e->getLine(),
96
                ]);
97
            }
98
        }
99
100
        $this->em->flush();
101
    }
102
}
103