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