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
|
|
|
private PeopleService $PeopleService |
24
|
|
|
) { |
25
|
|
|
$this->request = $requestStack->getCurrentRequest(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function beforePersist(People $people) |
29
|
|
|
{ |
30
|
|
|
$language = $this->manager->getRepository(Language::class)->findOneBy(['language' => 'pt-br']); |
31
|
|
|
$people->setLanguage($language); |
32
|
|
|
return $people; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function afterPersist(People $people) |
36
|
|
|
{ |
37
|
|
|
$request = $this->requestStack->getCurrentRequest(); |
38
|
|
|
$payload = json_decode($request->getContent()); |
39
|
|
|
if (isset($payload->link_type)) { |
40
|
|
|
$company = $this->manager->getRepository(People::class)->find(preg_replace('/\D/', '', $payload->company)); |
41
|
|
|
if ($company) |
42
|
|
|
$this->addLink($company, $people, $payload->link_type); |
43
|
|
|
else { |
44
|
|
|
$link = $this->manager->getRepository(People::class)->find(preg_replace('/\D/', '', $payload->link)); |
45
|
|
|
if ($payload->link_type == 'employee' && $link) { |
46
|
|
|
$this->addLink($people, $link, $payload->link_type); |
47
|
|
|
if ($payload->people_document) { |
48
|
|
|
$document_type = $this->manager->getRepository(DocumentType::class)->findOneBy(['document_type' => 'cnpj']); |
49
|
|
|
|
50
|
|
|
$document = new Document(); |
51
|
|
|
$document->setPeople($people); |
52
|
|
|
$document->setDocumentType($document_type); |
53
|
|
|
$document->setDocument($payload->people_document); |
54
|
|
|
$this->manager->persist($document); |
55
|
|
|
$this->manager->flush(); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function addLink(People $company, People $people, $link_type) |
63
|
|
|
{ |
64
|
|
|
|
65
|
|
|
$peopleLink = $this->manager->getRepository(PeopleLink::class)->findOneBy([ |
66
|
|
|
'company' => $company, |
67
|
|
|
'people' => $people, |
68
|
|
|
'link_type' => $link_type |
69
|
|
|
]); |
70
|
|
|
|
71
|
|
|
if (!$peopleLink) |
72
|
|
|
$peopleLink = new PeopleLink(); |
73
|
|
|
|
74
|
|
|
$peopleLink->setCompany($company); |
75
|
|
|
$peopleLink->setPeople($people); |
76
|
|
|
$peopleLink->setLinkType($link_type); |
77
|
|
|
|
78
|
|
|
$this->manager->persist($peopleLink); |
79
|
|
|
$this->manager->flush(); |
80
|
|
|
return $peopleLink; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function secutiryFilter(QueryBuilder $queryBuilder, $resourceClass = null, $applyTo = null, $rootAlias = null): void |
84
|
|
|
{ |
85
|
|
|
$this->checkLink($queryBuilder, $resourceClass, $applyTo, $rootAlias); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function checkLink(QueryBuilder $queryBuilder, $resourceClass = null, $applyTo = null, $rootAlias = null): void |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
|
91
|
|
|
$link = $this->request->query->get('link', null); |
92
|
|
|
$company = $this->request->query->get('company', null); |
93
|
|
|
$link_type = $this->request->query->get('link_type', null); |
94
|
|
|
|
95
|
|
|
if ($link_type) { |
96
|
|
|
$queryBuilder->join(sprintf('%s.' . ($link ? 'company' : 'link'), $rootAlias), 'PeopleLink'); |
97
|
|
|
$queryBuilder->andWhere('PeopleLink.link_type IN(:link_type)'); |
98
|
|
|
$queryBuilder->setParameter('link_type', $link_type); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if ($company || $link) { |
102
|
|
|
$queryBuilder->andWhere('PeopleLink.' . ($link ? 'people' : 'company') . ' IN(:people)'); |
103
|
|
|
$queryBuilder->setParameter('people', preg_replace("/[^0-9]/", "", ($link ?: $company))); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
public function checkCompany($type, QueryBuilder $queryBuilder, $resourceClass = null, $applyTo = null, $rootAlias = null): void |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
$companies = $this->getMyCompanies(); |
109
|
|
|
$queryBuilder->andWhere(sprintf('%s.' . $type . ' IN(:companies)', $rootAlias, $rootAlias)); |
110
|
|
|
$queryBuilder->setParameter('companies', $companies); |
111
|
|
|
|
112
|
|
|
if ($payer = $this->request->query->get('company', null)) { |
113
|
|
|
$queryBuilder->andWhere(sprintf('%s.' . $type . ' IN(:people)', $rootAlias)); |
114
|
|
|
$queryBuilder->setParameter('people', preg_replace("/[^0-9]/", "", $payer)); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
|
120
|
|
|
public function getMyCompanies(): array |
121
|
|
|
{ |
122
|
|
|
/** |
123
|
|
|
* @var \ControleOnline\Entity\User $currentUser |
124
|
|
|
*/ |
125
|
|
|
$currentUser = $this->security->getUser(); |
126
|
|
|
$companies = []; |
127
|
|
|
|
128
|
|
|
if (!$currentUser->getPeople()->getLink()->isEmpty()) { |
129
|
|
|
foreach ($currentUser->getPeople()->getLink() as $company) { |
130
|
|
|
$companies[] = $company->getCompany(); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
return $companies; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
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