Passed
Push — master ( c73812...aec782 )
by Luiz Kim
02:06
created

GetMyCompaniesAction   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 250
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 131
c 1
b 0
f 0
dl 0
loc 250
rs 10
wmc 25

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getDocument() 0 12 2
A getPeopleDomains() 0 20 3
F __invoke() 0 152 13
A getPeoplePackages() 0 26 4
A getLogo() 0 10 2
A __construct() 0 5 1
1
<?php
2
3
namespace ControleOnline\Controller;
4
5
use ControleOnline\Entity\Config;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\Config 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 ControleOnline\Entity\File;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\File 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 ControleOnline\Entity\People;
8
use ControleOnline\Entity\PeopleDomain;
9
use ControleOnline\Entity\PeopleLink;
10
use ControleOnline\Entity\PeoplePackage;
11
use ControleOnline\Service\PeopleRoleService;
12
use ControleOnline\Entity\PackageModules;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\PackageModules 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
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...
14
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...
15
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...
16
17
class GetMyCompaniesAction
18
{
19
  /*
20
   * @var Security
21
   */
22
  private $security;
23
24
  private $em = null;
25
  private $roles;
26
27
  public function __construct(Security $security, EntityManagerInterface $entityManager, PeopleRoleService $roles)
28
  {
29
    $this->security = $security;
30
    $this->em      = $entityManager;
31
    $this->roles = $roles;
32
  }
33
34
35
36
37
  public function __invoke(): JsonResponse
38
  {
39
    try {
40
41
      $myCompanies = [];
42
43
      /**
44
       * @var \ControleOnline\Entity\User
45
       */
46
      $currentUser = $this->security->getUser();
47
48
      /**
49
       * @var \ControleOnline\Entity\People
50
       */
51
      $userPeople  = $currentUser->getPeople();
52
      $permissions = [];
53
54
55
      $getPeopleCompanies = $userPeople->getLink();
56
57
      /**
58
       * @var \ControleOnline\Entity\PeopleLink $peopleCompany
59
       */
60
      foreach ($getPeopleCompanies as $peopleCompany) {
61
62
        $allConfigs = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $allConfigs is dead and can be removed.
Loading history...
63
        $configs = [];
64
        $people = $peopleCompany->getCompany();
65
66
        //if ($peopleCompany->getEnabled() && $people->getEnabled()) {
67
68
        $domains = $this->getPeopleDomains($people);
69
        $packages = $this->getPeoplePackages($people);
70
71
72
        $permissions[$people->getId()] = $this->roles->getAllRoles($userPeople);
73
74
        $allConfigs = $this->em->getRepository(Config::class)->findBy([
75
          'people'      => $people->getId(),
76
          'visibility'  => 'public'
77
        ]);
78
        foreach ($allConfigs as $config) {
79
          $configs[$config->getConfigKey()] = $config->getConfigValue();
80
        }
81
82
        $myCompanies[$people->getId()] = [
83
          'id'            => $people->getId(),
84
          'enabled'       => $people->getEnabled(),
85
          'alias'         => $people->getAlias(),
86
          'logo'          => $this->getLogo($people),
87
          'document'      => $this->getDocument($people),
88
          'domains'       => $domains,
89
          'configs'       => $configs,
90
          'packages'      => $packages,
91
          'user'          => [
92
            'id' => $userPeople->getId(),
93
            'name' => $userPeople->getName(),
94
            'alias' => $userPeople->getAlias(),
95
            'enabled' => $userPeople->getEnabled(),
96
            'employee_enabled' => $peopleCompany->getEnabled(),
97
            'salesman_enabled' => false
98
          ]
99
        ];
100
        //}
101
      }
102
103
      $peopleSalesman = $this->em->getRepository(People::class)->getPeopleLinks($userPeople, 'salesman');
104
105
      foreach ($peopleSalesman as $com) {
106
        $company = $this->em->getRepository(People::class)->find($com['people_id']);
107
        $allConfigs = [];
108
        $configs = [];
109
        $allConfigs = $this->em->getRepository(Config::class)->findBy([
110
          'people'      => $company->getId(),
111
          'visibility'  => 'public'
112
        ]);
113
        foreach ($allConfigs as $config) {
114
          $configs[$config->getConfigKey()] = $config->getConfigValue();
115
        }
116
117
        if ($company) {
118
          $people_domains = $this->em->getRepository(PeopleDomain::class)->findBy(['people' => $com['people_id']]);
119
120
          $domains = [];
121
122
          if (!empty($people_domains)) {
123
124
            /**
125
             * @var PeopleDomain $company
126
             */
127
            foreach ($people_domains as $domain) {
128
129
              $domains[] = [
130
                'id'         => $domain->getId(),
131
                'domainType' => $domain->getDomainType(),
132
                'domain'     => $domain->getDomain()
133
              ];
134
            }
135
          }
136
137
          $peopleemployee =   $this->em->getRepository(PeopleLink::class)->findOneBy(['company' => $company, 'employee' => $userPeople]);
138
139
          $permissions[$company->getId()][] = 'salesman';
140
          $myCompanies[$company->getId()] = [
141
            'id'         => $company->getId(),
142
            'enabled'    => $company->getEnabled(),
143
            'alias'      => $company->getAlias(),
144
            'logo'       => $this->getLogo($company),
145
            'document'   => $this->getDocument($company),
146
            'commission' => $com['commission'],
147
            'domains'    => $domains,
148
            'configs'       => $configs,
149
            'user'          => [
150
              'id' => $userPeople->getId(),
151
              'name' => $userPeople->getName(),
152
              'alias' => $userPeople->getAlias(),
153
              'enabled' => $userPeople->getEnabled(),
154
              'employee_enabled' => $peopleemployee ? $peopleemployee->getEnabled() : $com['enable'],
155
              'salesman_enabled' => $com['enable']
156
            ]
157
          ];
158
        }
159
      }
160
161
      foreach ($permissions as $key => $permission) {
162
        $myCompanies[$key]['permission'] = array_values($permission);
163
      }
164
165
      usort($myCompanies, function ($a, $b) {
166
167
        if ($a['alias'] == $b['alias']) {
168
          return 0;
169
        }
170
        return ($a['alias'] < $b['alias']) ? -1 : 1;
171
      });
172
173
      return new JsonResponse([
174
        'response' => [
175
          'data'        => $myCompanies,
176
          'count'       => count($myCompanies),
177
          'error'       => '',
178
          'success'     => true,
179
        ],
180
      ]);
181
    } catch (\Exception $e) {
182
183
      return new JsonResponse([
184
        'response' => [
185
          'data'    => [],
186
          'count'   => 0,
187
          'error'   => $e->getMessage(),
188
          'success' => false,
189
        ],
190
      ]);
191
    }
192
  }
193
  private function getPeoplePackages($people)
194
  {
195
196
197
    $people_packages = $this->em->getRepository(PeoplePackage::class)->findBy(['people' => $people]);
198
    $packages = [];
199
    $p_m = [];
200
201
202
    foreach ($people_packages as $people_package) {
203
      $package = $people_package->getPackage();
204
      $package_modules = $this->em->getRepository(PackageModules::class)->findBy(['package' => $package]);
205
206
      foreach ($package_modules as $package_module) {
207
        $p_m[$package_module->getId()]['users']  = $package_module->getUsers();
208
        $p_m[$package_module->getId()]['module'] = $package_module->getModule()->getName();
209
      }
210
211
      $packages[$people_package->getId()]['id']                   =  $people_package->getId();
212
      $packages[$people_package->getId()]['package']['id']        =  $package->getId();
213
      $packages[$people_package->getId()]['package']['name']      =  $package->getName();
214
      $packages[$people_package->getId()]['package']['active']    =  $package->isActive() ? true : false;
215
      $packages[$people_package->getId()]['package']['modules']   =  $p_m;
216
    }
217
218
    return $packages;
219
  }
220
221
  private function getPeopleDomains($people)
222
  {
223
    $people_domains = $this->em->getRepository(PeopleDomain::class)->findBy(['people' => $people->getId()]);
224
    $domains = [];
225
226
    if (!empty($people_domains)) {
227
228
      /**
229
       * @var PeopleDomain $company
230
       */
231
      foreach ($people_domains as $domain) {
232
233
        $domains[] = [
234
          'id'         => $domain->getId(),
235
          'domainType' => $domain->getDomainType(),
236
          'domain'     => $domain->getDomain()
237
        ];
238
      }
239
    }
240
    return $domains;
241
  }
242
243
  private function getDocument(People $company): ?string
244
  {
245
    $documents = $company->getDocument();
246
247
    /**
248
     * @var \ControleOnline\Entity\Document $document
249
     */
250
    $documents = $documents->filter(function ($document) {
251
      return $document->getDocumentType()->getDocumentType() == 'CNPJ';
252
    });
253
254
    return $documents->first() !== false ? $documents->first()->getDocument() : null;
255
  }
256
257
  private function getLogo(People $company): ?array
258
  {
259
    if ($company->getFile() instanceof File)
260
      return [
261
        'id'     => $company->getFile()->getId(),
262
        'domain' => $_SERVER['HTTP_HOST'],
263
        'url'    => '/files/download/' . $company->getFile()->getId()
264
      ];
265
266
    return null;
267
  }
268
}
269