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) |
||
112 | { |
||
113 | // Delete all virtual hosts |
||
114 | /** @var VhostInterface $vhost */ |
||
115 | foreach ($account->getVhosts() as $vhost) { |
||
116 | $this->deleteVhost($account, $vhost); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Enable an account |
||
122 | * |
||
123 | * @param AccountInterface $account Account |
||
124 | * @return void |
||
125 | */ |
||
126 | public function enableAccount(AccountInterface $account) |
||
127 | { |
||
128 | // Enable all virtual hosts |
||
129 | /** @var VhostInterface $vhost */ |
||
130 | foreach ($account->getVhosts() as $vhost) { |
||
131 | $this->enableVhost($account, $vhost); |
||
132 | } |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Disable an account |
||
137 | * |
||
138 | * @param AccountInterface $account Account |
||
139 | * @return void |
||
140 | */ |
||
141 | public function disableAccount(AccountInterface $account) |
||
142 | { |
||
143 | // Disable all virtual hosts |
||
144 | /** @var VhostInterface $vhost */ |
||
145 | foreach ($account->getVhosts() as $vhost) { |
||
146 | $this->disableVhost($account, $vhost); |
||
147 | } |
||
148 | } |
||
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) |
||
195 | { |
||
196 | // Disable the virtual host first |
||
197 | $this->disableVhost($account, $vhost); |
||
198 | |||
199 | $accountHelper = new AccountHelper($account); |
||
200 | $availableVhost = $accountHelper->vhostDirectory($vhost, false); |
||
201 | if (is_dir($availableVhost)) { |
||
202 | $this->deleteDirectory($availableVhost, $account->getName(), App::getConfig('general.group')); |
||
203 | } |
||
204 | } |
||
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) |
||
216 | { |
||
217 | // If both the account and the virtual host are active: Enable the virtual host |
||
218 | if ($account->isActive() && $vhost->isActive()) { |
||
219 | $accountHelper = new AccountHelper($account); |
||
220 | $availableVhost = $accountHelper->vhostDirectory($vhost, false); |
||
221 | |||
222 | // If the virtual host directory doesn't exist yet: Error |
||
223 | if (!is_dir($availableVhost)) { |
||
224 | throw new \RuntimeException( |
||
225 | sprintf('Virtual host "%s" isn\'t persisted yet', strval($vhost->getPrimaryDomain())), |
||
226 | 1476015113 |
||
227 | ); |
||
228 | } |
||
229 | |||
230 | $enabledVhost = $accountHelper->vhostDirectory($vhost, true); |
||
231 | $this->prepareDirectory(dirname($enabledVhost), $account->getName(), App::getConfig('general.group')); |
||
232 | |||
233 | // If the virtual host is already enabled but cannot be renewed |
||
234 | if (file_exists($enabledVhost) && !unlink($enabledVhost)) { |
||
235 | throw new \RuntimeException( |
||
236 | sprintf( |
||
237 | 'Virtual host "%s" is already enabled but cannot be renewed', |
||
238 | strval($vhost->getPrimaryDomain()) |
||
239 | ), |
||
240 | 1476016371 |
||
241 | ); |
||
242 | } |
||
243 | |||
244 | $relativeEnabledVhost = '..'.substr($availableVhost, strlen($accountHelper->directory('config'))); |
||
245 | symlink($relativeEnabledVhost, $enabledVhost); |
||
246 | |||
247 | // Reload apache |
||
248 | $this->serviceService->reloadWebserver($vhost->getType()); |
||
249 | } |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Disable a virtual host |
||
254 | * |
||
255 | * @param AccountInterface $account Account |
||
256 | * @param VhostInterface $vhost Virtual host |
||
257 | * @return void |
||
258 | */ |
||
259 | public function disableVhost(AccountInterface $account, VhostInterface $vhost) |
||
275 | |||
276 | /** |
||
277 | * Redirect a virtual host |
||
278 | * |
||
279 | * @param AccountInterface $account Account |
||
280 | * @param VhostInterface $vhost Virtual host |
||
281 | * @return void |
||
282 | */ |
||
283 | public function redirectVhost(AccountInterface $account, VhostInterface $vhost) |
||
291 | |||
292 | /** |
||
293 | * Configure the PHP version of a virtual host |
||
294 | * |
||
295 | * @param AccountInterface $account Account |
||
296 | * @param VhostInterface $vhost Virtual host |
||
297 | * @param string|null $oldPhpVersion Old PHP version |
||
298 | * @return void |
||
299 | * @throws \RuntimeException If the previous PHP configuration cannot be removed |
||
300 | */ |
||
301 | public function phpVhost(AccountInterface $account, VhostInterface $vhost, $oldPhpVersion = null) |
||
337 | |||
338 | /** |
||
339 | * Configure a protocol based port for a virtual host |
||
340 | * |
||
341 | * @param AccountInterface $account Account |
||
342 | * @param VhostInterface $vhost Virtual host |
||
343 | * @return void |
||
344 | */ |
||
345 | public function portVhost(AccountInterface $account, VhostInterface $vhost) |
||
367 | |||
368 | /** |
||
369 | * Add a secondary domain to a virtual host |
||
370 | * |
||
371 | * @param AccountInterface $account Account |
||
372 | * @param VhostInterface $vhost Virtual host |
||
373 | * @return void |
||
374 | */ |
||
375 | public function domainVhost(AccountInterface $account, VhostInterface $vhost) |
||
383 | |||
384 | /** |
||
385 | * Certifiy a virtual host |
||
386 | * |
||
387 | * @param AccountInterface $account Account |
||
388 | * @param VhostInterface $vhost Virtual host |
||
389 | * @return void |
||
390 | */ |
||
391 | public function certifyVhost(AccountInterface $account, VhostInterface $vhost) { |
||
401 | |||
402 | /** |
||
403 | * Prepare a directory |
||
404 | * |
||
405 | * @param string $directory Directory |
||
406 | * @param string $user Owner user |
||
407 | * @param string $group Owner group |
||
408 | * @return string Prepared directory |
||
409 | */ |
||
410 | protected function prepareDirectory($directory, $user, $group) |
||
417 | |||
418 | /** |
||
419 | * Recursively delete a directory |
||
420 | * |
||
421 | * @param string $directory Directory |
||
422 | * @param string $user Owner user |
||
423 | * @param string $group Owner group |
||
424 | * @throws \RuntimeException If a file or directory cannot be deleted |
||
425 | */ |
||
426 | protected function deleteDirectory($directory, $user, $group) |
||
450 | |||
451 | /** |
||
452 | * Persist a virtual host |
||
453 | * |
||
454 | * @param AccountInterface $account Account |
||
455 | * @param VhostInterface $vhost Virtual host |
||
456 | */ |
||
457 | protected function persistVhost(AccountInterface $account, VhostInterface $vhost) |
||
464 | } |
||
465 |