Complex classes like ViewHandler 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 ViewHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class ViewHandler implements ConfigurableViewHandlerInterface |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * Key format, value a callable that returns a Response instance. |
||
| 38 | * |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected $customHandlers = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The supported formats as keys and if the given formats |
||
| 45 | * uses templating is denoted by a true value. |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $formats; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * HTTP response status code for a failed validation. |
||
| 53 | * |
||
| 54 | * @var int |
||
| 55 | */ |
||
| 56 | protected $failedValidationCode; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * HTTP response status code when the view data is null. |
||
| 60 | * |
||
| 61 | * @var int |
||
| 62 | */ |
||
| 63 | protected $emptyContentCode; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Whether or not to serialize null view data. |
||
| 67 | * |
||
| 68 | * @var bool |
||
| 69 | */ |
||
| 70 | protected $serializeNull; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * If to force a redirect for the given key format, |
||
| 74 | * with value being the status code to use. |
||
| 75 | * |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | protected $forceRedirects; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var string |
||
| 82 | */ |
||
| 83 | protected $defaultEngine; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var array |
||
| 87 | */ |
||
| 88 | protected $exclusionStrategyGroups = []; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var string |
||
| 92 | */ |
||
| 93 | protected $exclusionStrategyVersion; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var bool |
||
| 97 | */ |
||
| 98 | protected $serializeNullStrategy; |
||
| 99 | |||
| 100 | private $urlGenerator; |
||
| 101 | private $serializer; |
||
| 102 | private $templating; |
||
| 103 | private $requestStack; |
||
| 104 | private $exceptionWrapperHandler; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Constructor. |
||
| 108 | * |
||
| 109 | * @param UrlGeneratorInterface $urlGenerator The URL generator |
||
| 110 | * @param Serializer $serializer |
||
| 111 | * @param EngineInterface $templating The configured templating engine |
||
| 112 | * @param RequestStack $requestStack The request stack |
||
| 113 | * @param ExceptionWrapperHandlerInterface $exceptionWrapperHandler An exception wrapper handler |
||
| 114 | * @param array $formats the supported formats as keys and if the given formats uses templating is denoted by a true value |
||
| 115 | * @param int $failedValidationCode The HTTP response status code for a failed validation |
||
| 116 | * @param int $emptyContentCode HTTP response status code when the view data is null |
||
| 117 | * @param bool $serializeNull Whether or not to serialize null view data |
||
| 118 | * @param array $forceRedirects If to force a redirect for the given key format, with value being the status code to use |
||
| 119 | * @param string $defaultEngine default engine (twig, php ..) |
||
| 120 | */ |
||
| 121 | 75 | public function __construct( |
|
| 122 | UrlGeneratorInterface $urlGenerator, |
||
| 123 | Serializer $serializer, |
||
| 124 | EngineInterface $templating, |
||
| 125 | RequestStack $requestStack, |
||
| 126 | ExceptionWrapperHandlerInterface $exceptionWrapperHandler, |
||
| 127 | array $formats = null, |
||
| 128 | $failedValidationCode = Response::HTTP_BAD_REQUEST, |
||
| 129 | $emptyContentCode = Response::HTTP_NO_CONTENT, |
||
| 130 | $serializeNull = false, |
||
| 131 | array $forceRedirects = null, |
||
| 132 | $defaultEngine = 'twig' |
||
| 133 | ) { |
||
| 134 | 75 | $this->urlGenerator = $urlGenerator; |
|
| 135 | 75 | $this->serializer = $serializer; |
|
| 136 | 75 | $this->templating = $templating; |
|
| 137 | 75 | $this->requestStack = $requestStack; |
|
| 138 | 75 | $this->exceptionWrapperHandler = $exceptionWrapperHandler; |
|
| 139 | 75 | $this->formats = (array) $formats; |
|
| 140 | 75 | $this->failedValidationCode = $failedValidationCode; |
|
| 141 | 75 | $this->emptyContentCode = $emptyContentCode; |
|
| 142 | 75 | $this->serializeNull = $serializeNull; |
|
| 143 | 75 | $this->forceRedirects = (array) $forceRedirects; |
|
| 144 | 75 | $this->defaultEngine = $defaultEngine; |
|
| 145 | 75 | } |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Sets the default serialization groups. |
||
| 149 | * |
||
| 150 | * @param array|string $groups |
||
| 151 | */ |
||
| 152 | 1 | public function setExclusionStrategyGroups($groups) |
|
| 153 | { |
||
| 154 | 1 | $this->exclusionStrategyGroups = (array) $groups; |
|
| 155 | 1 | } |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Sets the default serialization version. |
||
| 159 | * |
||
| 160 | * @param string $version |
||
| 161 | */ |
||
| 162 | 5 | public function setExclusionStrategyVersion($version) |
|
| 163 | { |
||
| 164 | 5 | $this->exclusionStrategyVersion = $version; |
|
| 165 | 5 | } |
|
| 166 | |||
| 167 | /** |
||
| 168 | * If nulls should be serialized. |
||
| 169 | * |
||
| 170 | * @param bool $isEnabled |
||
| 171 | */ |
||
| 172 | 18 | public function setSerializeNullStrategy($isEnabled) |
|
| 173 | { |
||
| 174 | 18 | $this->serializeNullStrategy = $isEnabled; |
|
| 175 | 18 | } |
|
| 176 | |||
| 177 | /** |
||
| 178 | * {@inheritdoc} |
||
| 179 | */ |
||
| 180 | 38 | public function supports($format) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Registers a custom handler. |
||
| 187 | * |
||
| 188 | * The handler must have the following signature: handler(ViewHandler $viewHandler, View $view, Request $request, $format) |
||
| 189 | * It can use the public methods of this class to retrieve the needed data and return a |
||
| 190 | * Response object ready to be sent. |
||
| 191 | * |
||
| 192 | * @param string $format |
||
| 193 | * @param callable $callable |
||
| 194 | * |
||
| 195 | * @throws \InvalidArgumentException |
||
| 196 | */ |
||
| 197 | 16 | public function registerHandler($format, $callable) |
|
| 198 | { |
||
| 199 | 16 | if (!is_callable($callable)) { |
|
| 200 | 1 | throw new \InvalidArgumentException('Registered view callback must be callable.'); |
|
| 201 | } |
||
| 202 | |||
| 203 | 15 | $this->customHandlers[$format] = $callable; |
|
| 204 | 15 | } |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Gets a response HTTP status code from a View instance. |
||
| 208 | * |
||
| 209 | * By default it will return 200. However if there is a FormInterface stored for |
||
| 210 | * the key 'form' in the View's data it will return the failed_validation |
||
| 211 | * configuration if the form instance has errors. |
||
| 212 | * |
||
| 213 | * @param View $view |
||
| 214 | * @param mixed $content |
||
| 215 | * |
||
| 216 | * @return int HTTP status code |
||
| 217 | */ |
||
| 218 | 49 | protected function getStatusCode(View $view, $content = null) |
|
| 219 | { |
||
| 220 | 49 | $form = $this->getFormFromView($view); |
|
| 221 | |||
| 222 | 49 | if ($form && $form->isSubmitted() && !$form->isValid()) { |
|
| 223 | 7 | return $this->failedValidationCode; |
|
| 224 | } |
||
| 225 | |||
| 226 | 42 | $statusCode = $view->getStatusCode(); |
|
| 227 | 42 | if (null !== $statusCode) { |
|
| 228 | 10 | return $statusCode; |
|
| 229 | } |
||
| 230 | |||
| 231 | 32 | return null !== $content ? Response::HTTP_OK : $this->emptyContentCode; |
|
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * If the given format uses the templating system for rendering. |
||
| 236 | * |
||
| 237 | * @param string $format |
||
| 238 | * |
||
| 239 | * @return bool |
||
| 240 | */ |
||
| 241 | 40 | public function isFormatTemplating($format) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Gets or creates a JMS\Serializer\SerializationContext and initializes it with |
||
| 248 | * the view exclusion strategies, groups & versions if a new context is created. |
||
| 249 | * |
||
| 250 | * @param View $view |
||
| 251 | * |
||
| 252 | * @return Context |
||
| 253 | */ |
||
| 254 | 27 | protected function getSerializationContext(View $view) |
|
| 255 | { |
||
| 256 | 27 | $context = $view->getContext(); |
|
| 257 | |||
| 258 | 27 | $groups = $context->getGroups(); |
|
| 259 | 27 | if (empty($groups) && $this->exclusionStrategyGroups) { |
|
|
|
|||
| 260 | 1 | $context->addGroups($this->exclusionStrategyGroups); |
|
| 261 | 1 | } |
|
| 262 | |||
| 263 | 27 | if (null === $context->getVersion() && $this->exclusionStrategyVersion) { |
|
| 264 | 4 | $context->setVersion($this->exclusionStrategyVersion); |
|
| 265 | 4 | } |
|
| 266 | |||
| 267 | 27 | if (null === $context->getSerializeNull() && null !== $this->serializeNullStrategy) { |
|
| 268 | 14 | $context->setSerializeNull($this->serializeNullStrategy); |
|
| 269 | 14 | } |
|
| 270 | |||
| 271 | 27 | return $context; |
|
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Handles a request with the proper handler. |
||
| 276 | * |
||
| 277 | * Decides on which handler to use based on the request format. |
||
| 278 | * |
||
| 279 | * @param View $view |
||
| 280 | * @param Request $request |
||
| 281 | * |
||
| 282 | * @throws UnsupportedMediaTypeHttpException |
||
| 283 | * |
||
| 284 | * @return Response |
||
| 285 | */ |
||
| 286 | 34 | public function handle(View $view, Request $request = null) |
|
| 287 | { |
||
| 288 | 34 | if (null === $request) { |
|
| 289 | 7 | $request = $this->requestStack->getCurrentRequest(); |
|
| 290 | 7 | } |
|
| 291 | |||
| 292 | 34 | $format = $view->getFormat() ?: $request->getRequestFormat(); |
|
| 293 | |||
| 294 | 34 | if (!$this->supports($format)) { |
|
| 295 | 1 | $msg = "Format '$format' not supported, handler must be implemented"; |
|
| 296 | 1 | throw new UnsupportedMediaTypeHttpException($msg); |
|
| 297 | } |
||
| 298 | |||
| 299 | 33 | if (isset($this->customHandlers[$format])) { |
|
| 300 | 10 | return call_user_func($this->customHandlers[$format], $this, $view, $request, $format); |
|
| 301 | } |
||
| 302 | |||
| 303 | 23 | return $this->createResponse($view, $request, $format); |
|
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Creates the Response from the view. |
||
| 308 | * |
||
| 309 | * @param View $view |
||
| 310 | * @param string $location |
||
| 311 | * @param string $format |
||
| 312 | * |
||
| 313 | * @return Response |
||
| 314 | */ |
||
| 315 | 8 | public function createRedirectResponse(View $view, $location, $format) |
|
| 316 | { |
||
| 317 | 8 | $content = null; |
|
| 318 | 8 | if (($view->getStatusCode() == Response::HTTP_CREATED || $view->getStatusCode() == Response::HTTP_ACCEPTED) && $view->getData() != null) { |
|
| 319 | 1 | $response = $this->initResponse($view, $format); |
|
| 320 | 1 | } else { |
|
| 321 | 7 | $response = $view->getResponse(); |
|
| 322 | 7 | if ('html' === $format && isset($this->forceRedirects[$format])) { |
|
| 323 | 2 | $redirect = new RedirectResponse($location); |
|
| 324 | 2 | $content = $redirect->getContent(); |
|
| 325 | 2 | $response->setContent($content); |
|
| 326 | 2 | } |
|
| 327 | } |
||
| 328 | |||
| 329 | 8 | $code = isset($this->forceRedirects[$format]) |
|
| 330 | 8 | ? $this->forceRedirects[$format] : $this->getStatusCode($view, $content); |
|
| 331 | |||
| 332 | 8 | $response->setStatusCode($code); |
|
| 333 | 8 | $response->headers->set('Location', $location); |
|
| 334 | |||
| 335 | 8 | return $response; |
|
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Renders the view data with the given template. |
||
| 340 | * |
||
| 341 | * @param View $view |
||
| 342 | * @param string $format |
||
| 343 | * |
||
| 344 | * @return string |
||
| 345 | */ |
||
| 346 | 13 | public function renderTemplate(View $view, $format) |
|
| 347 | { |
||
| 348 | 13 | $data = $this->prepareTemplateParameters($view); |
|
| 349 | |||
| 350 | 13 | $template = $view->getTemplate(); |
|
| 351 | 13 | if ($template instanceof TemplateReferenceInterface) { |
|
| 352 | if (null === $template->get('format')) { |
||
| 353 | $template->set('format', $format); |
||
| 354 | } |
||
| 355 | |||
| 356 | if (null === $template->get('engine')) { |
||
| 357 | $engine = $view->getEngine() ?: $this->defaultEngine; |
||
| 358 | $template->set('engine', $engine); |
||
| 359 | } |
||
| 360 | } |
||
| 361 | |||
| 362 | 13 | return $this->templating->render($template, $data); |
|
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Prepares view data for use by templating engine. |
||
| 367 | * |
||
| 368 | * @param View $view |
||
| 369 | * |
||
| 370 | * @return array |
||
| 371 | */ |
||
| 372 | 20 | public function prepareTemplateParameters(View $view) |
|
| 373 | { |
||
| 374 | 20 | $data = $view->getData(); |
|
| 375 | |||
| 376 | 20 | if ($data instanceof FormInterface) { |
|
| 377 | 2 | $data = [$view->getTemplateVar() => $data->getData(), 'form' => $data]; |
|
| 378 | 20 | } elseif (empty($data) || !is_array($data) || is_numeric((key($data)))) { |
|
| 379 | 11 | $data = [$view->getTemplateVar() => $data]; |
|
| 380 | 11 | } |
|
| 381 | |||
| 382 | 20 | if (isset($data['form']) && $data['form'] instanceof FormInterface) { |
|
| 383 | 2 | $data['form'] = $data['form']->createView(); |
|
| 384 | 2 | } |
|
| 385 | |||
| 386 | 20 | $templateData = $view->getTemplateData(); |
|
| 387 | 20 | if (is_callable($templateData)) { |
|
| 388 | 2 | $templateData = call_user_func($templateData, $this, $view); |
|
| 389 | 2 | } |
|
| 390 | |||
| 391 | 20 | return array_merge($data, $templateData); |
|
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Handles creation of a Response using either redirection or the templating/serializer service. |
||
| 396 | * |
||
| 397 | * @param View $view |
||
| 398 | * @param Request $request |
||
| 399 | * @param string $format |
||
| 400 | * |
||
| 401 | * @return Response |
||
| 402 | */ |
||
| 403 | 45 | public function createResponse(View $view, Request $request, $format) |
|
| 423 | |||
| 424 | /** |
||
| 425 | * Initializes a response object that represents the view and holds the view's status code. |
||
| 426 | * |
||
| 427 | * @param View $view |
||
| 428 | * @param string $format |
||
| 429 | * |
||
| 430 | * @return Response |
||
| 431 | */ |
||
| 432 | 38 | private function initResponse(View $view, $format) |
|
| 433 | { |
||
| 434 | 38 | $content = null; |
|
| 453 | |||
| 454 | /** |
||
| 455 | * Returns the form from the given view if present, false otherwise. |
||
| 456 | * |
||
| 457 | * @param View $view |
||
| 458 | * |
||
| 459 | * @return bool|FormInterface |
||
| 460 | */ |
||
| 461 | 49 | protected function getFormFromView(View $view) |
|
| 475 | |||
| 476 | /** |
||
| 477 | * Returns the data from a view. If the data is form with errors, it will return it wrapped in an ExceptionWrapper. |
||
| 478 | * |
||
| 479 | * @param View $view |
||
| 480 | * |
||
| 481 | * @return mixed|null |
||
| 482 | */ |
||
| 483 | 24 | private function getDataFromView(View $view) |
|
| 503 | } |
||
| 504 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.