| Total Complexity | 60 |
| Total Lines | 247 |
| Duplicated Lines | 0 % |
| Changes | 24 | ||
| 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) {} |
||
| 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)) |
||
| 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); |
||
| 103 | $document->setDocumentType($this->discoveryDocumentType($document_type)); |
||
| 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 |
||
| 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); |
||
| 162 | ]); |
||
| 163 | } |
||
| 164 | |||
| 165 | public function getPeopleTypeByDocumentLen(?string $document_number = null) |
||
| 168 | } |
||
| 169 | |||
| 170 | public function getDocumentTypeByDocumentLen(?string $document_number = null) |
||
| 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 |
||
| 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 |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | public function getMyCompanies(): array |
||
| 252 | { |
||
| 270 |
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