Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Dispatcher 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 Dispatcher, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Comodojo\Dispatcher; |
||
| 38 | |||
| 39 | use TimestampTrait; |
||
| 40 | |||
| 41 | private $configuration = null; |
||
| 42 | |||
| 43 | private $request = null; |
||
| 44 | |||
| 45 | private $router = null; |
||
| 46 | |||
| 47 | private $response = null; |
||
| 48 | |||
| 49 | private $logger = null; |
||
| 50 | |||
| 51 | private $cache = null; |
||
| 52 | |||
| 53 | private $events = null; |
||
| 54 | |||
| 55 | public function __construct() { |
||
| 56 | |||
| 57 | ob_start(); |
||
| 58 | |||
| 59 | $this->setTimestamp(); |
||
| 60 | |||
| 61 | $this->configuration = new Configuration(); |
||
| 62 | |||
| 63 | $this->events = new Emitter(); |
||
| 64 | |||
| 65 | $this->cache = new CacheManager( CacheManager::PICK_FIRST ); |
||
| 66 | |||
| 67 | $this->request = new Request($this->configuration, $this->logger); |
||
| 68 | |||
| 69 | $this->router = new RouteCollector($this->configuration, $this->logger, $this->cache); |
||
| 70 | |||
| 71 | $this->response = new Response($this->configuration, $this->logger); |
||
| 72 | |||
| 73 | } |
||
| 74 | |||
| 75 | public function configuration() { |
||
| 76 | |||
| 77 | return $this->configuration; |
||
| 78 | |||
| 79 | } |
||
| 80 | |||
| 81 | public function events() { |
||
| 82 | |||
| 83 | return $this->events; |
||
| 84 | |||
| 85 | } |
||
| 86 | |||
| 87 | public function cache() { |
||
| 88 | |||
| 89 | return $this->cache; |
||
| 90 | |||
| 91 | } |
||
| 92 | |||
| 93 | public function request() { |
||
| 94 | |||
| 95 | return $this->request; |
||
| 96 | |||
| 97 | } |
||
| 98 | |||
| 99 | public function router() { |
||
| 100 | |||
| 101 | return $this->router; |
||
| 102 | |||
| 103 | } |
||
| 104 | |||
| 105 | public function response() { |
||
| 106 | |||
| 107 | return $this->response; |
||
| 108 | |||
| 109 | } |
||
| 110 | |||
| 111 | public function dispatch() { |
||
| 112 | |||
| 113 | $this->events->emit( new DispatcherEvent($this) ); |
||
| 114 | |||
| 115 | $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request') ); |
||
| 116 | |||
| 117 | $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request.'.$this->request->method()->get()) ); |
||
| 118 | |||
| 119 | $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request.#') ); |
||
| 120 | |||
| 121 | $this->router->route($this->request); |
||
| 122 | |||
| 123 | $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route') ); |
||
| 124 | |||
| 125 | $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.'.$this->router->route()->type) ); |
||
| 126 | |||
| 127 | $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.'.$this->router->route()->service) ); |
||
| 128 | |||
| 129 | $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.#') ); |
||
| 130 | |||
| 131 | // translate route to service |
||
| 132 | |||
| 133 | $this->router->compose($this->response); |
||
| 134 | |||
| 135 | $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response') ); |
||
| 136 | |||
| 137 | $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response.'.$this->response->status()->get()) ); |
||
| 138 | |||
| 139 | $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response.#') ); |
||
| 140 | |||
| 141 | $return = Processor::parse($this->configuration, $this->logger, $this->response); |
||
| 142 | |||
| 143 | ob_end_clean(); |
||
| 144 | |||
| 145 | return $return; |
||
| 146 | |||
| 147 | } |
||
| 148 | |||
| 149 | private function emitServiceSpecializedEvents($name) { |
||
| 150 | |||
| 151 | return new ServiceEvent( |
||
| 152 | $name, |
||
| 153 | $this->logger, |
||
| 154 | $this->request, |
||
| 155 | $this->router, |
||
| 156 | $this->response |
||
| 157 | ) ); |
||
|
|
|||
| 158 | |||
| 159 | } |
||
| 160 | |||
| 161 | } |
||
| 162 |