Total Complexity | 40 |
Total Lines | 261 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like HttpRedirectBinding often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use HttpRedirectBinding, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class HttpRedirectBinding extends AbstractBinding |
||
27 | { |
||
28 | /** |
||
29 | * @param string|null $destination |
||
30 | * |
||
31 | * @return \Symfony\Component\HttpFoundation\Response |
||
32 | */ |
||
33 | public function send(MessageContext $context, $destination = null) |
||
34 | { |
||
35 | $destination = $context->getMessage()->getDestination() ? $context->getMessage()->getDestination() : $destination; |
||
36 | |||
37 | $url = $this->getRedirectURL($context, $destination); |
||
38 | |||
39 | return new RedirectResponse($url); |
||
40 | } |
||
41 | |||
42 | public function receive(Request $request, MessageContext $context) |
||
43 | { |
||
44 | $data = $this->parseQuery($request); |
||
45 | |||
46 | $this->processData($data, $context); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @throws \Exception |
||
51 | */ |
||
52 | protected function processData(array $data, MessageContext $context) |
||
53 | { |
||
54 | $msg = $this->getMessageStringFromData($data); |
||
55 | $encoding = $this->getEncodingFromData($data); |
||
56 | $msg = $this->decodeMessageString($msg, $encoding); |
||
57 | |||
58 | $this->dispatchReceive($msg); |
||
59 | |||
60 | $deserializationContext = $context->getDeserializationContext(); |
||
61 | $message = SamlMessage::fromXML($msg, $deserializationContext); |
||
62 | |||
63 | $this->loadRelayState($message, $data); |
||
64 | $this->loadSignature($message, $data); |
||
65 | |||
66 | $context->setMessage($message); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @return string |
||
71 | * |
||
72 | * @throws LightSamlBindingException |
||
73 | */ |
||
74 | protected function getMessageStringFromData(array $data) |
||
75 | { |
||
76 | if (array_key_exists('SAMLRequest', $data)) { |
||
77 | return $data['SAMLRequest']; |
||
78 | } elseif (array_key_exists('SAMLResponse', $data)) { |
||
79 | return $data['SAMLResponse']; |
||
80 | } else { |
||
81 | throw new LightSamlBindingException('Missing SAMLRequest or SAMLResponse parameter'); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @return string |
||
87 | */ |
||
88 | protected function getEncodingFromData(array $data) |
||
89 | { |
||
90 | if (array_key_exists('SAMLEncoding', $data)) { |
||
91 | return $data['SAMLEncoding']; |
||
92 | } else { |
||
93 | return SamlConstants::ENCODING_DEFLATE; |
||
94 | } |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param string $msg |
||
99 | * @param string $encoding |
||
100 | * |
||
101 | * @throws \LightSaml\Error\LightSamlBindingException |
||
102 | * |
||
103 | * @return string |
||
104 | */ |
||
105 | protected function decodeMessageString($msg, $encoding) |
||
106 | { |
||
107 | $msg = base64_decode($msg); |
||
108 | switch ($encoding) { |
||
109 | case SamlConstants::ENCODING_DEFLATE: |
||
110 | return gzinflate($msg); |
||
111 | break; |
||
|
|||
112 | default: |
||
113 | throw new LightSamlBindingException(sprintf("Unknown encoding '%s'", $encoding)); |
||
114 | } |
||
115 | } |
||
116 | |||
117 | protected function loadRelayState(SamlMessage $message, array $data) |
||
118 | { |
||
119 | if (array_key_exists('RelayState', $data)) { |
||
120 | $message->setRelayState($data['RelayState']); |
||
121 | } |
||
122 | } |
||
123 | |||
124 | protected function loadSignature(SamlMessage $message, array $data) |
||
125 | { |
||
126 | if (array_key_exists('Signature', $data)) { |
||
127 | if (false == array_key_exists('SigAlg', $data)) { |
||
128 | throw new LightSamlBindingException('Missing signature algorithm'); |
||
129 | } |
||
130 | $message->setSignature( |
||
131 | new SignatureStringReader($data['Signature'], $data['SigAlg'], $data['SignedQuery']) |
||
132 | ); |
||
133 | } |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @param string|null $destination |
||
138 | * |
||
139 | * @return string |
||
140 | */ |
||
141 | protected function getRedirectURL(MessageContext $context, $destination) |
||
142 | { |
||
143 | $message = MessageContextHelper::asSamlMessage($context); |
||
144 | $signature = $message->getSignature(); |
||
145 | if ($signature && false == $signature instanceof SignatureWriter) { |
||
146 | throw new LightSamlBindingException('Signature must be SignatureWriter'); |
||
147 | } |
||
148 | |||
149 | $xml = $this->getMessageEncodedXml($message, $context); |
||
150 | $msg = $this->addMessageToUrl($message, $xml); |
||
151 | $this->addRelayStateToUrl($msg, $message); |
||
152 | $this->addSignatureToUrl($msg, $signature); |
||
153 | |||
154 | return $this->getDestinationUrl($msg, $message, $destination); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @return string |
||
159 | */ |
||
160 | protected function getMessageEncodedXml(SamlMessage $message, MessageContext $context) |
||
161 | { |
||
162 | $message->setSignature(null); |
||
163 | |||
164 | $serializationContext = $context->getSerializationContext(); |
||
165 | $message->serialize($serializationContext->getDocument(), $serializationContext); |
||
166 | $xml = $serializationContext->getDocument()->saveXML(); |
||
167 | |||
168 | $this->dispatchSend($xml); |
||
169 | |||
170 | $xml = gzdeflate($xml); |
||
171 | $xml = base64_encode($xml); |
||
172 | |||
173 | return $xml; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * @param string $xml |
||
178 | * |
||
179 | * @return string |
||
180 | */ |
||
181 | protected function addMessageToUrl(SamlMessage $message, $xml) |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * @param string $msg |
||
195 | */ |
||
196 | protected function addRelayStateToUrl(&$msg, SamlMessage $message) |
||
197 | { |
||
198 | if (null !== $message->getRelayState()) { |
||
199 | $msg .= '&RelayState='.urlencode($message->getRelayState()); |
||
200 | } |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @param string $msg |
||
205 | */ |
||
206 | protected function addSignatureToUrl(&$msg, SignatureWriter $signature = null) |
||
215 | } |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * @param string $msg |
||
220 | * @param string|null $destination |
||
221 | * |
||
222 | * @return string |
||
223 | */ |
||
224 | protected function getDestinationUrl($msg, SamlMessage $message, $destination) |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * @return array |
||
238 | */ |
||
239 | protected function parseQuery(Request $request) |
||
240 | { |
||
241 | /* |
||
242 | * Parse the query string. We need to do this ourself, so that we get access |
||
243 | * to the raw (urlencoded) values. This is required because different software |
||
244 | * can urlencode to different values. |
||
245 | */ |
||
246 | $sigQuery = $relayState = $sigAlg = ''; |
||
247 | $data = $this->parseQueryString($request->server->get('QUERY_STRING'), false); |
||
248 | $result = []; |
||
249 | foreach ($data as $name => $value) { |
||
250 | $result[$name] = urldecode($value); |
||
251 | switch ($name) { |
||
252 | case 'SAMLRequest': |
||
253 | case 'SAMLResponse': |
||
254 | $sigQuery = $name.'='.$value; |
||
255 | break; |
||
256 | case 'RelayState': |
||
257 | $relayState = '&RelayState='.$value; |
||
258 | break; |
||
259 | case 'SigAlg': |
||
260 | $sigAlg = '&SigAlg='.$value; |
||
261 | break; |
||
262 | } |
||
263 | } |
||
264 | $result['SignedQuery'] = $sigQuery.$relayState.$sigAlg; |
||
265 | |||
266 | return $result; |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * @param string $queryString |
||
271 | * @param bool $urlDecodeValues |
||
272 | * |
||
273 | * @return array |
||
274 | */ |
||
275 | protected function parseQueryString($queryString, $urlDecodeValues = false) |
||
287 | } |
||
288 | } |
||
289 |
The
break
statement is not necessary if it is preceded for example by areturn
statement:If you would like to keep this construct to be consistent with other
case
statements, you can safely mark this issue as a false-positive.