| Conditions | 8 |
| Paths | 33 |
| Total Lines | 53 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 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 |
||
| 54 | #[Route('/billing/subscription/start', name: 'parthenon_billing_subscription_start_with_payment_details', methods: ['POST'])] |
||
| 55 | public function startSubscriptionWithPaymentDetails( |
||
| 56 | Request $request, |
||
| 57 | CustomerProviderInterface $customerProvider, |
||
| 58 | SerializerInterface $serializer, |
||
| 59 | CustomerRepositoryInterface $customerRepository, |
||
| 60 | ValidatorInterface $validator, |
||
| 61 | SubscriptionManagerInterface $subscriptionManager, |
||
| 62 | ): Response { |
||
| 63 | $this->getLogger()->info('Starting the subscription'); |
||
| 64 | |||
| 65 | try { |
||
| 66 | $customer = $customerProvider->getCurrentCustomer(); |
||
| 67 | } catch (NoCustomerException $exception) { |
||
| 68 | $this->getLogger()->error('No customer found when starting subscription with payment details - probable misconfigured firewall.'); |
||
| 69 | |||
| 70 | return new JsonResponse(StartSubscriptionResponse::createGeneralError(), JsonResponse::HTTP_BAD_REQUEST); |
||
| 71 | } |
||
| 72 | |||
| 73 | try { |
||
| 74 | /** @var StartSubscriptionDto $subscriptionDto */ |
||
| 75 | $subscriptionDto = $serializer->deserialize($request->getContent(), StartSubscriptionDto::class, 'json'); |
||
| 76 | |||
| 77 | $errors = $validator->validate($subscriptionDto); |
||
| 78 | |||
| 79 | if (count($errors) > 0) { |
||
| 80 | return new JsonResponse(StartSubscriptionResponse::createInvalidRequestResponse($errors), JsonResponse::HTTP_BAD_REQUEST); |
||
| 81 | } |
||
| 82 | |||
| 83 | $subscription = $subscriptionManager->startSubscriptionWithDto($customer, $subscriptionDto); |
||
| 84 | |||
| 85 | $customerRepository->save($customer); |
||
| 86 | } catch (NoEntityFoundException $exception) { |
||
| 87 | return new JsonResponse(StartSubscriptionResponse::createGeneralError(), JsonResponse::HTTP_BAD_REQUEST); |
||
| 88 | } catch (NoPlanPriceFoundException $exception) { |
||
| 89 | $this->getLogger()->warning('No price plan found'); |
||
| 90 | |||
| 91 | return new JsonResponse(StartSubscriptionResponse::createPlanPriceNotFound(), JsonResponse::HTTP_BAD_REQUEST); |
||
| 92 | } catch (NoPlanFoundException $exception) { |
||
| 93 | $this->getLogger()->warning('No plan found'); |
||
| 94 | |||
| 95 | return new JsonResponse(StartSubscriptionResponse::createPlanNotFound(), JsonResponse::HTTP_BAD_REQUEST); |
||
| 96 | } catch (UnsupportedFunctionalityException $exception) { |
||
| 97 | $this->getLogger()->error('Payment provider does not support payment details'); |
||
| 98 | |||
| 99 | return new JsonResponse(StartSubscriptionResponse::createUnsupportedPaymentProvider(), JsonResponse::HTTP_BAD_REQUEST); |
||
| 100 | } catch (\Throwable $t) { |
||
| 101 | $this->getLogger()->error('Unknown error while starting a subscription'); |
||
| 102 | |||
| 103 | throw $t; |
||
| 104 | } |
||
| 105 | |||
| 106 | return new JsonResponse(StartSubscriptionResponse::createSuccessResponse($subscription), JsonResponse::HTTP_CREATED); |
||
| 107 | } |
||
| 109 |