Issues (188)

src/Controller/DeleteUserAction.php (6 issues)

Labels
Severity
1
<?php
2
3
namespace ControleOnline\Controller;
4
5
use Symfony\Component\HttpFoundation\Request;
0 ignored issues
show
The type Symfony\Component\HttpFoundation\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Doctrine\ORM\EntityManagerInterface;
0 ignored issues
show
The type Doctrine\ORM\EntityManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Symfony\Component\HttpFoundation\JsonResponse;
0 ignored issues
show
The type Symfony\Component\HttpFoundation\JsonResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface
0 ignored issues
show
The type Symfony\Component\Securi...e\TokenStorageInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
as Security;
10
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
0 ignored issues
show
The type Symfony\Component\Securi...asswordEncoderInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
use ControleOnline\Entity\People;
13
use ControleOnline\Entity\User;
0 ignored issues
show
The type ControleOnline\Entity\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
16
class DeleteUserAction
17
{
18
    /**
19
     * Entity Manager
20
     *
21
     * @var EntityManagerInterface
22
     */
23
    private $manager = null;
24
25
    /**
26
     * Request
27
     *
28
     * @var Request
29
     */
30
    private $request  = null;
31
32
33
34
    public function __construct(EntityManagerInterface $manager)
35
    {
36
        $this->manager     = $manager;
37
    }
38
39
    public function __invoke(People $data, Request $request): JsonResponse
40
    {
41
        $this->request = $request;
42
43
        try {
44
            $payload   = json_decode($this->request->getContent(), true);
45
            $result    = $this->deleteUser($data, $payload);
46
47
            return new JsonResponse([
48
                'response' => [
49
                    'data'    => $result,
50
                    'count'   => 1,
51
                    'error'   => '',
52
                    'success' => true,
53
                ],
54
            ], 200);
55
        } catch (\Exception $e) {
56
57
            return new JsonResponse([
58
                'response' => [
59
                    'data'    => [],
60
                    'count'   => 0,
61
                    'error'   => $e->getMessage(),
62
                    'success' => false,
63
                ],
64
            ]);
65
        }
66
    }
67
68
69
    private function deleteUser(People $person, array $payload): bool
70
    {
71
        try {
72
            $this->manager->getConnection()->beginTransaction();
73
74
            if (!isset($payload['id'])) {
75
                throw new \InvalidArgumentException('Document id is not defined');
76
            }
77
78
            $users   = $this->manager->getRepository(User::class)->findBy(['people' => $person]);
79
            if (count($users) == 1) {
80
                throw new \InvalidArgumentException('Deve existir pelo menos um usuário');
81
            }
82
83
            $user    = $this->manager->getRepository(User::class)->findOneBy(['id' => $payload['id'], 'people' => $person]);
84
            if (!$user instanceof User) {
85
                throw new \InvalidArgumentException('Person user was not found');
86
            }
87
88
            $this->manager->remove($user);
89
90
            $this->manager->flush();
91
            $this->manager->getConnection()->commit();
92
93
            return true;
94
        } catch (\Exception $e) {
95
            if ($this->manager->getConnection()->isTransactionActive())
96
                $this->manager->getConnection()->rollBack();
97
98
            throw new \InvalidArgumentException($e->getMessage());
99
        }
100
    }
101
}
102