| Conditions | 10 |
| Paths | 45 |
| Total Lines | 61 |
| Code Lines | 35 |
| 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 |
||
| 68 | #[Route('/billing/subscription/start', name: 'parthenon_billing_subscription_start_with_payment_details', methods: ['POST'])] |
||
| 69 | public function startSubscriptionWithPaymentDetails( |
||
| 70 | Request $request, |
||
| 71 | CustomerProviderInterface $customerProvider, |
||
| 72 | SerializerInterface $serializer, |
||
| 73 | CustomerRepositoryInterface $customerRepository, |
||
| 74 | ValidatorInterface $validator, |
||
| 75 | SubscriptionManagerInterface $subscriptionManager, |
||
| 76 | ): Response { |
||
| 77 | $this->getLogger()->info('Starting the subscription'); |
||
| 78 | |||
| 79 | try { |
||
| 80 | $customer = $customerProvider->getCurrentCustomer(); |
||
| 81 | } catch (NoCustomerException $exception) { |
||
| 82 | $this->getLogger()->error('No customer found when starting subscription with payment details - probable misconfigured firewall.'); |
||
| 83 | |||
| 84 | return new JsonResponse(StartSubscriptionResponse::createGeneralError(), JsonResponse::HTTP_BAD_REQUEST); |
||
| 85 | } |
||
| 86 | |||
| 87 | try { |
||
| 88 | /** @var StartSubscriptionDto $subscriptionDto */ |
||
| 89 | $subscriptionDto = $serializer->deserialize($request->getContent(), StartSubscriptionDto::class, 'json'); |
||
| 90 | |||
| 91 | $errors = $validator->validate($subscriptionDto); |
||
| 92 | |||
| 93 | if (count($errors) > 0) { |
||
| 94 | return new JsonResponse(StartSubscriptionResponse::createInvalidRequestResponse($errors), JsonResponse::HTTP_BAD_REQUEST); |
||
| 95 | } |
||
| 96 | |||
| 97 | $subscription = $subscriptionManager->startSubscriptionWithDto($customer, $subscriptionDto); |
||
| 98 | |||
| 99 | $customerRepository->save($customer); |
||
| 100 | } catch (NoEntityFoundException $exception) { |
||
| 101 | return new JsonResponse(StartSubscriptionResponse::createGeneralError(), JsonResponse::HTTP_BAD_REQUEST); |
||
| 102 | } catch (NoPlanPriceFoundException $exception) { |
||
| 103 | $this->getLogger()->warning('No price plan found'); |
||
| 104 | |||
| 105 | return new JsonResponse(StartSubscriptionResponse::createPlanPriceNotFound(), JsonResponse::HTTP_BAD_REQUEST); |
||
| 106 | } catch (NoPlanFoundException $exception) { |
||
| 107 | $this->getLogger()->warning('No plan found'); |
||
| 108 | |||
| 109 | return new JsonResponse(StartSubscriptionResponse::createPlanNotFound(), JsonResponse::HTTP_BAD_REQUEST); |
||
| 110 | } catch (PaymentFailureException $exception) { |
||
| 111 | $this->getLogger()->warning('Payment failed so subscription was not created'); |
||
| 112 | |||
| 113 | return new JsonResponse(StartSubscriptionResponse::createPaymentFailed($exception->getChargeFailureReason()), JsonResponse::HTTP_BAD_REQUEST); |
||
| 114 | } catch (NoPaymentDetailsException $exception) { |
||
| 115 | $this->getLogger()->warning('Customer does not have payment details so subscription was not created'); |
||
| 116 | |||
| 117 | return new JsonResponse(StartSubscriptionResponse::createNoPaymentDetails(), JsonResponse::HTTP_BAD_REQUEST); |
||
| 118 | } catch (UnsupportedFunctionalityException $exception) { |
||
| 119 | $this->getLogger()->error('Payment provider does not support payment details'); |
||
| 120 | |||
| 121 | return new JsonResponse(StartSubscriptionResponse::createUnsupportedPaymentProvider(), JsonResponse::HTTP_BAD_REQUEST); |
||
| 122 | } catch (\Throwable $t) { |
||
| 123 | $this->getLogger()->error('Unknown error while starting a subscription'); |
||
| 124 | |||
| 125 | throw $t; |
||
| 126 | } |
||
| 127 | |||
| 128 | return new JsonResponse(StartSubscriptionResponse::createSuccessResponse($subscription), JsonResponse::HTTP_CREATED); |
||
| 129 | } |
||
| 150 |