Passed
Push — master ( 929af5...8f450c )
by Luiz Kim
04:31 queued 02:06
created

GetMyCompaniesAction::getLogo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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