ControleOnline /
api-platform-people
| 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
|
|||||||
| 9 | use ControleOnline\Entity\ExtraFields; |
||||||
|
0 ignored issues
–
show
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. 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
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. 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
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. 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
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. 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
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. 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
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. 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) |
||||||
| 44 | { |
||||||
| 45 | return $this->discoveryLink($provider, $client, 'client'); |
||||||
| 46 | } |
||||||
| 47 | |||||||
| 48 | public function discoveryLink(People $company, People $people, $linkType): PeopleLink |
||||||
| 49 | { |
||||||
| 50 | $peopleLink = $this->manager->getRepository(PeopleLink::class)->findOneBy([ |
||||||
| 51 | 'company' => $company, |
||||||
| 52 | 'people' => $people, |
||||||
| 53 | 'link_type' => $linkType |
||||||
| 54 | ]); |
||||||
| 55 | |||||||
| 56 | if (!$peopleLink) |
||||||
| 57 | $peopleLink = $this->addLink($company, $people, $linkType); |
||||||
| 58 | |||||||
| 59 | return $peopleLink; |
||||||
| 60 | } |
||||||
| 61 | |||||||
| 62 | public function discoveryPeople(?string $document = null, ?string $email = null, ?array $phone = [], ?string $name = null, ?string $peopleType = null): People |
||||||
| 63 | { |
||||||
| 64 | |||||||
| 65 | if (!empty($document)) |
||||||
| 66 | $people = $this->getDocument($document)?->getPeople(); |
||||||
| 67 | |||||||
| 68 | if (!$people && !empty($email)) |
||||||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||||
| 69 | $people = $this->getEmail($email)?->getPeople(); |
||||||
| 70 | |||||||
| 71 | if (!$people && !empty($phone)) |
||||||
| 72 | $people = $this->getPhone($phone['ddi'], $phone['ddd'], $phone['phone'])?->getPeople(); |
||||||
| 73 | |||||||
| 74 | if (!$people) { |
||||||
| 75 | $people = new People(); |
||||||
| 76 | $people->setName($name ?? 'Name not given'); |
||||||
| 77 | $people->setLanguage($this->manager->getRepository(Language::class)->findOneBy(['language' => 'pt-br'])); |
||||||
| 78 | $people->setPeopleType($peopleType ?: $this->getPeopleTypeByDocumentLen($document)); |
||||||
| 79 | $this->manager->persist($people); |
||||||
| 80 | $this->manager->flush(); |
||||||
| 81 | } |
||||||
| 82 | |||||||
| 83 | if ($document) |
||||||
| 84 | $this->addDocument($people, $document); |
||||||
| 85 | if ($email) |
||||||
| 86 | $this->addEmail($people, $email); |
||||||
| 87 | if ($phone) |
||||||
| 88 | $this->addPhone($people, $phone); |
||||||
| 89 | |||||||
| 90 | |||||||
| 91 | return $people; |
||||||
| 92 | } |
||||||
| 93 | |||||||
| 94 | public function addPhone(People $people, array $phone_number): Phone |
||||||
| 95 | { |
||||||
| 96 | $phone = $this->getPhone($phone_number['ddi'], $phone_number['ddd'], $phone_number['phone']); |
||||||
| 97 | if ($phone && $phone->getPeople()) { |
||||||
| 98 | if ($phone->getPeople()->getId() != $people->getId()) |
||||||
| 99 | throw new Exception("Phone is in use by people " . $people->getId(), 1); |
||||||
| 100 | } else { |
||||||
| 101 | $phone = new Phone(); |
||||||
| 102 | $phone->setDdi((int) $phone_number['ddi']); |
||||||
| 103 | $phone->setDdd((int) $phone_number['ddd']); |
||||||
| 104 | $phone->setPhone((int) $phone_number['phone']); |
||||||
| 105 | $phone->setPeople($people); |
||||||
| 106 | $this->manager->persist($phone); |
||||||
| 107 | $this->manager->flush(); |
||||||
| 108 | } |
||||||
| 109 | |||||||
| 110 | return $phone; |
||||||
| 111 | } |
||||||
| 112 | public function addDocument(People $people, string|int $document_number, ?string $document_type = null): Document |
||||||
| 113 | { |
||||||
| 114 | $document = $this->getDocument($document_number, $document_type); |
||||||
| 115 | if ($document) { |
||||||
| 116 | if ($document->getPeople()->getId() != $people->getId()) |
||||||
| 117 | throw new Exception("Document is in use by people " . $people->getId(), 1); |
||||||
| 118 | } else { |
||||||
| 119 | $document_type = $document_type ? $this->discoveryDocumentType($document_type) : $this->discoveryDocumentType($this->getDocumentTypeByDocumentLen($document_number)); |
||||||
| 120 | $document = new Document(); |
||||||
| 121 | $document->setDocument((int)$document_number); |
||||||
| 122 | $document->setDocumentType($document_type); |
||||||
| 123 | $document->setPeople($people); |
||||||
| 124 | $this->manager->persist($document); |
||||||
| 125 | $this->manager->flush(); |
||||||
| 126 | } |
||||||
| 127 | |||||||
| 128 | return $document; |
||||||
| 129 | } |
||||||
| 130 | |||||||
| 131 | public function addEmail(People $people, string $email_str): Email |
||||||
| 132 | { |
||||||
| 133 | $email = $this->getEmail($email_str); |
||||||
| 134 | if ($email && $email->getPeople()) { |
||||||
| 135 | if ($email->getPeople()->getId() != $people->getId()) |
||||||
| 136 | throw new Exception("Email is in use by people " . $people->getId(), 1); |
||||||
| 137 | } else { |
||||||
| 138 | $email = new Email(); |
||||||
| 139 | $email->setEmail($email_str); |
||||||
| 140 | $email->setPeople($people); |
||||||
| 141 | $this->manager->persist($email); |
||||||
| 142 | $this->manager->flush(); |
||||||
| 143 | } |
||||||
| 144 | |||||||
| 145 | return $email; |
||||||
| 146 | } |
||||||
| 147 | |||||||
| 148 | public function getEmail(string $email): ?Email |
||||||
| 149 | { |
||||||
| 150 | return $this->manager->getRepository(Email::class)->findOneBy(['email' => $email]); |
||||||
| 151 | } |
||||||
| 152 | |||||||
| 153 | public function getPhone(int $ddi, int $ddd, string $phone): ?Phone |
||||||
| 154 | { |
||||||
| 155 | return $this->manager->getRepository(Phone::class)->findOneBy([ |
||||||
| 156 | 'ddi' => $ddi, |
||||||
| 157 | 'ddd' => $ddd, |
||||||
| 158 | 'phone' => $phone |
||||||
| 159 | ]); |
||||||
| 160 | } |
||||||
| 161 | |||||||
| 162 | |||||||
| 163 | public function discoveryDocumentType(string $document_type): DocumentType |
||||||
| 164 | { |
||||||
| 165 | $documentType = $this->manager->getRepository(DocumentType::class)->findOneBy(['documentType' => $document_type]); |
||||||
| 166 | |||||||
| 167 | if (!$documentType) { |
||||||
| 168 | $documentType = new DocumentType(); |
||||||
| 169 | $documentType->setDocumentType($document_type); |
||||||
| 170 | $this->manager->persist($documentType); |
||||||
| 171 | $this->manager->flush(); |
||||||
| 172 | } |
||||||
| 173 | |||||||
| 174 | return $documentType; |
||||||
| 175 | } |
||||||
| 176 | |||||||
| 177 | public function getDocument(string $document_number, ?string $document_type = null): ?Document |
||||||
| 178 | { |
||||||
| 179 | if (!$document_type) |
||||||
| 180 | $document_type = $this->getDocumentTypeByDocumentLen($document_number); |
||||||
| 181 | return $this->manager->getRepository(Document::class)->findOneBy([ |
||||||
| 182 | 'document' => $document_number, |
||||||
| 183 | 'documentType' => |
||||||
| 184 | $this->discoveryDocumentType($document_type) |
||||||
| 185 | ]); |
||||||
| 186 | } |
||||||
| 187 | |||||||
| 188 | public function getPeopleTypeByDocumentLen(?string $document_number = null) |
||||||
| 189 | { |
||||||
| 190 | return strlen($document_number) > 11 ? 'J' : 'F'; |
||||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||||
| 191 | } |
||||||
| 192 | |||||||
| 193 | public function getDocumentTypeByDocumentLen(?string $document_number = null) |
||||||
| 194 | { |
||||||
| 195 | return strlen($document_number) > 11 ? 'CNPJ' : 'CPF'; |
||||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||||
| 196 | } |
||||||
| 197 | |||||||
| 198 | public function postPersist(People $people) |
||||||
| 199 | { |
||||||
| 200 | $request = $this->requestStack->getCurrentRequest(); |
||||||
| 201 | if (!$request) return; |
||||||
| 202 | $payload = json_decode($request->getContent()); |
||||||
| 203 | if (isset($payload->link_type)) { |
||||||
| 204 | $company = $this->manager->getRepository(People::class)->find(preg_replace('/\D/', '', $payload->company)); |
||||||
| 205 | if ($company) |
||||||
| 206 | $this->discoveryLink($company, $people, $payload->link_type); |
||||||
| 207 | else { |
||||||
| 208 | $link = $this->manager->getRepository(People::class)->find(preg_replace('/\D/', '', $payload->link)); |
||||||
| 209 | if ($payload->link_type == 'employee' && $link) { |
||||||
| 210 | $this->discoveryLink($people, $link, $payload->link_type); |
||||||
| 211 | if ($payload->people_document) |
||||||
| 212 | $this->addDocument($people, $payload->people_document); |
||||||
| 213 | } |
||||||
| 214 | } |
||||||
| 215 | } |
||||||
| 216 | } |
||||||
| 217 | |||||||
| 218 | public function addLink(People $company, People $people, $link_type): PeopleLink |
||||||
| 219 | { |
||||||
| 220 | |||||||
| 221 | $peopleLink = $this->manager->getRepository(PeopleLink::class)->findOneBy([ |
||||||
| 222 | 'company' => $company, |
||||||
| 223 | 'people' => $people, |
||||||
| 224 | 'link_type' => $link_type |
||||||
| 225 | ]); |
||||||
| 226 | |||||||
| 227 | if (!$peopleLink) |
||||||
| 228 | $peopleLink = new PeopleLink(); |
||||||
| 229 | |||||||
| 230 | $peopleLink->setCompany($company); |
||||||
| 231 | $peopleLink->setPeople($people); |
||||||
| 232 | $peopleLink->setLinkType($link_type); |
||||||
| 233 | |||||||
| 234 | $this->manager->persist($peopleLink); |
||||||
| 235 | $this->manager->flush(); |
||||||
| 236 | return $peopleLink; |
||||||
| 237 | } |
||||||
| 238 | |||||||
| 239 | public function securityFilter(QueryBuilder $queryBuilder, $resourceClass = null, $applyTo = null, $rootAlias = null): void |
||||||
| 240 | { |
||||||
| 241 | $this->checkLink($queryBuilder, $resourceClass, $applyTo, $rootAlias); |
||||||
| 242 | } |
||||||
| 243 | |||||||
| 244 | public function checkLink(QueryBuilder $queryBuilder, $resourceClass = null, $applyTo = null, $rootAlias = null): void |
||||||
|
0 ignored issues
–
show
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
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...
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
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...
|
|||||||
| 245 | { |
||||||
| 246 | |||||||
| 247 | $link = $this->request->query->get('link', null); |
||||||
| 248 | $company = $this->request->query->get('company', null); |
||||||
| 249 | $link_type = $this->request->query->get('link_type', null); |
||||||
| 250 | |||||||
| 251 | if ($link_type) { |
||||||
| 252 | $queryBuilder->join(sprintf('%s.' . ($link ? 'company' : 'link'), $rootAlias), 'PeopleLink'); |
||||||
| 253 | $queryBuilder->andWhere('PeopleLink.link_type IN(:link_type)'); |
||||||
| 254 | $queryBuilder->setParameter('link_type', $link_type); |
||||||
| 255 | } |
||||||
| 256 | |||||||
| 257 | if ($company || $link) { |
||||||
| 258 | $queryBuilder->andWhere('PeopleLink.' . ($link ? 'people' : 'company') . ' IN(:people)'); |
||||||
| 259 | $queryBuilder->setParameter('people', preg_replace("/[^0-9]/", "", ($link ?: $company))); |
||||||
| 260 | } |
||||||
| 261 | } |
||||||
| 262 | public function checkCompany($type, QueryBuilder $queryBuilder, $resourceClass = null, $applyTo = null, $rootAlias = null): void |
||||||
|
0 ignored issues
–
show
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
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...
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
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...
|
|||||||
| 263 | { |
||||||
| 264 | $companies = $this->getMyCompanies(); |
||||||
| 265 | $queryBuilder->andWhere(sprintf('%s.' . $type . ' IN(:companies)', $rootAlias, $rootAlias)); |
||||||
| 266 | $queryBuilder->setParameter('companies', $companies); |
||||||
| 267 | |||||||
| 268 | if ($payer = $this->request->query->get('company', null)) { |
||||||
| 269 | $queryBuilder->andWhere(sprintf('%s.' . $type . ' IN(:people)', $rootAlias)); |
||||||
| 270 | $queryBuilder->setParameter('people', preg_replace("/[^0-9]/", "", $payer)); |
||||||
| 271 | } |
||||||
| 272 | } |
||||||
| 273 | |||||||
| 274 | public function getMyCompanies(): array |
||||||
| 275 | { |
||||||
| 276 | /** |
||||||
| 277 | * @var \ControleOnline\Entity\User $currentUser |
||||||
| 278 | */ |
||||||
| 279 | $token = $this->security->getToken(); |
||||||
| 280 | if (!$token) return []; |
||||||
| 281 | $currentUser = $token->getUser(); |
||||||
| 282 | $companies = []; |
||||||
| 283 | if (!$currentUser) return []; |
||||||
| 284 | |||||||
| 285 | if (!$currentUser->getPeople()->getLink()->isEmpty()) { |
||||||
| 286 | foreach ($currentUser->getPeople()->getLink() as $company) { |
||||||
| 287 | $companies[] = $company->getCompany(); |
||||||
| 288 | } |
||||||
| 289 | } |
||||||
| 290 | return $companies; |
||||||
| 291 | } |
||||||
| 292 | } |
||||||
| 293 |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths