Complex classes like Controller 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 Controller, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | class Controller implements \JsonSerializable |
||
| 45 | { |
||
| 46 | use Helper; |
||
| 47 | use ResponseTrait; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | protected $module; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | protected $controller; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | protected $template; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | protected $file; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var Meta |
||
| 71 | */ |
||
| 72 | protected $meta; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var Data |
||
| 76 | */ |
||
| 77 | protected $data; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * One of HTML, JSON or empty string |
||
| 81 | * @var string |
||
| 82 | */ |
||
| 83 | protected $render = 'HTML'; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Constructor of Statement |
||
| 87 | * |
||
| 88 | * @param string $module |
||
| 89 | * @param string $controller |
||
| 90 | */ |
||
| 91 | 676 | public function __construct($module, $controller) |
|
| 92 | { |
||
| 93 | 676 | $this->module = $module; |
|
| 94 | 676 | $this->controller = $controller; |
|
| 95 | 676 | $this->template = $controller . '.phtml'; |
|
| 96 | |||
| 97 | // initial default helper path |
||
| 98 | 676 | $this->addHelperPath(__DIR__ . '/Helper/'); |
|
| 99 | 676 | } |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Check `Privilege` |
||
| 103 | * |
||
| 104 | * @throws ForbiddenException |
||
| 105 | */ |
||
| 106 | 9 | public function checkPrivilege() |
|
| 107 | { |
||
| 108 | 9 | $privilege = $this->getMeta()->getPrivilege(); |
|
| 109 | 8 | if ($privilege && !Acl::isAllowed($this->module, $privilege)) { |
|
| 110 | throw new ForbiddenException; |
||
| 111 | } |
||
| 112 | 8 | } |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Check `Method` |
||
| 116 | * |
||
| 117 | * @throws NotAllowedException |
||
| 118 | */ |
||
| 119 | 9 | public function checkMethod() |
|
| 120 | { |
||
| 121 | 9 | $methods = $this->getMeta()->getMethod(); |
|
| 122 | 8 | if ($methods && !in_array(Request::getMethod(), $methods)) { |
|
| 123 | throw new NotAllowedException(implode(',', $methods)); |
||
| 124 | } |
||
| 125 | 8 | } |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Check `Accept` |
||
| 129 | * |
||
| 130 | * @throws NotAcceptableException |
||
| 131 | */ |
||
| 132 | 9 | public function checkAccept() |
|
| 174 | |||
| 175 | /** |
||
| 176 | * __invoke |
||
| 177 | * |
||
| 178 | * @param array $params |
||
| 179 | * @return Data |
||
| 180 | * @throws ControllerException |
||
| 181 | */ |
||
| 182 | 8 | public function run(array $params = []) |
|
| 183 | { |
||
| 184 | // initial variables for use inside controller |
||
| 185 | 8 | $module = $this->module; |
|
| 186 | 8 | $controller = $this->controller; |
|
| 187 | |||
| 188 | 8 | $cacheKey = "data.$module.$controller." . md5(http_build_query($params)); |
|
| 189 | |||
| 190 | 8 | $cacheTime = $this->getMeta()->getCache(); |
|
| 191 | |||
| 192 | 8 | if ($cacheTime && $cached = Cache::get($cacheKey)) { |
|
| 193 | $this->data = $cached; |
||
| 194 | return $cached; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @var \closure $controllerClosure |
||
| 199 | */ |
||
| 200 | 8 | $controllerClosure = include $this->getFile(); |
|
| 201 | |||
| 202 | 8 | if (!is_callable($controllerClosure)) { |
|
| 203 | throw new ControllerException("Controller is not callable '{$module}/{$controller}'"); |
||
| 204 | } |
||
| 205 | |||
| 206 | // process params |
||
| 207 | 8 | $params = $this->getMeta()->params($params); |
|
| 208 | |||
| 209 | // call Closure or Controller |
||
| 210 | 8 | $result = $controllerClosure(...$params); |
|
| 211 | |||
| 212 | // switch statement for result of Closure run |
||
| 213 | switch (true) { |
||
| 214 | 6 | case ($result === false): |
|
| 215 | // return "false" is equal to disable view and layout |
||
| 216 | $this->disableLayout(); |
||
| 217 | $this->disableView(); |
||
| 218 | break; |
||
| 219 | 6 | case is_string($result): |
|
| 220 | // return string variable is equal to change view template |
||
| 221 | $this->template = $result; |
||
| 222 | break; |
||
| 223 | 6 | case is_array($result): |
|
| 224 | // return associative array is equal to setup view data |
||
| 225 | 3 | $this->getData()->setFromArray($result); |
|
| 226 | 3 | break; |
|
| 227 | case ($result instanceof Controller): |
||
| 228 | $this->getData()->setFromArray($result->getData()->toArray()); |
||
| 229 | break; |
||
| 230 | } |
||
| 231 | |||
| 232 | 6 | if ($cacheTime) { |
|
| 233 | Cache::set( |
||
| 234 | $cacheKey, |
||
| 235 | $this->getData(), |
||
| 236 | $cacheTime, |
||
| 237 | ['system', 'data', Cache::prepare("$module.$controller")] |
||
| 238 | ); |
||
| 239 | } |
||
| 240 | |||
| 241 | 6 | return $this->getData(); |
|
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Setup controller file |
||
| 246 | * |
||
| 247 | * @return void |
||
| 248 | * @throws ControllerException |
||
| 249 | */ |
||
| 250 | 676 | protected function setFile() |
|
| 251 | { |
||
| 252 | 676 | $path = Application::getInstance()->getPath(); |
|
| 253 | 676 | $file = "$path/modules/{$this->module}/controllers/{$this->controller}.php"; |
|
| 254 | |||
| 255 | 676 | if (!file_exists($file)) { |
|
| 256 | 3 | throw new ControllerException("Controller file not found '{$this->module}/{$this->controller}'", 404); |
|
| 257 | } |
||
| 258 | |||
| 259 | 676 | $this->file = $file; |
|
| 260 | 676 | } |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Get controller file path |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | 676 | protected function getFile() // : string |
|
| 267 | { |
||
| 268 | 676 | if (!$this->file) { |
|
| 269 | 676 | $this->setFile(); |
|
| 270 | } |
||
| 271 | 676 | return $this->file; |
|
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Retrieve reflection for anonymous function |
||
| 276 | * @return void |
||
| 277 | * @throws \Bluz\Common\Exception\ComponentException |
||
| 278 | */ |
||
| 279 | 676 | protected function setMeta() |
|
| 280 | { |
||
| 281 | // cache for reflection data |
||
| 282 | 676 | $cacheKey = "meta.{$this->module}.{$this->controller}"; |
|
| 283 | |||
| 284 | 676 | if (!$meta = Cache::get($cacheKey)) { |
|
| 285 | 676 | $meta = new Meta($this->getFile()); |
|
| 286 | 676 | $meta->process(); |
|
| 287 | |||
| 288 | 676 | Cache::set( |
|
| 289 | $cacheKey, |
||
| 290 | $meta, |
||
| 291 | 676 | Cache::TTL_NO_EXPIRY, |
|
| 292 | 676 | ['system', 'meta', Cache::prepare($this->module . '.' . $this->controller)] |
|
| 293 | ); |
||
| 294 | } |
||
| 295 | 676 | $this->meta = $meta; |
|
| 296 | 676 | } |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Get meta information |
||
| 300 | * @return Meta |
||
| 301 | */ |
||
| 302 | 676 | public function getMeta() |
|
| 303 | { |
||
| 304 | 676 | if (!$this->meta) { |
|
| 305 | 676 | $this->setMeta(); |
|
| 306 | } |
||
| 307 | 676 | return $this->meta; |
|
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Assign key/value pair to Data object |
||
| 312 | * @param string $key |
||
| 313 | * @param mixed $value |
||
| 314 | * @return Controller |
||
| 315 | */ |
||
| 316 | public function assign($key, $value) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Get controller Data container |
||
| 324 | * |
||
| 325 | * @return Data |
||
| 326 | */ |
||
| 327 | 6 | public function getData() |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Specify data which should be serialized to JSON |
||
| 337 | * |
||
| 338 | * @return Data |
||
| 339 | */ |
||
| 340 | public function jsonSerialize() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Magic cast to string |
||
| 347 | * |
||
| 348 | * @return string |
||
| 349 | */ |
||
| 350 | 2 | public function __toString() |
|
| 351 | { |
||
| 386 | } |
||
| 387 |