Total Complexity | 41 |
Total Lines | 266 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like OwnershipTransferService 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.
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 OwnershipTransferService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
53 | class OwnershipTransferService { |
||
54 | |||
55 | /** @var IEncryptionManager */ |
||
56 | private $encryptionManager; |
||
57 | |||
58 | /** @var IShareManager */ |
||
59 | private $shareManager; |
||
60 | |||
61 | /** @var IMountManager */ |
||
62 | private $mountManager; |
||
63 | |||
64 | public function __construct(IEncryptionManager $manager, |
||
65 | IShareManager $shareManager, |
||
66 | IMountManager $mountManager) { |
||
67 | $this->encryptionManager = $manager; |
||
68 | $this->shareManager = $shareManager; |
||
69 | $this->mountManager = $mountManager; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @param IUser $sourceUser |
||
74 | * @param IUser $destinationUser |
||
75 | * @param string $path |
||
76 | * |
||
77 | * @param OutputInterface|null $output |
||
78 | * @param bool $move |
||
79 | * @throws TransferOwnershipException |
||
80 | * @throws \OC\User\NoUserException |
||
81 | */ |
||
82 | public function transfer(IUser $sourceUser, |
||
83 | IUser $destinationUser, |
||
84 | string $path, |
||
85 | ?OutputInterface $output = null, |
||
86 | bool $move = false, |
||
87 | bool $firstLogin = false): void { |
||
88 | $output = $output ?? new NullOutput(); |
||
89 | $sourceUid = $sourceUser->getUID(); |
||
90 | $destinationUid = $destinationUser->getUID(); |
||
91 | $sourcePath = rtrim($sourceUid . '/files/' . $path, '/'); |
||
92 | |||
93 | // target user has to be ready |
||
94 | if ($destinationUser->getLastLogin() === 0 || !$this->encryptionManager->isReadyForUser($destinationUid)) { |
||
|
|||
95 | throw new TransferOwnershipException("The target user is not ready to accept files. The user has at least to have logged in once.", 2); |
||
96 | } |
||
97 | |||
98 | // setup filesystem |
||
99 | Filesystem::initMountPoints($sourceUid); |
||
100 | Filesystem::initMountPoints($destinationUid); |
||
101 | |||
102 | $view = new View(); |
||
103 | |||
104 | if ($move) { |
||
105 | $finalTarget = "$destinationUid/files/"; |
||
106 | } else { |
||
107 | $date = date('Y-m-d H-i-s'); |
||
108 | |||
109 | // Remove some characters which are prone to cause errors |
||
110 | $cleanUserName = str_replace(['\\', '/', ':', '.', '?', '#', '\'', '"'], '-', $sourceUser->getDisplayName()); |
||
111 | // Replace multiple dashes with one dash |
||
112 | $cleanUserName = preg_replace('/-{2,}/s', '-', $cleanUserName); |
||
113 | $cleanUserName = $cleanUserName ?: $sourceUid; |
||
114 | |||
115 | $finalTarget = "$destinationUid/files/transferred from $cleanUserName on $date"; |
||
116 | try { |
||
117 | $view->verifyPath(dirname($finalTarget), basename($finalTarget)); |
||
118 | } catch (InvalidPathException $e) { |
||
119 | $finalTarget = "$destinationUid/files/transferred from $sourceUid on $date"; |
||
120 | } |
||
121 | } |
||
122 | |||
123 | if (!($view->is_dir($sourcePath) || $view->is_file($sourcePath))) { |
||
124 | throw new TransferOwnershipException("Unknown path provided: $path", 1); |
||
125 | } |
||
126 | |||
127 | if ($move && ( |
||
128 | !$view->is_dir($finalTarget) || ( |
||
129 | !$firstLogin && |
||
130 | count($view->getDirectoryContent($finalTarget)) > 0 |
||
131 | ) |
||
132 | ) |
||
133 | ) { |
||
134 | throw new TransferOwnershipException("Destination path does not exists or is not empty", 1); |
||
135 | } |
||
136 | |||
137 | |||
138 | // analyse source folder |
||
139 | $this->analyse( |
||
140 | $sourceUid, |
||
141 | $destinationUid, |
||
142 | $sourcePath, |
||
143 | $view, |
||
144 | $output |
||
145 | ); |
||
146 | |||
147 | // collect all the shares |
||
148 | $shares = $this->collectUsersShares( |
||
149 | $sourceUid, |
||
150 | $output |
||
151 | ); |
||
152 | |||
153 | // transfer the files |
||
154 | $this->transferFiles( |
||
155 | $sourceUid, |
||
156 | $sourcePath, |
||
157 | $finalTarget, |
||
158 | $view, |
||
159 | $output |
||
160 | ); |
||
161 | |||
162 | // restore the shares |
||
163 | $this->restoreShares( |
||
164 | $sourceUid, |
||
165 | $destinationUid, |
||
166 | $shares, |
||
167 | $output |
||
168 | ); |
||
169 | } |
||
170 | |||
171 | private function walkFiles(View $view, $path, Closure $callBack) { |
||
172 | foreach ($view->getDirectoryContent($path) as $fileInfo) { |
||
173 | if (!$callBack($fileInfo)) { |
||
174 | return; |
||
175 | } |
||
176 | if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) { |
||
177 | $this->walkFiles($view, $fileInfo->getPath(), $callBack); |
||
178 | } |
||
179 | } |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * @param OutputInterface $output |
||
184 | * |
||
185 | * @throws \Exception |
||
186 | */ |
||
187 | protected function analyse(string $sourceUid, |
||
188 | string $destinationUid, |
||
189 | string $sourcePath, |
||
190 | View $view, |
||
191 | OutputInterface $output): void { |
||
192 | $output->writeln('Validating quota'); |
||
193 | $size = $view->getFileInfo($sourcePath, false)->getSize(false); |
||
194 | $freeSpace = $view->free_space($destinationUid . '/files/'); |
||
195 | if ($size > $freeSpace) { |
||
196 | $output->writeln('<error>Target user does not have enough free space available.</error>'); |
||
197 | throw new \Exception('Execution terminated.'); |
||
198 | } |
||
199 | |||
200 | $output->writeln("Analysing files of $sourceUid ..."); |
||
201 | $progress = new ProgressBar($output); |
||
202 | $progress->start(); |
||
203 | |||
204 | $encryptedFiles = []; |
||
205 | $this->walkFiles($view, $sourcePath, |
||
206 | function (FileInfo $fileInfo) use ($progress) { |
||
207 | if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) { |
||
208 | // only analyze into folders from main storage, |
||
209 | if (!$fileInfo->getStorage()->instanceOfStorage(IHomeStorage::class)) { |
||
210 | return false; |
||
211 | } |
||
212 | return true; |
||
213 | } |
||
214 | $progress->advance(); |
||
215 | if ($fileInfo->isEncrypted()) { |
||
216 | $encryptedFiles[] = $fileInfo; |
||
217 | } |
||
218 | return true; |
||
219 | }); |
||
220 | $progress->finish(); |
||
221 | $output->writeln(''); |
||
222 | |||
223 | // no file is allowed to be encrypted |
||
224 | if (!empty($encryptedFiles)) { |
||
225 | $output->writeln("<error>Some files are encrypted - please decrypt them first.</error>"); |
||
226 | foreach ($encryptedFiles as $encryptedFile) { |
||
227 | /** @var FileInfo $encryptedFile */ |
||
228 | $output->writeln(" " . $encryptedFile->getPath()); |
||
229 | } |
||
230 | throw new \Exception('Execution terminated.'); |
||
231 | } |
||
232 | } |
||
233 | |||
234 | private function collectUsersShares(string $sourceUid, |
||
235 | OutputInterface $output): array { |
||
236 | $output->writeln("Collecting all share information for files and folders of $sourceUid ..."); |
||
237 | |||
238 | $shares = []; |
||
239 | $progress = new ProgressBar($output); |
||
240 | foreach ([\OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_LINK, \OCP\Share::SHARE_TYPE_REMOTE, \OCP\Share::SHARE_TYPE_ROOM] as $shareType) { |
||
241 | $offset = 0; |
||
242 | while (true) { |
||
243 | $sharePage = $this->shareManager->getSharesBy($sourceUid, $shareType, null, true, 50, $offset); |
||
244 | $progress->advance(count($sharePage)); |
||
245 | if (empty($sharePage)) { |
||
246 | break; |
||
247 | } |
||
248 | $shares = array_merge($shares, $sharePage); |
||
249 | $offset += 50; |
||
250 | } |
||
251 | } |
||
252 | |||
253 | $progress->finish(); |
||
254 | $output->writeln(''); |
||
255 | return $shares; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * @throws TransferOwnershipException |
||
260 | */ |
||
261 | protected function transferFiles(string $sourceUid, |
||
262 | string $sourcePath, |
||
263 | string $finalTarget, |
||
264 | View $view, |
||
265 | OutputInterface $output): void { |
||
266 | $output->writeln("Transferring files to $finalTarget ..."); |
||
267 | |||
268 | // This change will help user to transfer the folder specified using --path option. |
||
269 | // Else only the content inside folder is transferred which is not correct. |
||
270 | if ($sourcePath !== "$sourceUid/files") { |
||
271 | $view->mkdir($finalTarget); |
||
272 | $finalTarget = $finalTarget . '/' . basename($sourcePath); |
||
273 | } |
||
274 | if ($view->rename($sourcePath, $finalTarget) === false) { |
||
275 | throw new TransferOwnershipException("Could not transfer files.", 1); |
||
276 | } |
||
277 | if (!is_dir("$sourceUid/files")) { |
||
278 | // because the files folder is moved away we need to recreate it |
||
279 | $view->mkdir("$sourceUid/files"); |
||
280 | } |
||
281 | } |
||
282 | |||
283 | private function restoreShares(string $sourceUid, |
||
319 | } |
||
320 | |||
321 | } |
||
322 |