Total Complexity | 63 |
Total Lines | 270 |
Duplicated Lines | 0 % |
Changes | 29 | ||
Bugs | 0 | Features | 0 |
Complex classes like PeopleService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PeopleService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
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) |
||
38 | } |
||
39 | |||
40 | public function addClient() {} |
||
41 | |||
42 | |||
43 | public function discoveryClient(People $provider, People $client) |
||
46 | } |
||
47 | |||
48 | public function discoveryLink(People $company, People $people, $linkType): PeopleLink |
||
60 | } |
||
61 | |||
62 | public function discoveryPeople(?string $document = null, ?string $email = null, ?array $phone = [], ?string $name = null, ?string $peopleType = null): 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'; |
||
191 | } |
||
192 | |||
193 | public function getDocumentTypeByDocumentLen(?string $document_number = null) |
||
194 | { |
||
195 | return strlen($document_number) > 11 ? 'CNPJ' : 'CPF'; |
||
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 |
||
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 |
||
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 = []; |
||
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