Complex classes like Module 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 Module, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class Module implements PdfInterface, ResolverInterface, ServiceManagerAwareInterface |
||
| 32 | { |
||
| 33 | const RENDER_FULL = 0; |
||
| 34 | const RENDER_WITHOUT_PDF = 1; |
||
| 35 | const RENDER_WITHOUT_ATTACHMENTS = 2; |
||
| 36 | |||
| 37 | protected $serviceManager; |
||
| 38 | |||
| 39 | protected $viewResolverAttached = false; |
||
| 40 | |||
| 41 | protected $appendPDF = array(); |
||
| 42 | protected $appendImage = array(); |
||
| 43 | |||
| 44 | |||
| 45 | /** |
||
| 46 | * Loads module specific configuration. |
||
| 47 | * |
||
| 48 | * @return array |
||
| 49 | */ |
||
| 50 | public function getConfig() |
||
| 54 | |||
| 55 | public function setServiceManager(ServiceManager $serviceManager) |
||
| 56 | { |
||
| 57 | $this->serviceManager = $serviceManager; |
||
| 58 | return $this; |
||
| 59 | } |
||
| 60 | |||
| 61 | public function onBootstrap(MvcEvent $e) |
||
| 62 | { |
||
| 63 | $eventManager = $e->getApplication()->getEventManager(); |
||
| 64 | $eventManager->getSharedManager()->attach( |
||
| 65 | 'Applications', |
||
| 66 | 'application.detail.actionbuttons', |
||
| 67 | function ($event) { |
||
|
|
|||
| 68 | return 'pdf/application/details/button'; |
||
| 69 | } |
||
| 70 | ); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * hook into the rendering for transformation of HTML to PDF |
||
| 75 | * @param \Zend\EventManager\EventManagerInterface $events |
||
| 76 | */ |
||
| 77 | public function attach(EventManagerInterface $events) |
||
| 78 | { |
||
| 79 | $events->attach(ViewEvent::EVENT_RENDERER_POST, array($this, 'cleanLayout'), 1); |
||
| 80 | $events->attach(ViewEvent::EVENT_RESPONSE, array($this, 'attachPDFtransformer'), 10); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * hook into the MVC |
||
| 85 | * in here you could still decide, if you want to hook into the Rendering |
||
| 86 | * @param \Zend\EventManager\EventManagerInterface $events |
||
| 87 | */ |
||
| 88 | public function attachMvc(EventManagerInterface $events) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * hook into the Rendering of files |
||
| 95 | * the manager to hook in is the viewhelper 'insertfiles' |
||
| 96 | * |
||
| 97 | * @param \Zend\Mvc\MvcEvent $e |
||
| 98 | */ |
||
| 99 | public function initializeViewHelper(MvcEvent $e) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * proxy, in case that you just got a name and have to find the associated file-entity |
||
| 112 | * maybe this is redundant and can be deprecated |
||
| 113 | * |
||
| 114 | * @param \Core\View\Helper\InsertFile\FileEvent $e |
||
| 115 | * @return null |
||
| 116 | */ |
||
| 117 | public function getFile(FileEvent $e) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * here the inserted File is rendered, |
||
| 136 | * there is a lot which still can be done like outsorcing the HTML to a template, |
||
| 137 | * or distinguish between different File Types, |
||
| 138 | * at the moment we assume the $file is always an (sub-)instance of \Core\File\Entity |
||
| 139 | * |
||
| 140 | * @param \Core\View\Helper\InsertFile\FileEvent $e |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | public function renderFile(FileEvent $e) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * give a summary of all inserted Files, |
||
| 166 | * this is for having access to those files in the post-process |
||
| 167 | * @param \Core\View\Helper\InsertFile\FileEvent|\Zend\View\ViewEvent $e |
||
| 168 | * @return NULL |
||
| 169 | */ |
||
| 170 | public function collectFiles(FileEvent $e) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * remove unwanted or layout related data |
||
| 189 | * |
||
| 190 | * basically you rake through the viewmodel for the data you want to use for your template, |
||
| 191 | * this may not be optimal because you have to rely on the correct naming of the viewmodels |
||
| 192 | * |
||
| 193 | * if you get the data you want, you switch to the specific template by adding the conforming resolver |
||
| 194 | * |
||
| 195 | * @param \Zend\View\ViewEvent $e |
||
| 196 | */ |
||
| 197 | public function cleanLayout(ViewEvent $e) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Attach an own ViewResolver |
||
| 222 | */ |
||
| 223 | public function attachViewResolver() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Transform the HTML to PDF, |
||
| 234 | * this is a post-rendering-process |
||
| 235 | * |
||
| 236 | * put in here everything related to the transforming-process like options |
||
| 237 | * |
||
| 238 | * @param \Zend\View\ViewEvent $e |
||
| 239 | */ |
||
| 240 | public function attachPDFtransformer(ViewEvent $e) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Look for a template with the Suffix ".pdf.phtml" |
||
| 313 | * |
||
| 314 | * @param string $name |
||
| 315 | * @param \Zend\View\Renderer\RendererInterface $renderer |
||
| 316 | * @return string|boolean |
||
| 317 | */ |
||
| 318 | public function resolve($name, Renderer $renderer = null) |
||
| 355 | } |
||
| 356 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.