| Conditions | 8 |
| Paths | 37 |
| Total Lines | 60 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| 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 |
||
| 56 | public function startSubscription(CustomerInterface $customer, SubscriptionPlan|Plan $plan, Price|PlanPrice $planPrice, PaymentDetails $paymentDetails, int $seatNumbers): Subscription |
||
| 57 | { |
||
| 58 | $billingDetails = $this->billingDetailsFactory->createFromCustomerAndPaymentDetails($customer, $paymentDetails); |
||
| 59 | $obolSubscription = $this->subscriptionFactory->createSubscription($billingDetails, $planPrice, $seatNumbers); |
||
|
|
|||
| 60 | $obolSubscription->setStoredPaymentReference($paymentDetails->getStoredPaymentReference()); |
||
| 61 | |||
| 62 | if ($this->subscriptionRepository->hasActiveSubscription($customer)) { |
||
| 63 | $subscription = $this->subscriptionRepository->getOneActiveSubscriptionForCustomer($customer); |
||
| 64 | |||
| 65 | if ($subscription->getCurrency() != $planPrice->getCurrency()) { |
||
| 66 | throw new SubscriptionCreationException("Can't add a child subscription for a different currency"); |
||
| 67 | } |
||
| 68 | |||
| 69 | $obolSubscription->setParentReference($subscription->getMainExternalReference()); |
||
| 70 | } |
||
| 71 | |||
| 72 | $subscriptionCreationResponse = $this->provider->payments()->startSubscription($obolSubscription); |
||
| 73 | if ($subscriptionCreationResponse->hasCustomerCreation()) { |
||
| 74 | $customer->setPaymentProviderDetailsUrl($subscriptionCreationResponse->getCustomerCreation()->getDetailsUrl()); |
||
| 75 | $customer->setExternalCustomerReference($subscriptionCreationResponse->getCustomerCreation()->getReference()); |
||
| 76 | } |
||
| 77 | |||
| 78 | $subscription = new Subscription(); |
||
| 79 | $subscription->setPlanName($plan->getName()); |
||
| 80 | $subscription->setPaymentSchedule($planPrice->getSchedule()); |
||
| 81 | $subscription->setActive(true); |
||
| 82 | $subscription->setMoneyAmount($subscriptionCreationResponse->getPaymentDetails()->getAmount()); |
||
| 83 | $subscription->setStatus(\Parthenon\Billing\Entity\EmbeddedSubscription::STATUS_ACTIVE); |
||
| 84 | $subscription->setMainExternalReference($subscriptionCreationResponse->getSubscriptionId()); |
||
| 85 | $subscription->setChildExternalReference($subscriptionCreationResponse->getLineId()); |
||
| 86 | $subscription->setSeats($seatNumbers); |
||
| 87 | $subscription->setCreatedAt(new \DateTime()); |
||
| 88 | $subscription->setUpdatedAt(new \DateTime()); |
||
| 89 | $subscription->setStartOfCurrentPeriod(new \DateTime()); |
||
| 90 | $subscription->setValidUntil($subscriptionCreationResponse->getBilledUntil()); |
||
| 91 | $subscription->setCustomer($customer); |
||
| 92 | $subscription->setMainExternalReferenceDetailsUrl($subscriptionCreationResponse->getDetailsUrl()); |
||
| 93 | $subscription->setPaymentExternalReference($subscriptionCreationResponse->getPaymentDetails()->getStoredPaymentReference()); |
||
| 94 | |||
| 95 | if ($plan instanceof SubscriptionPlan) { |
||
| 96 | $subscription->setSubscriptionPlan($plan); |
||
| 97 | } elseif ($plan->hasEntityId()) { |
||
| 98 | $subscriptionPlan = $this->subscriptionPlanRepository->findById($plan->getEntityId()); |
||
| 99 | $subscription->setSubscriptionPlan($subscriptionPlan); |
||
| 100 | } |
||
| 101 | |||
| 102 | if ($planPrice instanceof Price) { |
||
| 103 | $subscription->setPrice($planPrice); |
||
| 104 | } elseif ($planPrice->hasEntityId()) { |
||
| 105 | $price = $this->priceRepository->findById($planPrice->getEntityId()); |
||
| 106 | $subscription->setPrice($price); |
||
| 107 | } |
||
| 108 | $this->subscriptionRepository->save($subscription); |
||
| 109 | $this->subscriptionRepository->updateValidUntilForAllActiveSubscriptions($customer, $subscription->getMainExternalReference(), $subscriptionCreationResponse->getBilledUntil()); |
||
| 110 | |||
| 111 | $payment = $this->paymentFactory->fromSubscriptionCreation($subscriptionCreationResponse, $customer); |
||
| 112 | $payment->addSubscription($subscription); |
||
| 113 | $this->paymentRepository->save($payment); |
||
| 114 | |||
| 115 | return $subscription; |
||
| 116 | } |
||
| 190 |