Passed
Push — master ( c9bac0...64d18e )
by Luiz Kim
02:34
created

PeopleRoleService   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 10
Bugs 0 Features 0
Metric Value
eloc 38
c 10
b 0
f 0
dl 0
loc 88
rs 10
wmc 15

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A isSalesman() 0 20 4
A isFranchisee() 0 5 1
A getMainCompany() 0 9 2
B getAllRoles() 0 30 7
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\Authentication\Token\Storage\TokenStorageInterface
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Securi...e\TokenStorageInterface 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
 AS Security;
11
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...
12
13
class PeopleRoleService
14
{
15
16
  private static $mainCompany;
17
18
  public function __construct(
19
    private EntityManagerInterface $manager,
20
    private Security               $security,
21
    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...
22
  ) {}
23
24
  public function isFranchisee(People $people)
25
  {
26
    $mainCompany = $this->getMainCompany();
27
28
    return $this->manager->getRepository(People::class)->getCompanyPeopleLinks($mainCompany, $people, 'franchisee', 1);
29
  }
30
31
32
  public function isSalesman(People $people)
33
  {
34
    $mainCompany = $this->getMainCompany();
35
    $isSalesman = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $isSalesman is dead and can be removed.
Loading history...
36
37
    $isSalesman = $this->manager->getRepository(People::class)->getCompanyPeopleLinks($mainCompany, $people, 'salesman', 1);
38
    if ($isSalesman) return true;
39
40
    $getPeopleCompanies = $this->manager->getRepository(PeopleLink::class)->findBy([
41
      'people' => $people,
42
      'link_type' => 'employee'
43
    ]);
44
    /**
45
     * @var \ControleOnline\Entity\PeopleLink $peopleCompany
46
     */
47
    foreach ($getPeopleCompanies as $peopleCompany) {
48
      $isSalesman = $this->manager->getRepository(People::class)->getCompanyPeopleLinks($mainCompany, $peopleCompany->getCompany(), 'salesman', 1);
49
      if ($isSalesman) return true;
50
    }
51
    return $isSalesman;
52
  }
53
54
55
  public function getAllRoles(People $people): array
56
  {
57
    $peopleRole = [];
58
    $mainCompany = $this->getMainCompany();
59
60
    $isSuper = $this->manager->getRepository(People::class)->getCompanyPeopleLinks($mainCompany, $people, 'employee', 1);
61
    if ($isSuper)
62
      $peopleRole[] = 'super';
63
64
    $family = $this->manager->getRepository(People::class)->getCompanyPeopleLinks($mainCompany, $people, 'family', 1);
65
    if ($family)
66
      $peopleRole[] = 'family';    
67
68
    $isFranchisee = $this->isFranchisee($people);
69
    if ($isFranchisee) {
70
      $peopleRole[] = 'franchisee';
71
      $peopleRole[] = 'admin';
72
    }
73
74
    $isClient = $this->manager->getRepository(People::class)->getCompanyPeopleLinks($mainCompany, $people, 'client', 1);
75
    if ($isClient)
76
      $peopleRole[] = 'client';
77
78
79
    $isSalesman = $this->isSalesman($people);
80
    if ($isSalesman)
81
      $peopleRole[] = 'salesman';
82
83
84
    return array_values(array_unique(empty($peopleRole) ? ['guest'] : $peopleRole));
85
  }
86
87
  /**
88
   * Retorna a people da empresa principal segundo o dominio da api
89
   *
90
   * @return People
91
   */
92
  public function getMainCompany(): People
93
  {
94
95
    if (self::$mainCompany) return self::$mainCompany;
96
97
    $peopleDomain = $this->domainService->getPeopleDomain();
98
    self::$mainCompany =  $peopleDomain->getPeople();
99
100
    return self::$mainCompany;
101
  }
102
}
103