Passed
Push — master ( ad95a6...099c84 )
by Luiz Kim
04:28 queued 02:01
created

PeopleService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 5
b 0
f 0
nc 1
nop 3
dl 0
loc 6
rs 10
1
<?php
2
3
namespace ControleOnline\Service;
4
5
use ControleOnline\Entity\Document;
6
use ControleOnline\Entity\DocumentType;
7
use ControleOnline\Entity\Language;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\Language 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...
8
use ControleOnline\Entity\People;
9
use ControleOnline\Entity\PeopleLink;
10
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...
11
use Symfony\Component\HttpFoundation\RequestStack;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\RequestStack 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
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...
13
use Doctrine\ORM\QueryBuilder;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\QueryBuilder 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
15
class PeopleService
16
{
17
  private $request;
18
19
  public function __construct(
20
    private EntityManagerInterface $manager,
21
    private Security               $security,
22
    private RequestStack $requestStack
23
  ) {
24
    $this->request  = $requestStack->getCurrentRequest();
25
  }
26
27
  public function beforePersist(People $people)
28
  {
29
    $language = $this->manager->getRepository(Language::class)->findOneBy(['language' => 'pt-br']);
30
    $people->setLanguage($language);
31
    return $people;
32
  }
33
34
  public function addClient() {}
35
36
37
  public function discoveryPeopleByDocument($document_number, $document_type, $name = null): ?People
38
  {
39
40
    $document = $this->manager->getRepository(Document::class)->findOneBy(['document' => $document_number]);
41
    if ($document)
42
      return $document->getPeople();
43
44
45
    if ($name) {
46
47
      $people = new People();
48
      $people->setName($name);
49
      $people->setLanguage($this->manager->getRepository(Language::class)->findOneBy(['language' => 'pt-br']));
50
      $people->setPeopleType($document_type == 'cpf' ? 'F' : 'J');
51
52
      $document = new Document();
53
      $document->setDocument($document_number);
54
      $document->setDocumentType($this->manager->getRepository(DocumentType::class)->findOneBy(['document_type' => $document_type]));
55
      $document->setPeople($people);
56
57
      $this->manager->persist($people);
58
      $this->manager->persist($document);
59
60
      
61
      $this->manager->flush();
62
63
      return $people;
64
    }
65
  }
66
67
68
  public function afterPersist(People $people)
69
  {
70
    $request = $this->requestStack->getCurrentRequest();
71
    $payload   = json_decode($request->getContent());
72
    if (isset($payload->link_type)) {
73
      $company = $this->manager->getRepository(People::class)->find(preg_replace('/\D/', '', $payload->company));
74
      if ($company)
75
        $this->addLink($company, $people, $payload->link_type);
76
      else {
77
        $link = $this->manager->getRepository(People::class)->find(preg_replace('/\D/', '', $payload->link));
78
        if ($payload->link_type == 'employee' && $link) {
79
          $this->addLink($people, $link, $payload->link_type);
80
          if ($payload->people_document) {
81
            $document_type = $this->manager->getRepository(DocumentType::class)->findOneBy(['document_type' => 'cnpj']);
82
83
            $document = new Document();
84
            $document->setPeople($people);
85
            $document->setDocumentType($document_type);
86
            $document->setDocument($payload->people_document);
87
            $this->manager->persist($document);
88
            $this->manager->flush();
89
          }
90
        }
91
      }
92
    }
93
  }
94
95
  public function addLink(People $company, People $people, $link_type)
96
  {
97
98
    $peopleLink = $this->manager->getRepository(PeopleLink::class)->findOneBy([
99
      'company' => $company,
100
      'people' => $people,
101
      'link_type' => $link_type
102
    ]);
103
104
    if (!$peopleLink)
105
      $peopleLink = new PeopleLink();
106
107
    $peopleLink->setCompany($company);
108
    $peopleLink->setPeople($people);
109
    $peopleLink->setLinkType($link_type);
110
111
    $this->manager->persist($peopleLink);
112
    $this->manager->flush();
113
    return  $peopleLink;
114
  }
115
116
  public function secutiryFilter(QueryBuilder $queryBuilder, $resourceClass = null, $applyTo = null, $rootAlias = null): void
117
  {
118
    $this->checkLink($queryBuilder, $resourceClass, $applyTo, $rootAlias);
119
  }
120
121
  public function checkLink(QueryBuilder $queryBuilder, $resourceClass = null, $applyTo = null, $rootAlias = null): void
0 ignored issues
show
Unused Code introduced by
The parameter $applyTo is not used and could be removed. ( Ignorable by Annotation )

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

121
  public function checkLink(QueryBuilder $queryBuilder, $resourceClass = null, /** @scrutinizer ignore-unused */ $applyTo = null, $rootAlias = null): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $resourceClass is not used and could be removed. ( Ignorable by Annotation )

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

121
  public function checkLink(QueryBuilder $queryBuilder, /** @scrutinizer ignore-unused */ $resourceClass = null, $applyTo = null, $rootAlias = null): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
122
  {
123
124
    $link   = $this->request->query->get('link',   null);
125
    $company = $this->request->query->get('company', null);
126
    $link_type = $this->request->query->get('link_type', null);
127
128
    if ($link_type) {
129
      $queryBuilder->join(sprintf('%s.' . ($link ? 'company' : 'link'), $rootAlias), 'PeopleLink');
130
      $queryBuilder->andWhere('PeopleLink.link_type IN(:link_type)');
131
      $queryBuilder->setParameter('link_type', $link_type);
132
    }
133
134
    if ($company || $link) {
135
      $queryBuilder->andWhere('PeopleLink.' . ($link ? 'people' : 'company') . ' IN(:people)');
136
      $queryBuilder->setParameter('people', preg_replace("/[^0-9]/", "", ($link ?: $company)));
137
    }
138
  }
139
  public function checkCompany($type, QueryBuilder $queryBuilder, $resourceClass = null, $applyTo = null, $rootAlias = null): void
0 ignored issues
show
Unused Code introduced by
The parameter $resourceClass is not used and could be removed. ( Ignorable by Annotation )

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

139
  public function checkCompany($type, QueryBuilder $queryBuilder, /** @scrutinizer ignore-unused */ $resourceClass = null, $applyTo = null, $rootAlias = null): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $applyTo is not used and could be removed. ( Ignorable by Annotation )

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

139
  public function checkCompany($type, QueryBuilder $queryBuilder, $resourceClass = null, /** @scrutinizer ignore-unused */ $applyTo = null, $rootAlias = null): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
140
  {
141
    $companies   = $this->getMyCompanies();
142
    $queryBuilder->andWhere(sprintf('%s.' . $type . ' IN(:companies)', $rootAlias, $rootAlias));
143
    $queryBuilder->setParameter('companies', $companies);
144
145
    if ($payer = $this->request->query->get('company', null)) {
146
      $queryBuilder->andWhere(sprintf('%s.' . $type . ' IN(:people)', $rootAlias));
147
      $queryBuilder->setParameter('people', preg_replace("/[^0-9]/", "", $payer));
148
    }
149
  }
150
151
152
153
  public function getMyCompanies(): array
154
  {
155
    /**
156
     * @var \ControleOnline\Entity\User $currentUser
157
     */
158
    $currentUser  = $this->security->getUser();
159
    $companies    = [];
160
    if (!$currentUser)
0 ignored issues
show
introduced by
$currentUser is of type ControleOnline\Entity\User, thus it always evaluated to true.
Loading history...
161
      return [];
162
163
    if (!$currentUser->getPeople()->getLink()->isEmpty()) {
164
      foreach ($currentUser->getPeople()->getLink() as $company) {
165
        $companies[] = $company->getCompany();
166
      }
167
    }
168
    return $companies;
169
  }
170
}
171