Passed
Push — master ( 28aa0e...0fd630 )
by Luiz Kim
02:32
created

PeopleRoleService::getAllRoles()   A

Complexity

Conditions 6
Paths 16

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 6
eloc 16
c 4
b 0
f 0
nc 16
nop 1
dl 0
loc 28
rs 9.1111
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 ControleOnline\Entity\User;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\User 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
  private static $mainCompany;
16
17
  public function __construct(
18
    private EntityManagerInterface $manager,
19
    private Security               $security,
20
    private DomainService          $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...
21
  ) {}
22
23
  public function isFranchisee(People $people)
24
  {
25
    $mainCompany = $this->getMainCompany();
26
    $isFranchisee = false;
27
28
    $getPeopleCompanies = $this->manager->getRepository(PeopleLink::class)->findBy([
29
      'people' => $people,
30
      'link_type' => 'employee'
31
    ]);
32
    /**
33
     * @var \ControleOnline\Entity\PeopleLink $peopleCompany
34
     */
35
    foreach ($getPeopleCompanies as $peopleCompany) {
36
      $isFranchisee = $this->manager->getRepository(People::class)->getCompanyPeopleLinks($mainCompany, $peopleCompany->getCompany(), 'franchisee', 1);
37
      if ($isFranchisee) return true;
38
    }
39
    return $isFranchisee;
40
  }
41
42
43
  public function isSalesman(People $people)
44
  {
45
    $mainCompany = $this->getMainCompany();
46
    $isSalesman = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $isSalesman is dead and can be removed.
Loading history...
47
48
    $isSalesman = $this->manager->getRepository(People::class)->getCompanyPeopleLinks($mainCompany, $people, 'salesman', 1);
49
    if ($isSalesman) return true;
50
51
    $getPeopleCompanies = $this->manager->getRepository(PeopleLink::class)->findBy([
52
      'people' => $people,
53
      'link_type' => 'employee'
54
    ]);
55
    /**
56
     * @var \ControleOnline\Entity\PeopleLink $peopleCompany
57
     */
58
    foreach ($getPeopleCompanies as $peopleCompany) {
59
      $isSalesman = $this->manager->getRepository(People::class)->getCompanyPeopleLinks($mainCompany, $peopleCompany->getCompany(), 'salesman', 1);
60
      if ($isSalesman) return true;
61
    }
62
    return $isSalesman;
63
  }
64
65
66
  public function getAllRoles(People $people): array
67
  {
68
    $peopleRole = [];
69
    $mainCompany = $this->getMainCompany();
70
71
    $isSuper = $this->manager->getRepository(People::class)->getCompanyPeopleLinks($mainCompany, $people, 'employee', 1);
72
    if ($isSuper)
73
      $peopleRole[] = 'super';
74
75
76
77
    $isFranchisee = $this->isFranchisee($people);
78
    if ($isFranchisee) {
79
      $peopleRole[] = 'franchisee';
80
      $peopleRole[] = 'admin';
81
    }
82
83
    $isClient = $this->manager->getRepository(People::class)->getCompanyPeopleLinks($mainCompany, $people, 'client', 1);
84
    if ($isClient)
85
      $peopleRole[] = 'client';
86
87
88
    $isSalesman = $this->isSalesman($people);
89
    if ($isSalesman)
90
      $peopleRole[] = 'salesman';
91
92
93
    return array_values(array_unique(empty($peopleRole) ? ['guest'] : $peopleRole));
94
  }
95
96
  /**
97
   * Retorna a people da empresa principal segundo o dominio da api
98
   *
99
   * @return People
100
   */
101
  public function getMainCompany(): People
102
  {
103
104
    if (self::$mainCompany) return self::$mainCompany;
105
106
    $peopleDomain = $this->domainService->getPeopleDomain();
107
    self::$mainCompany =  $peopleDomain->getPeople();
108
    
109
    return self::$mainCompany;
110
  }
111
}
112