| Conditions | 10 |
| Paths | 145 |
| Total Lines | 71 |
| Code Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 10 | ||
| Bugs | 0 | 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 |
||
| 64 | public function startSubscription(CustomerInterface $customer, SubscriptionPlan|Plan $plan, Price|PlanPrice $planPrice, ?PaymentCard $paymentDetails = null, int $seatNumbers = 1, ?bool $hasTrial = null, ?int $trialLengthDays = 0): Subscription |
||
| 65 | { |
||
| 66 | $billingDetails = $this->billingDetailsFactory->createFromCustomerAndPaymentDetails($customer, $paymentDetails); |
||
|
|
|||
| 67 | $obolSubscription = $this->subscriptionFactory->createSubscription($billingDetails, $planPrice, $seatNumbers, $hasTrial ?? $plan->getHasTrial(), $trialLengthDays ?? $plan->getTrialLengthDays()); |
||
| 68 | $obolSubscription->setStoredPaymentReference($paymentDetails->getStoredPaymentReference()); |
||
| 69 | |||
| 70 | if ($this->subscriptionRepository->hasActiveSubscription($customer)) { |
||
| 71 | $subscription = $this->subscriptionRepository->getOneActiveSubscriptionForCustomer($customer); |
||
| 72 | |||
| 73 | if ($subscription->getCurrency() != $planPrice->getCurrency()) { |
||
| 74 | throw new SubscriptionCreationException("Can't add a child subscription for a different currency"); |
||
| 75 | } |
||
| 76 | |||
| 77 | $obolSubscription->setParentReference($subscription->getMainExternalReference()); |
||
| 78 | } |
||
| 79 | |||
| 80 | $subscriptionCreationResponse = $this->provider->payments()->startSubscription($obolSubscription); |
||
| 81 | if ($subscriptionCreationResponse->hasCustomerCreation()) { |
||
| 82 | $customer->setPaymentProviderDetailsUrl($subscriptionCreationResponse->getCustomerCreation()->getDetailsUrl()); |
||
| 83 | $customer->setExternalCustomerReference($subscriptionCreationResponse->getCustomerCreation()->getReference()); |
||
| 84 | } |
||
| 85 | |||
| 86 | $subscription = $this->entityFactory->getSubscriptionEntity(); |
||
| 87 | $subscription->setPlanName($plan->getName()); |
||
| 88 | if ($planPrice->isRecurring()) { |
||
| 89 | $subscription->setPaymentSchedule($planPrice->getSchedule()); |
||
| 90 | } |
||
| 91 | $subscription->setActive(true); |
||
| 92 | $subscription->setMoneyAmount($subscriptionCreationResponse->getPaymentDetails()?->getAmount()); |
||
| 93 | $subscription->setStatus(SubscriptionStatus::ACTIVE); |
||
| 94 | $subscription->setMainExternalReference($subscriptionCreationResponse->getSubscriptionId()); |
||
| 95 | $subscription->setChildExternalReference($subscriptionCreationResponse->getLineId()); |
||
| 96 | $subscription->setSeats($seatNumbers); |
||
| 97 | $subscription->setCreatedAt(new \DateTime()); |
||
| 98 | $subscription->setUpdatedAt(new \DateTime()); |
||
| 99 | $subscription->setStartOfCurrentPeriod(new \DateTime()); |
||
| 100 | $subscription->setValidUntil($subscriptionCreationResponse->getBilledUntil()); |
||
| 101 | $subscription->setCustomer($customer); |
||
| 102 | $subscription->setMainExternalReferenceDetailsUrl($subscriptionCreationResponse->getDetailsUrl()); |
||
| 103 | $subscription->setPaymentDetails($paymentDetails); |
||
| 104 | $subscription->setTrialLengthDays($obolSubscription->getTrialLengthDays()); |
||
| 105 | $subscription->setHasTrial($obolSubscription->hasTrial()); |
||
| 106 | |||
| 107 | if ($plan instanceof SubscriptionPlan) { |
||
| 108 | $subscription->setSubscriptionPlan($plan); |
||
| 109 | } elseif ($plan->hasEntityId()) { |
||
| 110 | $subscriptionPlan = $this->subscriptionPlanRepository->findById($plan->getEntityId()); |
||
| 111 | $subscription->setSubscriptionPlan($subscriptionPlan); |
||
| 112 | } |
||
| 113 | |||
| 114 | if ($planPrice instanceof Price) { |
||
| 115 | $subscription->setPrice($planPrice); |
||
| 116 | } elseif ($planPrice->hasEntityId()) { |
||
| 117 | $price = $this->priceRepository->findById($planPrice->getEntityId()); |
||
| 118 | $subscription->setPrice($price); |
||
| 119 | } |
||
| 120 | $this->subscriptionRepository->save($subscription); |
||
| 121 | $this->subscriptionRepository->updateValidUntilForAllActiveSubscriptions($customer, $subscription->getMainExternalReference(), $subscriptionCreationResponse->getBilledUntil()); |
||
| 122 | |||
| 123 | $this->dispatcher->dispatch(new SubscriptionCreated($subscription), SubscriptionCreated::NAME); |
||
| 124 | |||
| 125 | $obolPaymentDetails = $subscriptionCreationResponse->getPaymentDetails(); |
||
| 126 | if ($obolPaymentDetails) { |
||
| 127 | $payment = $this->paymentFactory->fromSubscriptionCreation($obolPaymentDetails, $customer); |
||
| 128 | $payment->addSubscription($subscription); |
||
| 129 | $this->paymentRepository->save($payment); |
||
| 130 | |||
| 131 | $this->dispatcher->dispatch(new PaymentCreated($payment, true), PaymentCreated::NAME); |
||
| 132 | } |
||
| 133 | |||
| 134 | return $subscription; |
||
| 135 | } |
||
| 241 |