Passed
Push — master ( 87f8af...7889cb )
by Luiz Kim
03:41 queued 01:37
created

PeopleRoleService   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 32
c 2
b 0
f 0
dl 0
loc 95
rs 10
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A isSuperAdmin() 0 3 1
A __construct() 0 8 1
A isSalesman() 0 3 1
A isFranchisee() 0 3 1
A getMainCompany() 0 11 2
A getAllRoles() 0 28 6
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
61
  public function getAllRoles(People $people): array
62
  {
63
    $peopleRole = [];
64
    $mainCompany = $this->getMainCompany();
65
66
    $isSuper = $this->manager->getRepository(People::class)->getCompanyPeopleLinks($mainCompany, $people, 'employee', 1);
67
    if ($isSuper) 
68
      $peopleRole[] = 'super';
69
    
70
71
72
    $isFranchisee = $this->manager->getRepository(People::class)->getCompanyPeopleLinks($mainCompany, $people, 'franchisee', 1);
73
    if ($isFranchisee) {
74
      $peopleRole[] = 'franchisee';
75
      $peopleRole[] = 'admin';
76
    }
77
78
    $isClient = $this->manager->getRepository(People::class)->getCompanyPeopleLinks($mainCompany, $people, 'client', 1);
79
    if ($isClient) 
80
      $peopleRole[] = 'client';
81
    
82
83
    $isSalesman = $this->manager->getRepository(People::class)->getCompanyPeopleLinks($mainCompany, $people, 'salesman', 1);
84
    if ($isSalesman) 
85
      $peopleRole[] = 'salesman';
86
    
87
88
    return array_values(array_unique(empty($peopleRole) ? ['guest'] : $peopleRole));
89
  }
90
91
  /**
92
   * Retorna a people da empresa principal segundo o dominio da api
93
   *
94
   * @return People
95
   */
96
  public function getMainCompany(): People
97
  {
98
    $domain  = $_SERVER['HTTP_HOST'];
99
    $company = $this->manager->getRepository(PeopleDomain::class)->findOneBy(['domain' => $domain]);
100
101
    if ($company === null)
102
      throw new \Exception(
103
        sprintf('Main company "%s" not found', $domain)
104
      );
105
106
    return $company->getPeople();
107
  }
108
}
109