Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 37 | class Verify extends AbstractController |
||
| 38 | { |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var TwoFactorSecret |
||
| 42 | */ |
||
| 43 | private $secret; |
||
| 44 | /** |
||
| 45 | * @var GoogleVerify |
||
| 46 | */ |
||
| 47 | private $verify; |
||
| 48 | /** |
||
| 49 | * @var Fetcher |
||
| 50 | */ |
||
| 51 | private $fetcher; |
||
|
|
|||
| 52 | /** |
||
| 53 | * @var IsVerified |
||
| 54 | */ |
||
| 55 | private $isVerified; |
||
| 56 | /** |
||
| 57 | * @var Session |
||
| 58 | */ |
||
| 59 | private $customerSession; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Constructor |
||
| 63 | * |
||
| 64 | * @param Context $context |
||
| 65 | * @param Customer $customerGetter |
||
| 66 | * @param TwoFactorSecret $secret |
||
| 67 | * @param GoogleVerify $verify |
||
| 68 | * @param Fetcher $fetcher |
||
| 69 | * @param IsVerified $isVerified |
||
| 70 | * @param Session $customerSession |
||
| 71 | * @param CustomerAdmin $customerAdmin |
||
| 72 | * @param IsUsingTwoFactor $isUsingTwoFactor |
||
| 73 | */ |
||
| 74 | 4 | View Code Duplication | public function __construct( |
| 75 | Context $context, |
||
| 76 | Customer $customerGetter, |
||
| 77 | TwoFactorSecret $secret, |
||
| 78 | GoogleVerify $verify, |
||
| 79 | Fetcher $fetcher, |
||
| 80 | IsVerified $isVerified, |
||
| 81 | Session $customerSession, |
||
| 82 | CustomerAdmin $customerAdmin, |
||
| 83 | IsUsingTwoFactor $isUsingTwoFactor |
||
| 84 | ) { |
||
| 85 | 4 | parent::__construct($context, $customerAdmin, $customerGetter, $fetcher, $isUsingTwoFactor); |
|
| 86 | 4 | $this->secret = $secret; |
|
| 87 | 4 | $this->verify = $verify; |
|
| 88 | 4 | $this->fetcher = $fetcher; |
|
| 89 | 4 | $this->isVerified = $isVerified; |
|
| 90 | 4 | $this->customerSession = $customerSession; |
|
| 91 | 4 | } |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Dispatch request |
||
| 95 | * |
||
| 96 | * @return \Magento\Framework\Controller\ResultInterface|ResponseInterface |
||
| 97 | * @throws \Magento\Framework\Exception\NotFoundException |
||
| 98 | */ |
||
| 99 | 4 | View Code Duplication | public function execute() |
| 100 | { |
||
| 101 | 4 | if ($this->shouldActionBeRun() === false) { |
|
| 102 | 2 | return $this->getRedirectAction(); |
|
| 103 | } |
||
| 104 | |||
| 105 | 2 | $secret = $this->getRequest()->getParam('secret'); |
|
| 106 | 2 | $customer = $this->getCustomer(); |
|
| 107 | 2 | $verificationPassed = $this->verifySecret($customer, $secret); |
|
| 108 | |||
| 109 | 2 | if ($verificationPassed === false) { |
|
| 110 | 1 | return $this->handleError(); |
|
| 111 | } |
||
| 112 | |||
| 113 | 1 | return $this->handleSuccess(); |
|
| 114 | } |
||
| 115 | |||
| 116 | 2 | private function verifySecret(CustomerInterface $customer, $postedSecret) |
|
| 117 | { |
||
| 118 | 2 | $customerSecret = $this->secret->getValue($customer); |
|
| 119 | try { |
||
| 120 | 2 | $verified = $this->verify->verify($customerSecret, $postedSecret); |
|
| 121 | } catch (InvalidCharactersException $exception) { |
||
| 122 | $verified = false; |
||
| 123 | } |
||
| 124 | |||
| 125 | 2 | return $verified; |
|
| 126 | } |
||
| 127 | |||
| 128 | 1 | private function handleSuccess() |
|
| 136 | |||
| 137 | 1 | View Code Duplication | private function handleError() |
| 138 | { |
||
| 139 | 1 | $this->isVerified->removeIsVerified($this->customerSession); |
|
| 140 | 1 | $this->addErrorMessage(); |
|
| 141 | 1 | $authenticateUrl = $this->fetcher->getAuthenticationUrl(); |
|
| 142 | |||
| 143 | 1 | return $this->redirect($authenticateUrl); |
|
| 144 | } |
||
| 145 | |||
| 146 | 1 | private function addErrorMessage() |
|
| 150 | |||
| 151 | 1 | private function addSuccessMessage() |
|
| 155 | } |
||
| 156 |