Complex classes like Manager 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 Manager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Manager |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var array Contains configurations and presets |
||
| 20 | */ |
||
| 21 | protected $options; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var MethodCaller |
||
| 25 | */ |
||
| 26 | protected $methodCaller; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $retinaRegex = '/(.*)@2x\\.(jpe?g|png|webp|gif)/'; |
||
| 32 | |||
| 33 | 153 | public function __construct($options) |
|
| 37 | |||
| 38 | /** |
||
| 39 | * @return MethodCaller |
||
| 40 | */ |
||
| 41 | 54 | public function getMethodCaller() |
|
| 49 | |||
| 50 | /** |
||
| 51 | * @param MethodCaller $methodCaller |
||
| 52 | */ |
||
| 53 | 3 | public function setMethodCaller(MethodCaller $methodCaller) |
|
| 57 | |||
| 58 | 12 | public function localUrl($preset, $file) { |
|
| 61 | |||
| 62 | 3 | public function url($preset, $file) |
|
| 66 | |||
| 67 | 3 | public function imageUrl($file) |
|
| 71 | |||
| 72 | 36 | public function isRetina($file) |
|
| 76 | |||
| 77 | 39 | public function getOriginalFilename($file) { |
|
| 82 | |||
| 83 | 36 | protected function getPresetActions($preset_key, $file) |
|
| 110 | |||
| 111 | 27 | protected function generateRetinaAction($action) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * The path to the original file |
||
| 124 | * @param $source_file |
||
| 125 | * @return string |
||
| 126 | * @throws Exceptions\NotFoundException |
||
| 127 | */ |
||
| 128 | 18 | public function getOriginalFile($source_file) |
|
| 129 | { |
||
| 130 | 18 | $original_file = $this->options['path_local'] . '/' . $source_file; |
|
| 131 | 18 | if (!is_file($original_file)) { |
|
| 132 | 6 | throw new Exceptions\NotFoundException('File not found'); |
|
| 133 | } |
||
| 134 | |||
| 135 | 12 | return $original_file; |
|
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Take a preset and a file and return a transformed image |
||
| 140 | * |
||
| 141 | * @param $preset_key string |
||
| 142 | * @param $file string |
||
| 143 | * @throws Exceptions\InvalidPresetException |
||
| 144 | * @throws Exceptions\NotFoundException |
||
| 145 | * @throws \RuntimeException |
||
| 146 | * @return string |
||
| 147 | */ |
||
| 148 | 42 | public function handleRequest($preset_key, $file) |
|
| 149 | { |
||
| 150 | //do it at the beginning for early validation |
||
| 151 | 42 | $preset = $this->getPresetActions($preset_key, $file); |
|
| 152 | |||
| 153 | 36 | $source_file = $this->getOriginalFilename($file); |
|
| 154 | |||
| 155 | 36 | $original_file = $this->getOriginalFile($source_file); |
|
| 156 | |||
| 157 | 27 | $final_file = $this->localUrl($preset_key, $file); |
|
| 158 | |||
| 159 | 27 | $this->verifyDirectoryExistence($this->options['path_local'], dirname($final_file)); |
|
| 160 | |||
| 161 | 27 | $final_file = $this->options['path_local'] . '/' . $final_file; |
|
| 162 | |||
| 163 | 27 | if (file_exists($final_file)) { |
|
| 164 | 6 | return $final_file; |
|
| 165 | } |
||
| 166 | |||
| 167 | 24 | $image = $this->loadImage($original_file); |
|
| 168 | |||
| 169 | 15 | return $this->buildImage($preset, $image, $final_file)->source; |
|
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Create the folder containing the cached images if it doesn't exist |
||
| 174 | * |
||
| 175 | * @param $base |
||
| 176 | * @param $cacheDir |
||
| 177 | */ |
||
| 178 | 15 | protected function verifyDirectoryExistence($base, $cacheDir) |
|
| 179 | { |
||
| 180 | 15 | if (is_dir("$base/$cacheDir")) { |
|
| 181 | 3 | return; |
|
| 182 | } |
||
| 183 | |||
| 184 | 15 | $folder_path = explode('/', $cacheDir); |
|
| 185 | 15 | foreach ($folder_path as $element) { |
|
| 186 | 15 | $base .= '/' . $element; |
|
| 187 | 15 | if (!is_dir($base)) { |
|
| 188 | 15 | mkdir($base, 0755, true); |
|
| 189 | 15 | chmod($base, 0755); |
|
| 190 | 5 | } |
|
| 191 | 5 | } |
|
| 192 | 15 | } |
|
| 193 | |||
| 194 | 15 | protected function loadImage($src) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Create a new image based on an image preset. |
||
| 201 | * |
||
| 202 | * @param array $actions An image preset array. |
||
| 203 | * @param Image $image Path of the source file. |
||
| 204 | * @param string $dst Path of the destination file. |
||
| 205 | * @throws \RuntimeException |
||
| 206 | * @return Image |
||
| 207 | */ |
||
| 208 | 78 | protected function buildImage($actions, Image $image, $dst) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Accept a percentage and return it in pixels. |
||
| 237 | * |
||
| 238 | * @param string $value |
||
| 239 | * @param int $current_pixels |
||
| 240 | * @return mixed |
||
| 241 | */ |
||
| 242 | 51 | public function percent($value, $current_pixels) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Accept a keyword (center, top, left, etc) and return it as an offset in pixels. |
||
| 253 | * |
||
| 254 | * @param $value |
||
| 255 | * @param $current_pixels |
||
| 256 | * @param $new_pixels |
||
| 257 | * @return float|int |
||
| 258 | */ |
||
| 259 | 27 | public function keywords($value, $current_pixels, $new_pixels) |
|
| 277 | } |
||
| 278 |