| Conditions | 11 |
| Paths | 192 |
| Total Lines | 62 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php declare(strict_types=1); |
||
| 55 | #[Route(path: '/store-api/account/change-profile', name: 'store-api.account.change-profile', methods: ['POST'], defaults: ['_loginRequired' => true, '_loginRequiredAllowGuest' => true])] |
||
| 56 | public function change(RequestDataBag $data, SalesChannelContext $context, CustomerEntity $customer): SuccessResponse |
||
| 57 | { |
||
| 58 | $validation = $this->customerProfileValidationFactory->update($context); |
||
| 59 | |||
| 60 | if ($data->has('accountType') && empty($data->get('accountType'))) { |
||
| 61 | $data->remove('accountType'); |
||
| 62 | } |
||
| 63 | |||
| 64 | if ($data->get('accountType') === CustomerEntity::ACCOUNT_TYPE_BUSINESS) { |
||
| 65 | $validation->add('company', new NotBlank()); |
||
| 66 | $billingAddress = $customer->getDefaultBillingAddress(); |
||
| 67 | if ($billingAddress) { |
||
| 68 | $this->addVatIdsValidation($validation, $billingAddress); |
||
| 69 | } |
||
| 70 | } else { |
||
| 71 | $data->set('company', ''); |
||
| 72 | $data->set('vatIds', null); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** @var ?RequestDataBag $vatIds */ |
||
| 76 | $vatIds = $data->get('vatIds'); |
||
| 77 | if ($vatIds) { |
||
| 78 | $vatIds = \array_filter($vatIds->all()); |
||
| 79 | $data->set('vatIds', empty($vatIds) ? null : $vatIds); |
||
| 80 | } |
||
| 81 | |||
| 82 | if (!$data->get('salutationId')) { |
||
| 83 | $data->set('salutationId', $this->getDefaultSalutationId($context)); |
||
| 84 | } |
||
| 85 | |||
| 86 | $this->dispatchValidationEvent($validation, $data, $context->getContext()); |
||
| 87 | |||
| 88 | $this->validator->validate($data->all(), $validation); |
||
| 89 | |||
| 90 | $customerData = $data->only('firstName', 'lastName', 'salutationId', 'title', 'company', 'accountType'); |
||
| 91 | |||
| 92 | if ($vatIds) { |
||
| 93 | $customerData['vatIds'] = $data->get('vatIds'); |
||
| 94 | } |
||
| 95 | |||
| 96 | if ($birthday = $this->getBirthday($data)) { |
||
| 97 | $customerData['birthday'] = $birthday; |
||
| 98 | } |
||
| 99 | |||
| 100 | if ($data->get('customFields') instanceof RequestDataBag) { |
||
| 101 | $customerData['customFields'] = $this->storeApiCustomFieldMapper->map( |
||
| 102 | CustomerDefinition::ENTITY_NAME, |
||
| 103 | $data->get('customFields') |
||
| 104 | ); |
||
| 105 | } |
||
| 106 | |||
| 107 | $mappingEvent = new DataMappingEvent($data, $customerData, $context->getContext()); |
||
| 108 | $this->eventDispatcher->dispatch($mappingEvent, CustomerEvents::MAPPING_CUSTOMER_PROFILE_SAVE); |
||
| 109 | |||
| 110 | $customerData = $mappingEvent->getOutput(); |
||
| 111 | |||
| 112 | $customerData['id'] = $customer->getId(); |
||
| 113 | |||
| 114 | $this->customerRepository->update([$customerData], $context->getContext()); |
||
| 115 | |||
| 116 | return new SuccessResponse(); |
||
| 117 | } |
||
| 176 |