Passed
Push — master ( be26c9...87f8af )
by Luiz Kim
02:08
created

PeopleRoleService   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 68
c 1
b 0
f 0
dl 0
loc 165
rs 10
wmc 21

7 Methods

Rating   Name   Duplication   Size   Complexity  
A isSuperAdmin() 0 3 1
A getMainCompany() 0 11 2
A getAllRoles() 0 20 4
A __construct() 0 8 1
A isSalesman() 0 3 1
B getAllRolesByCompany() 0 77 11
A isFranchisee() 0 3 1
1
<?php
2
3
namespace ControleOnline\Service;
4
5
use ControleOnline\Entity\People;
6
use ControleOnline\Entity\PeopleDomain;
7
use ControleOnline\Entity\PeopleLink;
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 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...
10
use App\Service\PeopleService;
0 ignored issues
show
Bug introduced by
The type App\Service\PeopleService 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
class PeopleRoleService
13
{
14
  /**
15
   * Entity Manager
16
   *
17
   * @var EntityManagerInterface
18
   */
19
  private $manager = null;
20
21
  /**
22
   * Security
23
   *
24
   * @var Security
25
   */
26
  private $security = null;
27
28
  /**
29
   * People Service
30
   *
31
   * @var PeopleService
32
   */
33
  private $people  = null;
34
35
  public function __construct(
36
    EntityManagerInterface $entityManager,
37
    Security               $security,
38
    PeopleService          $peopleService
39
  ) {
40
    $this->manager  = $entityManager;
41
    $this->security = $security;
42
    $this->people   = $peopleService;
43
  }
44
45
  public function isFranchisee(People $people): bool
46
  {
47
    return in_array('franchisee', $this->getAllRoles($people));
48
  }
49
50
  public function isSuperAdmin(People $people): bool
51
  {
52
    return in_array('super', $this->getAllRoles($people));
53
  }
54
55
  public function isSalesman(People $people): bool
56
  {
57
    return in_array('salesman', $this->getAllRoles($people));
58
  }
59
60
  public function getAllRoles(People $people = null): array
61
  {
62
63
    $mainCompany = $this->getMainCompany();
0 ignored issues
show
Unused Code introduced by
The assignment to $mainCompany is dead and can be removed.
Loading history...
64
65
66
    if ($people === null) {
67
      $people = $this->security->getUser()->getPeople();
68
      if (!($people instanceof People)) {
69
        return ['guest'];
70
      }
71
    } else {
72
      $peopleCompany = $people->getLink()->first();
73
      if ($peopleCompany === false) {
74
        return ['guest'];
75
      }
76
    }
77
78
    $myCompany  = $peopleCompany->getCompany();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $peopleCompany does not seem to be defined for all execution paths leading up to this point.
Loading history...
79
    return $this->getAllRolesByCompany($people, $myCompany);
80
  }
81
82
  public function getAllRolesByCompany(People $people, People $company): array
83
  {
84
    $peopleRole = [];
85
    $mainCompany = $this->getMainCompany();
86
87
    if ($company->getId() == $mainCompany->getId()) {
88
      $isSuper = $mainCompany->getCompany()
0 ignored issues
show
Bug introduced by
The method getCompany() does not exist on ControleOnline\Entity\People. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
      $isSuper = $mainCompany->/** @scrutinizer ignore-call */ getCompany()

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
89
        ->exists(
90
          function ($key, PeopleLink $peopleLink) use ($people) {
91
            return $peopleLink->getPeople()->getId() === $people->getId();
92
          }
93
        );
94
95
      if ($isSuper) {
96
        $peopleRole[] = 'super';
97
      }
98
    }
99
100
    $peopleFranchisee = $this->manager->getRepository(People::class)->getPeopleLink($mainCompany, 'franchisee');
0 ignored issues
show
Unused Code introduced by
The assignment to $peopleFranchisee is dead and can be removed.
Loading history...
101
102
    $isFranchisee = $mainCompany->getCompany()
103
      ->exists(
104
        function ($key, PeopleLink $peopleFranchisee) use ($people, $company) {
105
          foreach ($peopleFranchisee->getCompany() as $peopleLink) {
106
            if ($peopleLink->getCompany()->getId() == $company->getId() &&  $peopleLink->getPeople()->getId() === $people->getId()) {
107
              return  true;
108
            }
109
          }
110
        }
111
      );
112
      $peopleRole[] = 'franchisee';
113
    if ($isFranchisee) {
114
      $peopleRole[] = 'franchisee';
115
      $peopleRole[] = 'admin';
116
    }
117
118
    $isClient = $company->getCompany()
119
      ->exists(
120
        function ($key, PeopleLink $peopleLink) use ($people) {
121
          return $peopleLink->getPeople()->getId() === $people->getId();
122
        }
123
      );
124
125
    if ($isClient) {
126
      $peopleRole[] = 'client';
127
    }
128
129
130
    $isClient = $people->getLink()
131
      ->exists(
132
        function ($key, PeopleLink $peopleLink) use ($company) {
133
          return $this->manager->getRepository(PeopleClient::class)->findOneBy(
0 ignored issues
show
Bug introduced by
The type ControleOnline\Service\PeopleClient 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...
134
            [
135
              'client' => $peopleLink->getCompany(),
136
              'company_id' => $company->getId()
137
            ]
138
          );
139
        }
140
      );
141
142
    if ($isClient) {
143
      $peopleRole[] = 'client';
144
    }
145
146
147
    $isSalesman = $company->getPeopleSalesman()
0 ignored issues
show
Bug introduced by
The method getPeopleSalesman() does not exist on ControleOnline\Entity\People. Did you maybe mean getPeople()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

147
    $isSalesman = $company->/** @scrutinizer ignore-call */ getPeopleSalesman()

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
148
      ->exists(
149
        function ($key, PeopleSalesman $peopleSalesman) use ($people) {
0 ignored issues
show
Bug introduced by
The type ControleOnline\Service\PeopleSalesman 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...
150
          return $peopleSalesman->getSalesman()->getId() === $people->getId();
151
        }
152
      );
153
    if ($isSalesman) {
154
      $peopleRole[] = 'salesman';
155
    }
156
157
158
    return array_values(array_unique(empty($peopleRole) ? ['guest'] : $peopleRole));
159
  }
160
161
  /**
162
   * Retorna a people da empresa principal segundo o dominio da api
163
   *
164
   * @return People
165
   */
166
  public function getMainCompany(): People
167
  {
168
    $domain  = $_SERVER['HTTP_HOST'];
169
    $company = $this->manager->getRepository(PeopleDomain::class)->findOneBy(['domain' => $domain]);
170
171
    if ($company === null)
172
      throw new \Exception(
173
        sprintf('Main company "%s" not found', $domain)
174
      );
175
176
    return $company->getPeople();
177
  }
178
}
179