Complex classes like BridgeFileSystem 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 BridgeFileSystem, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class BridgeFileSystem implements BridgeInterface |
||
21 | { |
||
22 | /** |
||
23 | * @var Filesystem |
||
24 | */ |
||
25 | private $fileSystemService; |
||
26 | |||
27 | /** |
||
28 | * @var CacheInterface |
||
29 | */ |
||
30 | private $cache; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | private $repositoryDir = ''; |
||
36 | |||
37 | /** |
||
38 | * @var string[] |
||
39 | */ |
||
40 | private $directorySeparators = ['.', ':', '-', '\\']; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | private $jobFileMap = []; |
||
46 | |||
47 | /** |
||
48 | * @var array |
||
49 | */ |
||
50 | private $groupedApps = []; |
||
51 | |||
52 | /** |
||
53 | * @param Filesystem $oFileSystemService |
||
54 | * @param CacheInterface $cache |
||
55 | * @param string $repositoryDir |
||
56 | */ |
||
57 | 9 | public function __construct( |
|
66 | |||
67 | /** |
||
68 | * @return JobEntityInterface[] |
||
69 | */ |
||
70 | 8 | public function getJobs() |
|
78 | |||
79 | /** |
||
80 | * @param ChronosJobEntity|JobEntityInterface $jobEntity |
||
81 | * @return bool |
||
82 | * @throws JobLoadException |
||
83 | */ |
||
84 | 2 | public function addJob(JobEntityInterface $jobEntity) |
|
96 | |||
97 | /** |
||
98 | * @param JobEntityInterface $jobEntity |
||
99 | * @return bool |
||
100 | */ |
||
101 | 3 | public function updateJob(JobEntityInterface $jobEntity) |
|
102 | { |
||
103 | 3 | if (in_array($jobEntity->getKey(), $this->groupedApps)) { |
|
104 | // marathon's group case where app belongs to a group file |
||
105 | 1 | return $this->dumpFileWithGroup( |
|
106 | 1 | $this->getJobFileFromMap($jobEntity->getKey()), |
|
107 | 1 | $jobEntity |
|
108 | ); |
||
109 | } |
||
110 | 2 | return $this->hasDumpFile( |
|
111 | 2 | $this->getJobFileFromMap($jobEntity->getKey()), |
|
112 | 2 | $jobEntity |
|
113 | ); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @param ChronosJobEntity|JobEntityInterface $jobEntity |
||
118 | * @return bool |
||
119 | */ |
||
120 | 3 | public function removeJob(JobEntityInterface $jobEntity) |
|
121 | { |
||
122 | 3 | if (in_array($jobEntity->getKey(), $this->groupedApps)) { |
|
123 | 1 | $jobFile = $this->getJobFileFromMap($jobEntity->getKey()); |
|
124 | 1 | $this->dumpFileWithGroup( |
|
125 | 1 | $jobFile, |
|
126 | 1 | $jobEntity, |
|
127 | 1 | false |
|
128 | ); |
||
129 | |||
130 | 1 | unset($this->jobFileMap[$jobEntity->getKey()]); |
|
131 | 1 | return true; |
|
132 | } |
||
133 | |||
134 | 2 | $jobFile = $this->getJobFileFromMap($jobEntity->getKey()); |
|
135 | 2 | $this->fileSystemService->remove($jobFile); |
|
136 | |||
137 | 2 | return $this->hasUnsetJobFileFromMap($jobEntity->getKey(), $jobFile); |
|
138 | } |
||
139 | |||
140 | /** |
||
141 | * @param JobEntityInterface $jobEntity |
||
142 | * @return string |
||
143 | */ |
||
144 | 2 | private function generateJobFilePath(JobEntityInterface $jobEntity) |
|
158 | |||
159 | /** |
||
160 | * @param string $path |
||
161 | * @param array $jobFiles |
||
162 | * @return array |
||
163 | */ |
||
164 | 8 | private function getJobFilesFromFileSystem($path, array &$jobFiles = []) |
|
182 | |||
183 | /** |
||
184 | * @param string $jobName |
||
185 | * @param string $jobFile |
||
186 | * @throws JobLoadException |
||
187 | */ |
||
188 | 7 | private function setJobFileToMap($jobName, $jobFile) |
|
200 | |||
201 | /** |
||
202 | * @param string $jobName |
||
203 | * @return string |
||
204 | * @throws \RuntimeException |
||
205 | */ |
||
206 | 4 | private function getJobFileFromMap($jobName) |
|
213 | |||
214 | /** |
||
215 | * @param string $jobName |
||
216 | * @param string $jobFile |
||
217 | * @return bool |
||
218 | * @throws \RuntimeException |
||
219 | */ |
||
220 | 2 | private function hasUnsetJobFileFromMap($jobName, $jobFile = '') |
|
231 | |||
232 | /** |
||
233 | * @param array $jobFiles |
||
234 | * @param bool $setToFileMap |
||
235 | * @return JobEntityInterface[] |
||
236 | * @throws JobLoadException |
||
237 | */ |
||
238 | 8 | private function loadJobsFromFileContent(array $jobFiles, $setToFileMap) |
|
239 | { |
||
240 | 8 | $jobs = []; |
|
241 | |||
242 | 8 | foreach ($jobFiles as $jobFilePath) { |
|
243 | 8 | $jobEntities = []; |
|
244 | // remove comment blocks |
||
245 | 8 | $temp = json_decode( |
|
246 | 8 | preg_replace( |
|
247 | 8 | '~\/\*(.*?)\*\/~mis', |
|
248 | 8 | '', |
|
249 | 8 | file_get_contents($jobFilePath) |
|
250 | ) |
||
251 | ); |
||
252 | |||
253 | 8 | if ($temp) { |
|
254 | // chronos |
||
255 | 7 | if (property_exists($temp, 'name')) { |
|
256 | 4 | $jobEntities[] = new ChronosJobEntity($temp); |
|
257 | } //marathon |
||
258 | 4 | elseif (property_exists($temp, 'id')) { |
|
259 | 4 | foreach ($this->getMarathonEntitiesForConfig($temp) as $app) { |
|
260 | 4 | $jobEntities[] = $app; |
|
261 | } |
||
262 | } else { |
||
263 | throw new JobLoadException( |
||
264 | 'Could not distinguish job as either chronos or marathon', |
||
265 | JobLoadException::ERROR_CODE_UNKNOWN_ENTITY_TYPE |
||
266 | ); |
||
267 | } |
||
268 | |||
269 | /** @var JobEntityInterface $jobEntity */ |
||
270 | 7 | foreach ($jobEntities as $jobEntity) { |
|
271 | 7 | if ($setToFileMap) { |
|
272 | // set path to job file map |
||
273 | 5 | $this->setJobFileToMap($jobEntity->getKey(), $jobFilePath); |
|
274 | } |
||
275 | |||
276 | 7 | $jobs[] = $jobEntity; |
|
277 | } |
||
278 | } else { |
||
279 | 1 | throw new JobLoadException( |
|
280 | 1 | sprintf('Unable to load json job data from "%s". Please check if the json is valid.', $jobFilePath), |
|
281 | 8 | JobLoadException::ERROR_CODE_NO_VALID_JSON |
|
282 | ); |
||
283 | } |
||
284 | } |
||
285 | |||
286 | 6 | return $jobs; |
|
287 | } |
||
288 | |||
289 | |||
290 | 4 | private function getMarathonEntitiesForConfig($entityData) |
|
305 | |||
306 | /** |
||
307 | * @param string $jobFile |
||
308 | * @param JobEntityInterface $jobEntity |
||
309 | * @return bool |
||
310 | */ |
||
311 | 2 | private function hasDumpFile($jobFile, JobEntityInterface $jobEntity) |
|
320 | |||
321 | /** |
||
322 | * @param string $jobFile |
||
323 | * @param JobEntityInterface $jobEntity |
||
324 | * @param bool $add |
||
325 | * @return bool |
||
326 | */ |
||
327 | 2 | private function dumpFileWithGroup($jobFile, JobEntityInterface $jobEntity, $add = true) |
|
383 | } |
||
384 |