| Conditions | 6 |
| Paths | 17 |
| Total Lines | 52 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 51 | public function startSubscription(CustomerInterface $customer, Plan $plan, PlanPrice $planPrice, PaymentDetails $paymentDetails, int $seatNumbers): Subscription |
||
| 52 | { |
||
| 53 | $billingDetails = $this->billingDetailsFactory->createFromCustomerAndPaymentDetails($customer, $paymentDetails); |
||
| 54 | $obolSubscription = $this->subscriptionFactory->createSubscription($billingDetails, $planPrice, $seatNumbers); |
||
| 55 | |||
| 56 | if ($this->subscriptionRepository->hasActiveMainSubscription($customer)) { |
||
| 57 | $main = false; |
||
| 58 | $subscription = $this->subscriptionRepository->getActiveMainSubscription($customer); |
||
| 59 | |||
| 60 | if ($subscription->getCurrency() != $planPrice->getCurrency()) { |
||
| 61 | throw new GeneralException("Can't add a child subscription for a different currency"); |
||
| 62 | } |
||
| 63 | |||
| 64 | $obolSubscription->setParentReference($subscription->getExternalReference()); |
||
| 65 | } else { |
||
| 66 | $main = true; |
||
| 67 | } |
||
| 68 | |||
| 69 | $subscriptionCreationResponse = $this->provider->payments()->startSubscription($obolSubscription); |
||
| 70 | if ($subscriptionCreationResponse->hasCustomerCreation()) { |
||
| 71 | $customer->setPaymentProviderDetailsUrl($subscriptionCreationResponse->getCustomerCreation()->getDetailsUrl()); |
||
| 72 | $customer->setExternalCustomerReference($subscriptionCreationResponse->getCustomerCreation()->getReference()); |
||
| 73 | } |
||
| 74 | $payment = $this->paymentFactory->fromSubscriptionCreation($subscriptionCreationResponse); |
||
| 75 | $this->paymentRepository->save($payment); |
||
| 76 | |||
| 77 | $subscription = new Subscription(); |
||
| 78 | $subscription->setPlanName($plan->getName()); |
||
| 79 | $subscription->setPaymentSchedule($planPrice->getSchedule()); |
||
| 80 | $subscription->setActive(true); |
||
| 81 | $subscription->setMoneyAmount($planPrice->getPriceAsMoney()); |
||
| 82 | $subscription->setStatus(\Parthenon\Billing\Entity\EmbeddedSubscription::STATUS_ACTIVE); |
||
| 83 | $subscription->setExternalReference($subscriptionCreationResponse->getSubscriptionId()); |
||
| 84 | $subscription->setSeats($seatNumbers); |
||
| 85 | $subscription->setCreatedAt(new \DateTime()); |
||
| 86 | $subscription->setUpdatedAt(new \DateTime()); |
||
| 87 | $subscription->setCustomer($customer); |
||
| 88 | $subscription->setMainSubscription($main); |
||
| 89 | |||
| 90 | if ($plan->hasEntityId()) { |
||
| 91 | $subscriptionPlan = $this->subscriptionPlanRepository->findById($plan->getEntityId()); |
||
| 92 | $subscription->setSubscriptionPlan($subscriptionPlan); |
||
| 93 | } |
||
| 94 | |||
| 95 | if ($planPrice->hasEntityId()) { |
||
| 96 | $price = $this->priceRepository->findById($planPrice->getEntityId()); |
||
| 97 | $subscription->setPrice($price); |
||
| 98 | } |
||
| 99 | |||
| 100 | $this->subscriptionRepository->save($subscription); |
||
| 101 | |||
| 102 | return $subscription; |
||
| 103 | } |
||
| 119 |