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 |
||
38 | class ProcessSamlAuthenticationHandler implements AuthenticationHandler |
||
39 | { |
||
40 | /** |
||
41 | * @var AuthenticationHandler |
||
42 | */ |
||
43 | private $nextHandler; |
||
44 | |||
45 | /** |
||
46 | * @var TokenStorageInterface |
||
47 | */ |
||
48 | private $tokenStorage; |
||
49 | |||
50 | /** |
||
51 | * @var SamlInteractionProvider |
||
52 | */ |
||
53 | private $samlInteractionProvider; |
||
54 | |||
55 | /** |
||
56 | * @var SamlAuthenticationStateHandler |
||
57 | */ |
||
58 | private $authenticationStateHandler; |
||
59 | |||
60 | /** |
||
61 | * @var AuthenticatedSessionStateHandler |
||
62 | */ |
||
63 | private $authenticatedSession; |
||
64 | |||
65 | /** |
||
66 | * @var AuthenticationManagerInterface |
||
67 | */ |
||
68 | private $authenticationManager; |
||
69 | |||
70 | /** |
||
71 | * @var SamlAuthenticationLogger |
||
72 | */ |
||
73 | private $authenticationLogger; |
||
74 | |||
75 | /** |
||
76 | * @var EngineInterface |
||
77 | */ |
||
78 | private $templating; |
||
79 | |||
80 | View Code Duplication | public function __construct( |
|
|
|||
81 | TokenStorageInterface $tokenStorage, |
||
82 | SamlInteractionProvider $samlInteractionProvider, |
||
83 | SamlAuthenticationStateHandler $authenticationStateHandler, |
||
84 | AuthenticatedSessionStateHandler $authenticatedSession, |
||
85 | AuthenticationManagerInterface $authenticationManager, |
||
86 | SamlAuthenticationLogger $authenticationLogger, |
||
87 | EngineInterface $templating |
||
88 | ) { |
||
89 | $this->tokenStorage = $tokenStorage; |
||
90 | $this->samlInteractionProvider = $samlInteractionProvider; |
||
91 | $this->authenticationStateHandler = $authenticationStateHandler; |
||
92 | $this->authenticatedSession = $authenticatedSession; |
||
93 | $this->authenticationManager = $authenticationManager; |
||
94 | $this->authenticationLogger = $authenticationLogger; |
||
95 | $this->templating = $templating; |
||
96 | } |
||
97 | |||
98 | public function process(GetResponseEvent $event) |
||
99 | { |
||
100 | if ($this->tokenStorage->getToken() === null |
||
101 | && $this->samlInteractionProvider->isSamlAuthenticationInitiated() |
||
102 | ) { |
||
103 | $expectedInResponseTo = $this->authenticationStateHandler->getRequestId(); |
||
104 | $logger = $this->authenticationLogger->forAuthentication($expectedInResponseTo); |
||
105 | |||
106 | $logger->notice('No authenticated user and AuthnRequest pending, attempting to process SamlResponse'); |
||
107 | |||
108 | $assertion = $this->samlInteractionProvider->processSamlResponse($event->getRequest()); |
||
109 | |||
110 | if (!InResponseTo::assertEquals($assertion, $expectedInResponseTo)) { |
||
111 | throw new AuthenticationException('Unknown or unexpected InResponseTo in SAMLResponse'); |
||
112 | } |
||
113 | |||
114 | $logger->notice('Successfully processed SAMLResponse, attempting to authenticate'); |
||
115 | |||
116 | $token = new SamlToken(); |
||
117 | $token->assertion = $assertion; |
||
118 | |||
119 | $authToken = $this->authenticationManager->authenticate($token); |
||
120 | |||
121 | $this->authenticatedSession->logAuthenticationMoment(); |
||
122 | $this->tokenStorage->setToken($authToken); |
||
123 | |||
124 | // migrate the session to prevent session hijacking |
||
125 | $this->authenticatedSession->migrate(); |
||
126 | |||
127 | $event->setResponse(new RedirectResponse($this->authenticatedSession->getCurrentRequestUri())); |
||
128 | |||
129 | $logger->notice('Authentication succeeded, redirecting to original location'); |
||
130 | |||
131 | return; |
||
132 | } |
||
133 | |||
134 | if ($this->nextHandler) { |
||
135 | $this->nextHandler->process($event); |
||
136 | } |
||
137 | } |
||
138 | |||
139 | public function setNext(AuthenticationHandler $handler) |
||
143 | } |
||
144 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.