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 |
||
31 | final class ViewHandler implements ConfigurableViewHandlerInterface |
||
32 | { |
||
33 | /** |
||
34 | * Key format, value a callable that returns a Response instance. |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | private $customHandlers = []; |
||
39 | |||
40 | /** |
||
41 | * The supported formats as keys. |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | private $formats; |
||
46 | private $failedValidationCode; |
||
47 | private $emptyContentCode; |
||
48 | private $serializeNull; |
||
49 | private $exclusionStrategyGroups = []; |
||
50 | private $exclusionStrategyVersion; |
||
51 | private $serializeNullStrategy; |
||
52 | private $urlGenerator; |
||
53 | private $serializer; |
||
54 | private $requestStack; |
||
55 | private $options; |
||
56 | |||
57 | 54 | private function __construct( |
|
58 | UrlGeneratorInterface $urlGenerator, |
||
59 | Serializer $serializer, |
||
60 | RequestStack $requestStack, |
||
61 | array $formats = null, |
||
62 | int $failedValidationCode = Response::HTTP_BAD_REQUEST, |
||
63 | int $emptyContentCode = Response::HTTP_NO_CONTENT, |
||
64 | bool $serializeNull = false, |
||
65 | array $options = [] |
||
66 | ) { |
||
67 | 54 | $this->urlGenerator = $urlGenerator; |
|
68 | 54 | $this->serializer = $serializer; |
|
69 | 54 | $this->requestStack = $requestStack; |
|
70 | 54 | $this->formats = (array) $formats; |
|
71 | 54 | $this->failedValidationCode = $failedValidationCode; |
|
72 | 54 | $this->emptyContentCode = $emptyContentCode; |
|
73 | 54 | $this->serializeNull = $serializeNull; |
|
74 | 54 | $this->options = $options + [ |
|
75 | 54 | 'exclusionStrategyGroups' => [], |
|
76 | 'exclusionStrategyVersion' => null, |
||
77 | 'serializeNullStrategy' => null, |
||
78 | ]; |
||
79 | 54 | $this->reset(); |
|
80 | 54 | } |
|
81 | |||
82 | 54 | public static function create( |
|
83 | UrlGeneratorInterface $urlGenerator, |
||
84 | Serializer $serializer, |
||
85 | RequestStack $requestStack, |
||
86 | array $formats = null, |
||
87 | int $failedValidationCode = Response::HTTP_BAD_REQUEST, |
||
88 | int $emptyContentCode = Response::HTTP_NO_CONTENT, |
||
89 | bool $serializeNull = false, |
||
90 | array $options = [] |
||
91 | ): self |
||
92 | { |
||
93 | 54 | return new self($urlGenerator, $serializer, $requestStack, $formats, $failedValidationCode, $emptyContentCode, $serializeNull, $options); |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * @param string[]|string $groups |
||
98 | */ |
||
99 | 1 | public function setExclusionStrategyGroups($groups): void |
|
100 | { |
||
101 | 1 | $this->exclusionStrategyGroups = (array) $groups; |
|
102 | 1 | } |
|
103 | |||
104 | 8 | public function setExclusionStrategyVersion(string $version): void |
|
105 | { |
||
106 | 8 | $this->exclusionStrategyVersion = $version; |
|
107 | 8 | } |
|
108 | |||
109 | 3 | public function setSerializeNullStrategy(bool $isEnabled): void |
|
110 | { |
||
111 | 3 | $this->serializeNullStrategy = $isEnabled; |
|
112 | 3 | } |
|
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | 32 | public function supports(string $format): bool |
|
118 | { |
||
119 | 32 | return isset($this->customHandlers[$format]) || isset($this->formats[$format]); |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * Registers a custom handler. |
||
124 | * |
||
125 | * The handler must have the following signature: handler(ViewHandler $viewHandler, View $view, Request $request, $format) |
||
126 | * It can use the public methods of this class to retrieve the needed data and return a |
||
127 | * Response object ready to be sent. |
||
128 | */ |
||
129 | 15 | public function registerHandler(string $format, callable $callable): void |
|
130 | { |
||
131 | 15 | $this->customHandlers[$format] = $callable; |
|
132 | 15 | } |
|
133 | |||
134 | /** |
||
135 | * Handles a request with the proper handler. |
||
136 | * |
||
137 | * Decides on which handler to use based on the request format. |
||
138 | * |
||
139 | * @throws UnsupportedMediaTypeHttpException |
||
140 | */ |
||
141 | 28 | public function handle(View $view, Request $request = null): Response |
|
142 | { |
||
143 | 28 | if (null === $request) { |
|
144 | 2 | $request = $this->requestStack->getCurrentRequest(); |
|
145 | } |
||
146 | |||
147 | 28 | $format = $view->getFormat() ?: $request->getRequestFormat(); |
|
|
|||
148 | |||
149 | 28 | if (!$this->supports($format)) { |
|
150 | 1 | $msg = "Format '$format' not supported, handler must be implemented"; |
|
151 | |||
152 | 1 | throw new UnsupportedMediaTypeHttpException($msg); |
|
153 | } |
||
154 | |||
155 | 27 | if (isset($this->customHandlers[$format])) { |
|
156 | 10 | return call_user_func($this->customHandlers[$format], $this, $view, $request, $format); |
|
157 | } |
||
158 | |||
159 | 17 | return $this->createResponse($view, $request, $format); |
|
160 | } |
||
161 | |||
162 | 4 | public function createRedirectResponse(View $view, string $location, string $format): Response |
|
163 | { |
||
164 | 4 | $content = null; |
|
165 | 4 | if ((Response::HTTP_CREATED === $view->getStatusCode() || Response::HTTP_ACCEPTED === $view->getStatusCode()) && null !== $view->getData()) { |
|
166 | 1 | $response = $this->initResponse($view, $format); |
|
167 | } else { |
||
168 | 3 | $response = $view->getResponse(); |
|
169 | } |
||
170 | |||
171 | 4 | $code = $this->getStatusCode($view, $content); |
|
172 | |||
173 | 4 | $response->setStatusCode($code); |
|
174 | 4 | $response->headers->set('Location', $location); |
|
175 | |||
176 | 4 | return $response; |
|
177 | } |
||
178 | |||
179 | 33 | public function createResponse(View $view, Request $request, string $format): Response |
|
180 | { |
||
181 | 33 | $route = $view->getRoute(); |
|
182 | |||
183 | 33 | $location = $route |
|
184 | 2 | ? $this->urlGenerator->generate($route, (array) $view->getRouteParameters(), UrlGeneratorInterface::ABSOLUTE_URL) |
|
185 | 33 | : $view->getLocation(); |
|
186 | |||
187 | 33 | if ($location) { |
|
188 | 4 | return $this->createRedirectResponse($view, $location, $format); |
|
189 | } |
||
190 | |||
191 | 29 | $response = $this->initResponse($view, $format); |
|
192 | |||
193 | 29 | if (!$response->headers->has('Content-Type')) { |
|
194 | 29 | $mimeType = $request->attributes->get('media_type'); |
|
195 | 29 | if (null === $mimeType) { |
|
196 | 22 | $mimeType = $request->getMimeType($format); |
|
197 | } |
||
198 | |||
199 | 29 | $response->headers->set('Content-Type', $mimeType); |
|
200 | } |
||
201 | |||
202 | 29 | return $response; |
|
203 | } |
||
204 | |||
205 | /** |
||
206 | * Gets a response HTTP status code from a View instance. |
||
207 | * |
||
208 | * By default it will return 200. However if there is a FormInterface stored for |
||
209 | * the key 'form' in the View's data it will return the failed_validation |
||
210 | * configuration if the form instance has errors. |
||
211 | * |
||
212 | * @param string|false|null |
||
213 | */ |
||
214 | 40 | private function getStatusCode(View $view, $content = null): int |
|
229 | |||
230 | 30 | private function getSerializationContext(View $view): Context |
|
231 | { |
||
232 | 30 | $context = $view->getContext(); |
|
233 | |||
234 | 30 | $groups = $context->getGroups(); |
|
235 | 30 | if (empty($groups) && $this->exclusionStrategyGroups) { |
|
236 | 1 | $context->setGroups($this->exclusionStrategyGroups); |
|
237 | } |
||
238 | |||
239 | 30 | if (null === $context->getVersion() && $this->exclusionStrategyVersion) { |
|
253 | |||
254 | 30 | private function initResponse(View $view, string $format): Response |
|
278 | |||
279 | 40 | private function getFormFromView(View $view): ?FormInterface |
|
293 | |||
294 | 27 | private function getDataFromView(View $view) |
|
304 | |||
305 | 54 | public function reset(): void |
|
311 | } |
||
312 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: