| Conditions | 6 |
| Paths | 32 |
| Total Lines | 60 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 declare(strict_types=1); |
||
| 92 | public function subscribeCustomer(Request $request, RequestDataBag $dataBag, SalesChannelContext $context, ?CustomerEntity $customer = null): Response |
||
| 93 | { |
||
| 94 | /* @deprecated tag:v6.4.0 - Parameter $customer will be mandatory when using with @LoginRequired() */ |
||
| 95 | if (!$customer) { |
||
| 96 | $customer = $context->getCustomer(); |
||
| 97 | } |
||
| 98 | |||
| 99 | $subscribed = $request->get('option', false) === 'direct'; |
||
| 100 | |||
| 101 | if (!$subscribed) { |
||
| 102 | $dataBag->set('option', 'unsubscribe'); |
||
| 103 | } |
||
| 104 | |||
| 105 | $dataBag->set('storefrontUrl', $request->attributes->get(RequestTransformer::STOREFRONT_URL)); |
||
| 106 | |||
| 107 | $messages = []; |
||
| 108 | $success = null; |
||
| 109 | |||
| 110 | if ($subscribed) { |
||
| 111 | try { |
||
| 112 | $this->newsletterSubscribeRoute->subscribe( |
||
| 113 | $this->hydrateFromCustomer($dataBag, $customer), |
||
|
|
|||
| 114 | $context, |
||
| 115 | false |
||
| 116 | ); |
||
| 117 | |||
| 118 | $this->setNewsletterFlag($customer, true, $context); |
||
| 119 | |||
| 120 | $success = true; |
||
| 121 | $messages[] = ['type' => 'success', 'text' => $this->trans('newsletter.subscriptionConfirmationSuccess')]; |
||
| 122 | } catch (\Exception $exception) { |
||
| 123 | $success = false; |
||
| 124 | $messages[] = ['type' => 'danger', 'text' => $this->trans('newsletter.subscriptionConfirmationFailed')]; |
||
| 125 | } |
||
| 126 | |||
| 127 | return $this->renderStorefront('@Storefront/storefront/page/account/newsletter.html.twig', [ |
||
| 128 | 'customer' => $customer, |
||
| 129 | 'messages' => $messages, |
||
| 130 | 'success' => $success, |
||
| 131 | ]); |
||
| 132 | } |
||
| 133 | |||
| 134 | try { |
||
| 135 | $this->newsletterUnsubscribeRoute->unsubscribe( |
||
| 136 | $this->hydrateFromCustomer($dataBag, $customer), |
||
| 137 | $context |
||
| 138 | ); |
||
| 139 | $this->setNewsletterFlag($customer, false, $context); |
||
| 140 | |||
| 141 | $success = true; |
||
| 142 | $messages[] = ['type' => 'success', 'text' => $this->trans('newsletter.subscriptionRevokeSuccess')]; |
||
| 143 | } catch (\Exception $exception) { |
||
| 144 | $success = false; |
||
| 145 | $messages[] = ['type' => 'danger', 'text' => $this->trans('error.message-default')]; |
||
| 146 | } |
||
| 147 | |||
| 148 | return $this->renderStorefront('@Storefront/storefront/page/account/newsletter.html.twig', [ |
||
| 149 | 'customer' => $customer, |
||
| 150 | 'messages' => $messages, |
||
| 151 | 'success' => $success, |
||
| 152 | ]); |
||
| 180 |