| Conditions | 11 |
| Paths | 11 |
| Total Lines | 64 |
| Code Lines | 33 |
| 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 |
||
| 35 | public function setSession(RequestEvent $event): void |
||
| 36 | { |
||
| 37 | if ($event->getRequestType() !== HttpKernelInterface::MAIN_REQUEST) { |
||
| 38 | return; |
||
| 39 | } |
||
| 40 | |||
| 41 | $request = $event->getRequest(); |
||
| 42 | |||
| 43 | /** @var Session $session */ |
||
| 44 | $session = $request->getSession(); |
||
| 45 | |||
| 46 | if (!$request->query->has(AffiliateReferralInterface::TOKEN_PARAM_NAME)) { |
||
| 47 | return; |
||
| 48 | } |
||
| 49 | |||
| 50 | $tokenValue = $request->query->get(AffiliateReferralInterface::TOKEN_PARAM_NAME); |
||
| 51 | if (null === $tokenValue) { |
||
| 52 | return; |
||
| 53 | } |
||
| 54 | |||
| 55 | if ($session->get(AffiliateReferralInterface::TOKEN_PARAM_NAME) === $tokenValue) { |
||
| 56 | return; |
||
| 57 | } |
||
| 58 | |||
| 59 | /** @var AffiliateReferralInterface|null $affiliateReferral */ |
||
| 60 | $affiliateReferral = $this->affiliateReferralRepository->findOneBy([ |
||
| 61 | 'tokenValue' => $tokenValue |
||
| 62 | ]); |
||
| 63 | |||
| 64 | if (null === $affiliateReferral) { |
||
| 65 | $session->getFlashBag()->add('error', 'The referral link is invalid!'); |
||
| 66 | |||
| 67 | return; |
||
| 68 | } |
||
| 69 | |||
| 70 | if ($affiliateReferral->isExpired()) { |
||
| 71 | $session->getFlashBag()->add('info', 'The link you followed has expired!'); |
||
| 72 | |||
| 73 | return; |
||
| 74 | } |
||
| 75 | |||
| 76 | $token = $this->tokenStorage->getToken(); |
||
| 77 | if (!$token instanceof TokenInterface) { |
||
| 78 | return; |
||
| 79 | } |
||
| 80 | |||
| 81 | $affiliateReferralView = new AffiliateReferralView(); |
||
| 82 | $affiliateReferralView->setAffiliateReferral($affiliateReferral); |
||
| 83 | $affiliateReferralView->setIp($request->getClientIp()); |
||
| 84 | |||
| 85 | $shopUser = $token->getUser(); |
||
| 86 | |||
| 87 | if ($shopUser instanceof ShopUserInterface) { |
||
| 88 | $customer = $shopUser->getCustomer(); |
||
| 89 | if ($customer instanceof AffiliateInterface) { |
||
| 90 | if ($customer === $affiliateReferral->getAffiliate()) { |
||
| 91 | return; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | $this->affiliateReferralViewRepository->add($affiliateReferralView); |
||
| 97 | |||
| 98 | $session->set(AffiliateReferralInterface::TOKEN_PARAM_NAME, $tokenValue); |
||
| 99 | } |
||
| 101 |