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 | |||
| 105 | /** |
||
| 106 | * Constructor. |
||
| 107 | * |
||
| 108 | * @param UrlGeneratorInterface $urlGenerator The URL generator |
||
| 109 | * @param Serializer $serializer |
||
| 110 | * @param EngineInterface $templating The configured templating engine |
||
| 111 | * @param RequestStack $requestStack The request stack |
||
| 112 | * @param array $formats the supported formats as keys and if the given formats uses templating is denoted by a true value |
||
| 113 | * @param int $failedValidationCode The HTTP response status code for a failed validation |
||
| 114 | * @param int $emptyContentCode HTTP response status code when the view data is null |
||
| 115 | * @param bool $serializeNull Whether or not to serialize null view data |
||
| 116 | * @param array $forceRedirects If to force a redirect for the given key format, with value being the status code to use |
||
| 117 | * @param string $defaultEngine default engine (twig, php ..) |
||
| 118 | */ |
||
| 119 | 79 | public function __construct( |
|
| 120 | UrlGeneratorInterface $urlGenerator, |
||
| 121 | Serializer $serializer, |
||
| 122 | EngineInterface $templating, |
||
| 123 | RequestStack $requestStack, |
||
| 124 | array $formats = null, |
||
| 125 | $failedValidationCode = Response::HTTP_BAD_REQUEST, |
||
| 126 | $emptyContentCode = Response::HTTP_NO_CONTENT, |
||
| 127 | $serializeNull = false, |
||
| 128 | array $forceRedirects = null, |
||
| 129 | $defaultEngine = 'twig' |
||
| 130 | ) { |
||
| 131 | 79 | $this->urlGenerator = $urlGenerator; |
|
| 132 | 79 | $this->serializer = $serializer; |
|
| 133 | 79 | $this->templating = $templating; |
|
| 134 | 79 | $this->requestStack = $requestStack; |
|
| 135 | 79 | $this->formats = (array) $formats; |
|
| 136 | 79 | $this->failedValidationCode = $failedValidationCode; |
|
| 137 | 79 | $this->emptyContentCode = $emptyContentCode; |
|
| 138 | 79 | $this->serializeNull = $serializeNull; |
|
| 139 | 79 | $this->forceRedirects = (array) $forceRedirects; |
|
| 140 | 79 | $this->defaultEngine = $defaultEngine; |
|
| 141 | 79 | } |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Sets the default serialization groups. |
||
| 145 | * |
||
| 146 | * @param array|string $groups |
||
| 147 | */ |
||
| 148 | 1 | public function setExclusionStrategyGroups($groups) |
|
| 149 | { |
||
| 150 | 1 | $this->exclusionStrategyGroups = (array) $groups; |
|
| 151 | 1 | } |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Sets the default serialization version. |
||
| 155 | * |
||
| 156 | * @param string $version |
||
| 157 | */ |
||
| 158 | 5 | public function setExclusionStrategyVersion($version) |
|
| 162 | |||
| 163 | /** |
||
| 164 | * If nulls should be serialized. |
||
| 165 | * |
||
| 166 | * @param bool $isEnabled |
||
| 167 | */ |
||
| 168 | 23 | public function setSerializeNullStrategy($isEnabled) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * {@inheritdoc} |
||
| 175 | */ |
||
| 176 | 42 | public function supports($format) |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Registers a custom handler. |
||
| 183 | * |
||
| 184 | * The handler must have the following signature: handler(ViewHandler $viewHandler, View $view, Request $request, $format) |
||
| 185 | * It can use the public methods of this class to retrieve the needed data and return a |
||
| 186 | * Response object ready to be sent. |
||
| 187 | * |
||
| 188 | * @param string $format |
||
| 189 | * @param callable $callable |
||
| 190 | * |
||
| 191 | * @throws \InvalidArgumentException |
||
| 192 | */ |
||
| 193 | 16 | public function registerHandler($format, $callable) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Gets a response HTTP status code from a View instance. |
||
| 204 | * |
||
| 205 | * By default it will return 200. However if there is a FormInterface stored for |
||
| 206 | * the key 'form' in the View's data it will return the failed_validation |
||
| 207 | * configuration if the form instance has errors. |
||
| 208 | * |
||
| 209 | * @param View $view |
||
| 210 | * @param mixed $content |
||
| 211 | * |
||
| 212 | * @return int HTTP status code |
||
| 213 | */ |
||
| 214 | 53 | protected function getStatusCode(View $view, $content = null) |
|
| 215 | { |
||
| 216 | 53 | $form = $this->getFormFromView($view); |
|
| 217 | |||
| 218 | 53 | if ($form && $form->isSubmitted() && !$form->isValid()) { |
|
| 219 | 7 | return $this->failedValidationCode; |
|
| 220 | } |
||
| 221 | |||
| 222 | 46 | $statusCode = $view->getStatusCode(); |
|
| 223 | 46 | if (null !== $statusCode) { |
|
| 224 | 15 | return $statusCode; |
|
| 225 | } |
||
| 226 | |||
| 227 | 31 | return null !== $content ? Response::HTTP_OK : $this->emptyContentCode; |
|
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * If the given format uses the templating system for rendering. |
||
| 232 | * |
||
| 233 | * @param string $format |
||
| 234 | * |
||
| 235 | * @return bool |
||
| 236 | */ |
||
| 237 | 44 | public function isFormatTemplating($format) |
|
| 238 | { |
||
| 239 | 44 | return !empty($this->formats[$format]); |
|
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Gets or creates a JMS\Serializer\SerializationContext and initializes it with |
||
| 244 | * the view exclusion strategies, groups & versions if a new context is created. |
||
| 245 | * |
||
| 246 | * @param View $view |
||
| 247 | * |
||
| 248 | * @return Context |
||
| 249 | */ |
||
| 250 | 30 | protected function getSerializationContext(View $view) |
|
| 251 | { |
||
| 252 | 30 | $context = $view->getContext(); |
|
| 253 | |||
| 254 | 30 | $groups = $context->getGroups(); |
|
| 255 | 30 | if (empty($groups) && $this->exclusionStrategyGroups) { |
|
|
|
|||
| 256 | 1 | $context->addGroups($this->exclusionStrategyGroups); |
|
| 257 | 1 | } |
|
| 258 | |||
| 259 | 30 | if (null === $context->getVersion() && $this->exclusionStrategyVersion) { |
|
| 260 | 4 | $context->setVersion($this->exclusionStrategyVersion); |
|
| 261 | 4 | } |
|
| 262 | |||
| 263 | 30 | if (null === $context->getSerializeNull() && null !== $this->serializeNullStrategy) { |
|
| 264 | 17 | $context->setSerializeNull($this->serializeNullStrategy); |
|
| 265 | 17 | } |
|
| 266 | |||
| 267 | 30 | return $context; |
|
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Handles a request with the proper handler. |
||
| 272 | * |
||
| 273 | * Decides on which handler to use based on the request format. |
||
| 274 | * |
||
| 275 | * @param View $view |
||
| 276 | * @param Request $request |
||
| 277 | * |
||
| 278 | * @throws UnsupportedMediaTypeHttpException |
||
| 279 | * |
||
| 280 | * @return Response |
||
| 281 | */ |
||
| 282 | 38 | public function handle(View $view, Request $request = null) |
|
| 283 | { |
||
| 284 | 38 | if (null === $request) { |
|
| 285 | 12 | $request = $this->requestStack->getCurrentRequest(); |
|
| 286 | 12 | } |
|
| 287 | |||
| 288 | 38 | $format = $view->getFormat() ?: $request->getRequestFormat(); |
|
| 289 | |||
| 290 | 38 | if (!$this->supports($format)) { |
|
| 291 | 1 | $msg = "Format '$format' not supported, handler must be implemented"; |
|
| 292 | 1 | throw new UnsupportedMediaTypeHttpException($msg); |
|
| 293 | } |
||
| 294 | |||
| 295 | 37 | if (isset($this->customHandlers[$format])) { |
|
| 296 | 10 | return call_user_func($this->customHandlers[$format], $this, $view, $request, $format); |
|
| 297 | } |
||
| 298 | |||
| 299 | 27 | return $this->createResponse($view, $request, $format); |
|
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Creates the Response from the view. |
||
| 304 | * |
||
| 305 | * @param View $view |
||
| 306 | * @param string $location |
||
| 307 | * @param string $format |
||
| 308 | * |
||
| 309 | * @return Response |
||
| 310 | */ |
||
| 311 | 8 | public function createRedirectResponse(View $view, $location, $format) |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Renders the view data with the given template. |
||
| 336 | * |
||
| 337 | * @param View $view |
||
| 338 | * @param string $format |
||
| 339 | * |
||
| 340 | * @return string |
||
| 341 | */ |
||
| 342 | 14 | public function renderTemplate(View $view, $format) |
|
| 343 | { |
||
| 344 | 14 | $data = $this->prepareTemplateParameters($view); |
|
| 345 | |||
| 346 | 14 | $template = $view->getTemplate(); |
|
| 347 | 14 | if ($template instanceof TemplateReferenceInterface) { |
|
| 348 | 2 | if (null === $template->get('format')) { |
|
| 349 | $template->set('format', $format); |
||
| 350 | } |
||
| 351 | |||
| 352 | 2 | if (null === $template->get('engine')) { |
|
| 353 | $engine = $view->getEngine() ?: $this->defaultEngine; |
||
| 354 | $template->set('engine', $engine); |
||
| 355 | } |
||
| 356 | 2 | } |
|
| 357 | |||
| 358 | 14 | return $this->templating->render($template, $data); |
|
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Prepares view data for use by templating engine. |
||
| 363 | * |
||
| 364 | * @param View $view |
||
| 365 | * |
||
| 366 | * @return array |
||
| 367 | */ |
||
| 368 | 21 | public function prepareTemplateParameters(View $view) |
|
| 369 | { |
||
| 370 | 21 | $data = $view->getData(); |
|
| 371 | |||
| 372 | 21 | if ($data instanceof FormInterface) { |
|
| 373 | 2 | $data = [$view->getTemplateVar() => $data->getData(), 'form' => $data]; |
|
| 374 | 21 | } elseif (empty($data) || !is_array($data) || is_numeric((key($data)))) { |
|
| 375 | 12 | $data = [$view->getTemplateVar() => $data]; |
|
| 376 | 12 | } |
|
| 377 | |||
| 378 | 21 | if (isset($data['form']) && $data['form'] instanceof FormInterface) { |
|
| 379 | 2 | $data['form'] = $data['form']->createView(); |
|
| 380 | 2 | } |
|
| 381 | |||
| 382 | 21 | $templateData = $view->getTemplateData(); |
|
| 383 | 21 | if (is_callable($templateData)) { |
|
| 384 | 2 | $templateData = call_user_func($templateData, $this, $view); |
|
| 385 | 2 | } |
|
| 386 | |||
| 387 | 21 | return array_merge($data, $templateData); |
|
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Handles creation of a Response using either redirection or the templating/serializer service. |
||
| 392 | * |
||
| 393 | * @param View $view |
||
| 394 | * @param Request $request |
||
| 395 | * @param string $format |
||
| 396 | * |
||
| 397 | * @return Response |
||
| 398 | */ |
||
| 399 | 49 | public function createResponse(View $view, Request $request, $format) |
|
| 400 | { |
||
| 401 | 49 | $route = $view->getRoute(); |
|
| 402 | |||
| 403 | $location = $route |
||
| 404 | 49 | ? $this->urlGenerator->generate($route, (array) $view->getRouteParameters(), UrlGeneratorInterface::ABSOLUTE_URL) |
|
| 405 | 49 | : $view->getLocation(); |
|
| 406 | |||
| 407 | 49 | if ($location) { |
|
| 408 | 8 | return $this->createRedirectResponse($view, $location, $format); |
|
| 409 | } |
||
| 410 | |||
| 411 | 41 | $response = $this->initResponse($view, $format); |
|
| 412 | |||
| 413 | 41 | if (!$response->headers->has('Content-Type')) { |
|
| 414 | 41 | $response->headers->set('Content-Type', $request->getMimeType($format)); |
|
| 415 | 41 | } |
|
| 416 | |||
| 417 | 41 | return $response; |
|
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Initializes a response object that represents the view and holds the view's status code. |
||
| 422 | * |
||
| 423 | * @param View $view |
||
| 424 | * @param string $format |
||
| 425 | * |
||
| 426 | * @return Response |
||
| 427 | */ |
||
| 428 | 42 | private function initResponse(View $view, $format) |
|
| 429 | { |
||
| 430 | 42 | $content = null; |
|
| 431 | 42 | if ($this->isFormatTemplating($format)) { |
|
| 432 | 14 | $content = $this->renderTemplate($view, $format); |
|
| 433 | 42 | } elseif ($this->serializeNull || null !== $view->getData()) { |
|
| 434 | 27 | $data = $this->getDataFromView($view); |
|
| 435 | |||
| 436 | 27 | if ($data instanceof FormInterface && $data->isSubmitted() && !$data->isValid()) { |
|
| 437 | 6 | $view->getContext()->setAttribute('status_code', $this->failedValidationCode); |
|
| 438 | 6 | } |
|
| 439 | |||
| 440 | 27 | $context = $this->getSerializationContext($view); |
|
| 441 | 27 | $context->setAttribute('template_data', $view->getTemplateData()); |
|
| 442 | |||
| 443 | 27 | $content = $this->serializer->serialize($data, $format, $context); |
|
| 444 | 27 | } |
|
| 445 | |||
| 446 | 42 | $response = $view->getResponse(); |
|
| 447 | 42 | $response->setStatusCode($this->getStatusCode($view, $content)); |
|
| 448 | |||
| 449 | 42 | if (null !== $content) { |
|
| 450 | 37 | $response->setContent($content); |
|
| 451 | 37 | } |
|
| 452 | |||
| 453 | 42 | return $response; |
|
| 454 | } |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Returns the form from the given view if present, false otherwise. |
||
| 458 | * |
||
| 459 | * @param View $view |
||
| 460 | * |
||
| 461 | * @return bool|FormInterface |
||
| 462 | */ |
||
| 463 | 53 | protected function getFormFromView(View $view) |
|
| 464 | { |
||
| 465 | 53 | $data = $view->getData(); |
|
| 466 | |||
| 467 | 53 | if ($data instanceof FormInterface) { |
|
| 468 | 7 | return $data; |
|
| 469 | } |
||
| 470 | |||
| 471 | 46 | if (is_array($data) && isset($data['form']) && $data['form'] instanceof FormInterface) { |
|
| 472 | 4 | return $data['form']; |
|
| 473 | } |
||
| 474 | |||
| 475 | 42 | return false; |
|
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Returns the data from a view. |
||
| 480 | * |
||
| 481 | * @param View $view |
||
| 482 | * |
||
| 483 | * @return mixed|null |
||
| 484 | */ |
||
| 485 | 27 | private function getDataFromView(View $view) |
|
| 495 | } |
||
| 496 |
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.