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

GetMenuByPeopleAction   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getMenuByPeople() 0 51 2
A __invoke() 0 45 4
1
<?php
2
3
namespace ControleOnline\Controller;
4
5
use ControleOnline\Entity\Menu;
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
use 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...
13
14
15
class GetMenuByPeopleAction
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
40
   */
41
  private $repository = null;
42
43
44
  public function __construct(Security $security, EntityManagerInterface $entityManager)
45
  {
46
    $this->manager    = $entityManager;
47
    $this->security   = $security;
48
    $this->repository = $this->manager->getRepository(\ControleOnline\Entity\Menu::class);
49
  }
50
51
  public function __invoke(Request $request): JsonResponse
52
  {
53
    try {
54
55
      $menu  = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $menu is dead and can be removed.
Loading history...
56
57
      $company = $request->query->get('myCompany', null);
58
59
      if ($company === null)
60
        throw new Exception("Company not found", 404);
61
62
63
      $myCompany = $this->manager->getRepository(People::class)
64
        ->find($company);
65
66
      if ($myCompany === null)
67
        throw new Exception("Company not found", 404);
68
69
70
71
      $currentUser = $this->security->getUser();
72
      /**
73
       * @var People
74
       */
75
      $userPeople = $currentUser->getPeople();
76
77
      $menu =  $this->getMenuByPeople($userPeople, $myCompany);
78
79
80
      return new JsonResponse([
81
        'response' => [
82
          'data'    => $menu,
83
          'count'   => 1,
84
          'error'   => '',
85
          'success' => true,
86
        ],
87
      ]);
88
    } catch (\Exception $e) {
89
90
      return new JsonResponse([
91
        'response' => [
92
          'data'    => [],
93
          'count'   => 0,
94
          'error'   => $e->getMessage(),
95
          'success' => false,
96
        ],
97
      ]);
98
    }
99
  }
100
101
  private function getMenuByPeople(People $userPeople, People $myCompany)
102
  {
103
104
    $return = [];
105
    $connection = $this->manager->getConnection();
106
107
    // build query
108
109
    $sql  = 'SELECT menu.*,
110
            category.name AS category_label,
111
            category.color AS category_color,
112
            routes.route AS route,
113
            category.icon AS category_icon FROM menu             
114
             INNER JOIN category ON category.id = menu.category_id
115
             INNER JOIN menu_role ON menu.id = menu_role.menu_id
116
             INNER JOIN people_role ON people_role.role_id = menu_role.role_id    
117
             INNER JOIN routes ON routes.id = menu.route_id
118
             WHERE people_role.company_id=:myCompany AND people_role.people_id=:userPeople    
119
             GROUP BY menu.id
120
             ';
121
122
123
    $params = [];
124
125
    $params['myCompany']   = $myCompany->getId();
126
    $params['userPeople']   = $userPeople->getId();
127
    // execute query
128
129
    $statement = $connection->prepare($sql);
130
    $statement->execute($params);
131
132
    $result = $statement->fetchAll();
133
134
    foreach ($result as $menu) {
135
136
      $return['modules'][$menu['category_id']]['id'] = $menu['category_id'];
137
      $return['modules'][$menu['category_id']]['label'] = $menu['category_label'];
138
      $return['modules'][$menu['category_id']]['color'] = $menu['category_color'];
139
      $return['modules'][$menu['category_id']]['icon'] = $menu['category_icon'];
140
      $return['modules'][$menu['category_id']]['menus'][] = [
141
        'id' => $menu['id'],
142
        'label' =>  $menu['menu'],
143
        'icon' =>  $menu['icon'],
144
        'color' =>  $menu['color'],
145
        'route' =>  $menu['route'],
146
      ];
147
    }
148
149
150
151
    return $return;
152
  }
153
}
154