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