Complex classes like PersistenceService 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 PersistenceService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
54 | class PersistenceService implements PersistenceServiceInterface |
||
55 | { |
||
56 | /** |
||
57 | * Persistence adapter factory |
||
58 | * |
||
59 | * @var PersistenceAdapterFactoryInterface |
||
60 | */ |
||
61 | protected $persistenceAdapterFactory; |
||
62 | /** |
||
63 | * Webservers scheduled for reload |
||
64 | * |
||
65 | * @var array |
||
66 | */ |
||
67 | protected $reloadWebserver = []; |
||
68 | /** |
||
69 | * PHP-Restart flag |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $reloadPhp = []; |
||
74 | /** |
||
75 | * Service service |
||
76 | * |
||
77 | * @var ServiceServiceInterface |
||
78 | */ |
||
79 | protected $serviceService; |
||
80 | |||
81 | /** |
||
82 | * Constructor |
||
83 | * |
||
84 | * @param PersistenceAdapterFactoryInterface $persistenceAdapterFactory Persistence adapter factory |
||
85 | * @param ServiceServiceInterface $serviceService Service service |
||
86 | */ |
||
87 | public function __construct( |
||
94 | |||
95 | /** |
||
96 | * Create an account |
||
97 | * |
||
98 | * @param AccountInterface $account Account |
||
99 | * @return void |
||
100 | */ |
||
101 | public function createAccount(AccountInterface $account) |
||
104 | |||
105 | /** |
||
106 | * Delete an account |
||
107 | * |
||
108 | * @param AccountInterface $account Account |
||
109 | * @return void |
||
110 | */ |
||
111 | public function deleteAccount(AccountInterface $account) |
||
119 | |||
120 | /** |
||
121 | * Enable an account |
||
122 | * |
||
123 | * @param AccountInterface $account Account |
||
124 | * @return void |
||
125 | */ |
||
126 | public function enableAccount(AccountInterface $account) |
||
134 | |||
135 | /** |
||
136 | * Disable an account |
||
137 | * |
||
138 | * @param AccountInterface $account Account |
||
139 | * @return void |
||
140 | */ |
||
141 | public function disableAccount(AccountInterface $account) |
||
149 | |||
150 | /** |
||
151 | * Rename an account |
||
152 | * |
||
153 | * @param AccountInterface $account Account |
||
154 | * @return void |
||
155 | */ |
||
156 | public function renameAccount(AccountInterface $account) |
||
161 | |||
162 | /** |
||
163 | * Create a virtual host |
||
164 | * |
||
165 | * @param AccountInterface $account Account |
||
166 | * @param VhostInterface $vhost Virtual host |
||
167 | * @return void |
||
168 | */ |
||
169 | public function createVhost(AccountInterface $account, VhostInterface $vhost) |
||
186 | |||
187 | /** |
||
188 | * Delete a virtual host |
||
189 | * |
||
190 | * @param AccountInterface $account Account |
||
191 | * @param VhostInterface $vhost Virtual host |
||
192 | * @return void |
||
193 | */ |
||
194 | public function deleteVhost(AccountInterface $account, VhostInterface $vhost) |
||
205 | |||
206 | /** |
||
207 | * Enable a virtual host |
||
208 | * |
||
209 | * @param AccountInterface $account Account |
||
210 | * @param VhostInterface $vhost Virtual host |
||
211 | * @return void |
||
212 | * @throws \RuntimeException If the virtual host directory doesn't exist yet |
||
213 | * @throws \RuntimeException If the virtual host is already enabled but cannot be renewed |
||
214 | */ |
||
215 | public function enableVhost(AccountInterface $account, VhostInterface $vhost) |
||
253 | |||
254 | /** |
||
255 | * Disable a virtual host |
||
256 | * |
||
257 | * @param AccountInterface $account Account |
||
258 | * @param VhostInterface $vhost Virtual host |
||
259 | * @return void |
||
260 | */ |
||
261 | public function disableVhost(AccountInterface $account, VhostInterface $vhost) |
||
279 | |||
280 | /** |
||
281 | * Redirect a virtual host |
||
282 | * |
||
283 | * @param AccountInterface $account Account |
||
284 | * @param VhostInterface $vhost Virtual host |
||
285 | * @return void |
||
286 | */ |
||
287 | public function redirectVhost(AccountInterface $account, VhostInterface $vhost) |
||
297 | |||
298 | /** |
||
299 | * Configure the PHP version of a virtual host |
||
300 | * |
||
301 | * @param AccountInterface $account Account |
||
302 | * @param VhostInterface $vhost Virtual host |
||
303 | * @param string|null $oldPhpVersion Old PHP version |
||
304 | * @return void |
||
305 | * @throws \RuntimeException If the previous PHP configuration cannot be removed |
||
306 | */ |
||
307 | public function phpVhost(AccountInterface $account, VhostInterface $vhost, $oldPhpVersion = null) |
||
345 | |||
346 | /** |
||
347 | * Configure a protocol port for a virtual host |
||
348 | * |
||
349 | * @param AccountInterface $account Account |
||
350 | * @param VhostInterface $vhost Virtual host |
||
351 | * @return void |
||
352 | */ |
||
353 | public function portVhost(AccountInterface $account, VhostInterface $vhost) |
||
377 | |||
378 | /** |
||
379 | * Add a secondary domain to a virtual host |
||
380 | * |
||
381 | * @param AccountInterface $account Account |
||
382 | * @param VhostInterface $vhost Virtual host |
||
383 | * @return void |
||
384 | */ |
||
385 | public function domainVhost(AccountInterface $account, VhostInterface $vhost) |
||
395 | |||
396 | /** |
||
397 | * Certifiy a virtual host |
||
398 | * |
||
399 | * @param AccountInterface $account Account |
||
400 | * @param VhostInterface $vhost Virtual host |
||
401 | * @return void |
||
402 | */ |
||
403 | public function certifyVhost(AccountInterface $account, VhostInterface $vhost) |
||
416 | |||
417 | /** |
||
418 | * Prepare a directory |
||
419 | * |
||
420 | * @param string $directory Directory |
||
421 | * @param string $user Owner user |
||
422 | * @param string $group Owner group |
||
423 | * @return string Prepared directory |
||
424 | */ |
||
425 | protected function prepareDirectory($directory, $user, $group) |
||
432 | |||
433 | /** |
||
434 | * Recursively delete a directory |
||
435 | * |
||
436 | * @param string $directory Directory |
||
437 | * @param string $user Owner user |
||
438 | * @param string $group Owner group |
||
439 | * @throws \RuntimeException If a file or directory cannot be deleted |
||
440 | */ |
||
441 | protected function deleteDirectory($directory, $user, $group) |
||
465 | |||
466 | /** |
||
467 | * Persist a virtual host |
||
468 | * |
||
469 | * @param AccountInterface $account Account |
||
470 | * @param VhostInterface $vhost Virtual host |
||
471 | */ |
||
472 | protected function persistVhost(AccountInterface $account, VhostInterface $vhost) |
||
479 | } |
||
480 |