1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ControleOnline\Service; |
4
|
|
|
|
5
|
|
|
use ControleOnline\Entity\Document; |
6
|
|
|
use ControleOnline\Entity\DocumentType; |
7
|
|
|
use ControleOnline\Entity\Language; |
|
|
|
|
8
|
|
|
use ControleOnline\Entity\People; |
9
|
|
|
use ControleOnline\Entity\PeopleLink; |
10
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
|
|
|
11
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
|
|
|
12
|
|
|
use Symfony\Component\Security\Core\Security; |
|
|
|
|
13
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
|
|
|
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 |
|
|
|
|
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 |
|
|
|
|
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) |
|
|
|
|
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
|
|
|
|
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