Passed
Push — master ( e67af9...64baeb )
by Luiz Kim
09:26 queued 01:57
created

GetActionByPeopleAction   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
eloc 45
c 2
b 0
f 0
dl 0
loc 125
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 45 4
A getMenuByPeople() 0 40 2
A __construct() 0 4 1
1
<?php
2
3
namespace ControleOnline\Controller;
4
5
6
use ControleOnline\Entity\People;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\People 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\Request;
0 ignored issues
show
Bug introduced by
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...
8
use Doctrine\ORM\EntityManagerInterface;
0 ignored issues
show
Bug introduced by
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...
9
use Exception;
10
use Symfony\Component\HttpFoundation\JsonResponse;
0 ignored issues
show
Bug introduced by
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...
11
use Symfony\Component\Security\Core\Security;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Security\Core\Security 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...
12
13
14
15
class GetActionByPeopleAction
16
{
17
  /**
18
   * Entity Manager
19
   *
20
   * @var EntityManagerInterface
21
   */
22
  private $manager = null;
23
24
  /**
25
   * Request
26
   *
27
   * @var Request
28
   */
29
  private $request  = null;
0 ignored issues
show
introduced by
The private property $request is not used, and could be removed.
Loading history...
30
31
  /**
32
   * Security
33
   *
34
   * @var Security
35
   */
36
  private $security = null;
37
38
  /**
39
   * @var \ControleOnline\Repository\MenuRepository
0 ignored issues
show
Bug introduced by
The type ControleOnline\Repository\MenuRepository 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...
40
   */
41
  private $repository = null;
0 ignored issues
show
introduced by
The private property $repository is not used, and could be removed.
Loading history...
42
43
44
  public function __construct(Security $security, EntityManagerInterface $entityManager)
45
  {
46
    $this->manager    = $entityManager;
47
    $this->security   = $security;
48
  }
49
50
  public function __invoke(Request $request): JsonResponse
51
  {
52
    try {
53
54
      $menu  = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $menu is dead and can be removed.
Loading history...
55
56
      $company = $request->query->get('myCompany', null);
57
58
      if ($company === null)
59
        throw new Exception("Company not found", 404);
60
61
62
      $myCompany = $this->manager->getRepository(People::class)
63
        ->find($company);
64
65
      if ($myCompany === null)
66
        throw new Exception("Company not found", 404);
67
68
69
70
      $currentUser = $this->security->getUser();
71
      /**
72
       * @var People
73
       */
74
      $userPeople = $currentUser->getPeople();
75
76
      $menu =  $this->getMenuByPeople($userPeople, $myCompany);
77
78
79
      return new JsonResponse([
80
        'response' => [
81
          'data'    => $menu,
82
          'count'   => 1,
83
          'error'   => '',
84
          'success' => true,
85
        ],
86
      ]);
87
    } catch (\Exception $e) {
88
89
      return new JsonResponse([
90
        'response' => [
91
          'data'    => [],
92
          'count'   => 0,
93
          'error'   => $e->getMessage(),
94
          'success' => false,
95
        ],
96
      ]);
97
    }
98
  }
99
100
  private function getMenuByPeople(People $userPeople, People $myCompany)
101
  {
102
103
    $return = [];
104
    $connection = $this->manager->getConnection();
105
106
    // build query
107
108
    $sql  = 'SELECT action.*,routes.route
109
110
    FROM action 
111
       INNER JOIN routes ON routes.id = action.route_id
112
       INNER JOIN action_role ON action_role.action_id = action.id
113
       INNER JOIN role ON role.id = action_role.role_id
114
       INNER JOIN people_role ON people_role.role_id = role.id
115
    WHERE people_role.company_id=:myCompany AND 
116
       people_role.people_id=:userPeople AND 
117
       routes.route=:route
118
    GROUP BY action.id
119
    ';
120
121
122
123
    $params = [];
124
125
    $params['myCompany']   = $myCompany->getId();
126
    $params['userPeople']   = $userPeople->getId();
127
    $params['route']   = $this->route;
0 ignored issues
show
Bug Best Practice introduced by
The property route does not exist on ControleOnline\Controller\GetActionByPeopleAction. Did you maybe forget to declare it?
Loading history...
128
    // execute query
129
130
    $statement = $connection->prepare($sql);
131
    $statement->execute($params);
132
133
    $result = $statement->fetchAll();
134
135
    foreach ($result as $action) {
136
      $return['routes'][trim($action['route'])]['actions'][$action['id']] = trim($action['action']);
137
    }
138
139
    return $return;
140
  }
141
}
142