Passed
Push — master ( ec46a6...3fc116 )
by Luiz Kim
02:23
created

PeopleService::checkLink()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 10
c 1
b 0
f 0
nc 4
nop 4
dl 0
loc 16
rs 8.8333
1
<?php
2
3
namespace ControleOnline\Service;
4
5
use ControleOnline\Entity\Document;
6
use ControleOnline\Entity\DocumentType;
7
use ControleOnline\Entity\Email;
8
use ControleOnline\Entity\ExtraData;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\ExtraData 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 ControleOnline\Entity\ExtraFields;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\ExtraFields 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\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...
11
use ControleOnline\Entity\People;
12
use ControleOnline\Entity\PeopleLink;
13
use ControleOnline\Entity\Phone;
14
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...
15
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...
16
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...
17
as Security;
18
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...
19
use Exception;
20
21
class PeopleService
22
{
23
  private $request;
24
25
  public function __construct(
26
    private EntityManagerInterface $manager,
27
    private Security               $security,
28
    private RequestStack $requestStack,
29
  ) {
30
    $this->request  = $requestStack->getCurrentRequest();
31
  }
32
33
  public function prePersist(People $people)
34
  {
35
    $language = $this->manager->getRepository(Language::class)->findOneBy(['language' => 'pt-br']);
36
    $people->setLanguage($language);
37
    return $people;
38
  }
39
40
  public function addClient() {}
41
42
43
  public function discoveryClient(People $provider, People $client) {}
0 ignored issues
show
Unused Code introduced by
The parameter $provider 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

43
  public function discoveryClient(/** @scrutinizer ignore-unused */ People $provider, People $client) {}

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 $client 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

43
  public function discoveryClient(People $provider, /** @scrutinizer ignore-unused */ People $client) {}

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...
44
45
  public function discoveryPeople(?string $document = null, ?string  $email = null, ?array $phone = [], ?string $name = null, ?string $peopleType = null): People
46
  {
47
48
    if (!empty($document))
49
      $people = $this->getDocument($document)?->getPeople();
50
51
    if (!$people && !empty($email))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $people does not seem to be defined for all execution paths leading up to this point.
Loading history...
52
      $people = $this->getEmail($email)?->getPeople();
53
54
    if (!$people && !empty($phone))
55
      $people = $this->getPhone($phone['ddd'], $phone['phone'])?->getPeople();
56
57
    if (!$people) {
58
      $people = new People();
59
      $people->setName($name ?? 'Name not given');
60
      $people->setLanguage($this->manager->getRepository(Language::class)->findOneBy(['language' => 'pt-br']));
61
      $people->setPeopleType($peopleType ?: $this->getPeopleTypeByDocumentLen($document));
62
      $this->manager->persist($people);
63
      $this->manager->flush();
64
    }
65
66
    if ($document)
67
      $this->addDocument($people, $document);
68
    if ($email)
69
      $this->addEmail($people, $email);
70
    if ($phone)
71
      $this->addPhone($people, $phone);
72
73
74
    return $people;
75
  }
76
77
  public function addPhone(People $people, array $phone_number): Phone
78
  {
79
    $phone = $this->getPhone($phone_number['ddd'], $phone_number['phone']);
80
    if ($phone && $phone->getPeople()) {
81
      if ($phone->getPeople()->getId() != $people->getId())
82
        throw new Exception("Phone is in use by people " . $people->getId(), 1);
83
    } else {
84
      $phone = new Phone();
85
      $phone->setDdd($phone_number['ddd']);
86
      $phone->setPhone($phone_number['phone']);
87
      $phone->setPeople($people);
88
      $this->manager->persist($phone);
89
      $this->manager->flush();
90
    }
91
92
    return  $phone;
93
  }
94
  public function addDocument(People $people, string $document_number, ?string $document_type = null): Document
95
  {
96
    $document = $this->getDocument($document_number, $document_type);
97
    if ($document) {
98
      if ($document->getPeople()->getId() != $people->getId())
99
        throw new Exception("Document is in use by people " . $people->getId(), 1);
100
    } else {
101
      $document = new Document();
102
      $document->setDocument($document_number);
0 ignored issues
show
Bug introduced by
$document_number of type string is incompatible with the type integer expected by parameter $document of ControleOnline\Entity\Document::setDocument(). ( Ignorable by Annotation )

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

102
      $document->setDocument(/** @scrutinizer ignore-type */ $document_number);
Loading history...
103
      $document->setDocumentType($this->discoveryDocumentType($document_type));
0 ignored issues
show
Bug introduced by
It seems like $document_type can also be of type null; however, parameter $document_type of ControleOnline\Service\P...discoveryDocumentType() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

103
      $document->setDocumentType($this->discoveryDocumentType(/** @scrutinizer ignore-type */ $document_type));
Loading history...
104
      $document->setPeople($people);
105
      $this->manager->persist($document);
106
      $this->manager->flush();
107
    }
108
109
    return  $document;
110
  }
111
112
  public function addEmail(People $people, string $email_str): Email
113
  {
114
    $email = $this->getEmail($email_str);
115
    if ($email && $email->getPeople()) {
116
      if ($email->getPeople()->getId() != $people->getId())
117
        throw new Exception("Email is in use by people " . $people->getId(), 1);
118
    } else {
119
      $email = new Email();
120
      $email->setEmail($email_str);
121
      $email->setPeople($people);
122
      $this->manager->persist($email);
123
      $this->manager->flush();
124
    }
125
126
    return  $email;
127
  }
128
129
  public function getEmail(string $email): ?Email
130
  {
131
    return $this->manager->getRepository(Email::class)->findOneBy(['email' => $email]);
132
  }
133
134
  public function getPhone(string $ddd, string $phone): ?Phone
135
  {
136
    return $this->manager->getRepository(Phone::class)->findOneBy(['ddd' => $ddd, 'ddd' => $phone]);
137
  }
138
139
140
  public function discoveryDocumentType(string $document_type): DocumentType
141
  {
142
    $documentType =  $this->manager->getRepository(DocumentType::class)->findOneBy(['documentType' => $document_type]);
143
144
    if (!$documentType) {
145
      $documentType = new DocumentType();
146
      $documentType->setDocumentType($document_type);
147
      $this->manager->persist($documentType);
148
      $this->manager->flush();
149
    }
150
151
    return $documentType;
152
  }
153
154
  public function getDocument(string $document_number, ?string $document_type = null): ?Document
155
  {
156
    if (!$document_type)
157
      $document_type = $this->getDocumentTypeByDocumentLen($document_number);
158
    return $this->manager->getRepository(Document::class)->findOneBy([
159
      'document' => $document_number,
160
      'documentType' =>
161
      $this->discoveryDocumentType($document_type)
162
    ]);
163
  }
164
165
  public function getPeopleTypeByDocumentLen(?string $document_number = null)
166
  {
167
    return strlen($document_number) > 11 ? 'J' : 'F';
0 ignored issues
show
Bug introduced by
It seems like $document_number can also be of type null; however, parameter $string of strlen() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

167
    return strlen(/** @scrutinizer ignore-type */ $document_number) > 11 ? 'J' : 'F';
Loading history...
168
  }
169
170
  public function getDocumentTypeByDocumentLen(?string $document_number = null)
171
  {
172
    return strlen($document_number) > 11 ? 'CNPJ' : 'CPF';
0 ignored issues
show
Bug introduced by
It seems like $document_number can also be of type null; however, parameter $string of strlen() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

172
    return strlen(/** @scrutinizer ignore-type */ $document_number) > 11 ? 'CNPJ' : 'CPF';
Loading history...
173
  }
174
175
  public function postPersist(People $people)
176
  {
177
    $request = $this->requestStack->getCurrentRequest();
178
    if (!$request) return;
179
    $payload   = json_decode($request->getContent());
180
    if (isset($payload->link_type)) {
181
      $company = $this->manager->getRepository(People::class)->find(preg_replace('/\D/', '', $payload->company));
182
      if ($company)
183
        $this->addLink($company, $people, $payload->link_type);
184
      else {
185
        $link = $this->manager->getRepository(People::class)->find(preg_replace('/\D/', '', $payload->link));
186
        if ($payload->link_type == 'employee' && $link) {
187
          $this->addLink($people, $link, $payload->link_type);
188
          if ($payload->people_document)
189
            $this->addDocument($people, $payload->people_document);
190
        }
191
      }
192
    }
193
  }
194
195
  public function addLink(People $company, People $people, $link_type)
196
  {
197
198
    $peopleLink = $this->manager->getRepository(PeopleLink::class)->findOneBy([
199
      'company' => $company,
200
      'people' => $people,
201
      'link_type' => $link_type
202
    ]);
203
204
    if (!$peopleLink)
205
      $peopleLink = new PeopleLink();
206
207
    $peopleLink->setCompany($company);
208
    $peopleLink->setPeople($people);
209
    $peopleLink->setLinkType($link_type);
210
211
    $this->manager->persist($peopleLink);
212
    $this->manager->flush();
213
    return  $peopleLink;
214
  }
215
216
  public function securityFilter(QueryBuilder $queryBuilder, $resourceClass = null, $applyTo = null, $rootAlias = null): void
217
  {
218
    $this->checkLink($queryBuilder, $resourceClass, $applyTo, $rootAlias);
219
  }
220
221
  public function checkLink(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

221
  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...
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

221
  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...
222
  {
223
224
    $link   = $this->request->query->get('link',   null);
225
    $company = $this->request->query->get('company', null);
226
    $link_type = $this->request->query->get('link_type', null);
227
228
    if ($link_type) {
229
      $queryBuilder->join(sprintf('%s.' . ($link ? 'company' : 'link'), $rootAlias), 'PeopleLink');
230
      $queryBuilder->andWhere('PeopleLink.link_type IN(:link_type)');
231
      $queryBuilder->setParameter('link_type', $link_type);
232
    }
233
234
    if ($company || $link) {
235
      $queryBuilder->andWhere('PeopleLink.' . ($link ? 'people' : 'company') . ' IN(:people)');
236
      $queryBuilder->setParameter('people', preg_replace("/[^0-9]/", "", ($link ?: $company)));
237
    }
238
  }
239
  public function checkCompany($type, 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

239
  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...
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

239
  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...
240
  {
241
    $companies   = $this->getMyCompanies();
242
    $queryBuilder->andWhere(sprintf('%s.' . $type . ' IN(:companies)', $rootAlias, $rootAlias));
243
    $queryBuilder->setParameter('companies', $companies);
244
245
    if ($payer = $this->request->query->get('company', null)) {
246
      $queryBuilder->andWhere(sprintf('%s.' . $type . ' IN(:people)', $rootAlias));
247
      $queryBuilder->setParameter('people', preg_replace("/[^0-9]/", "", $payer));
248
    }
249
  }
250
251
  public function getMyCompanies(): array
252
  {
253
    /**
254
     * @var \ControleOnline\Entity\User $currentUser
255
     */
256
    $token = $this->security->getToken();
257
    if (!$token) return [];
258
    $currentUser  =  $token->getUser();
259
    $companies    = [];
260
    if (!$currentUser) return [];
261
262
    if (!$currentUser->getPeople()->getLink()->isEmpty()) {
263
      foreach ($currentUser->getPeople()->getLink() as $company) {
264
        $companies[] = $company->getCompany();
265
      }
266
    }
267
    return $companies;
268
  }
269
}
270