| Total Complexity | 42 |
| Total Lines | 247 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
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.
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 |
||
| 53 | class Manager implements IManager { |
||
| 54 | private const TOKEN_CLEANUP_TIME = 12 * 60 * 60 ; |
||
| 55 | |||
| 56 | public const TABLE_TOKENS = 'direct_edit'; |
||
| 57 | |||
| 58 | /** @var IEditor[] */ |
||
| 59 | private $editors = []; |
||
| 60 | /** @var IDBConnection */ |
||
| 61 | private $connection; |
||
| 62 | /** @var ISecureRandom */ |
||
| 63 | private $random; |
||
| 64 | /** @var string|null */ |
||
| 65 | private $userId; |
||
| 66 | /** @var IRootFolder */ |
||
| 67 | private $rootFolder; |
||
| 68 | /** @var IL10N */ |
||
| 69 | private $l10n; |
||
| 70 | /** @var EncryptionManager */ |
||
| 71 | private $encryptionManager; |
||
| 72 | |||
| 73 | public function __construct( |
||
| 74 | ISecureRandom $random, |
||
| 75 | IDBConnection $connection, |
||
| 76 | IUserSession $userSession, |
||
| 77 | IRootFolder $rootFolder, |
||
| 78 | IFactory $l10nFactory, |
||
| 79 | EncryptionManager $encryptionManager |
||
| 80 | ) { |
||
| 81 | $this->random = $random; |
||
| 82 | $this->connection = $connection; |
||
| 83 | $this->userId = $userSession->getUser() ? $userSession->getUser()->getUID() : null; |
||
| 84 | $this->rootFolder = $rootFolder; |
||
| 85 | $this->l10n = $l10nFactory->get('core'); |
||
| 86 | $this->encryptionManager = $encryptionManager; |
||
| 87 | } |
||
| 88 | |||
| 89 | public function registerDirectEditor(IEditor $directEditor): void { |
||
| 90 | $this->editors[$directEditor->getId()] = $directEditor; |
||
| 91 | } |
||
| 92 | |||
| 93 | public function getEditors(): array { |
||
| 94 | return $this->editors; |
||
| 95 | } |
||
| 96 | |||
| 97 | public function getTemplates(string $editor, string $type): array { |
||
| 98 | if (!array_key_exists($editor, $this->editors)) { |
||
| 99 | throw new \RuntimeException('No matching editor found'); |
||
| 100 | } |
||
| 101 | $templates = []; |
||
| 102 | foreach ($this->editors[$editor]->getCreators() as $creator) { |
||
| 103 | if ($creator->getId() === $type) { |
||
| 104 | $templates = [ |
||
| 105 | 'empty' => [ |
||
| 106 | 'id' => 'empty', |
||
| 107 | 'title' => $this->l10n->t('Empty file'), |
||
| 108 | 'preview' => null |
||
| 109 | ] |
||
| 110 | ]; |
||
| 111 | |||
| 112 | if ($creator instanceof ACreateFromTemplate) { |
||
| 113 | $templates = $creator->getTemplates(); |
||
| 114 | } |
||
| 115 | |||
| 116 | $templates = array_map(function ($template) use ($creator) { |
||
| 117 | $template['extension'] = $creator->getExtension(); |
||
| 118 | $template['mimetype'] = $creator->getMimetype(); |
||
| 119 | return $template; |
||
| 120 | }, $templates); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | $return = []; |
||
| 124 | $return['templates'] = $templates; |
||
| 125 | return $return; |
||
| 126 | } |
||
| 127 | |||
| 128 | public function create(string $path, string $editorId, string $creatorId, $templateId = null): string { |
||
| 129 | $userFolder = $this->rootFolder->getUserFolder($this->userId); |
||
| 130 | if ($userFolder->nodeExists($path)) { |
||
| 131 | throw new \RuntimeException('File already exists'); |
||
| 132 | } else { |
||
| 133 | $file = $userFolder->newFile($path); |
||
| 134 | $editor = $this->getEditor($editorId); |
||
| 135 | $creators = $editor->getCreators(); |
||
| 136 | foreach ($creators as $creator) { |
||
| 137 | if ($creator->getId() === $creatorId) { |
||
| 138 | $creator->create($file, $creatorId, $templateId); |
||
| 139 | return $this->createToken($editorId, $file, $path); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | throw new \RuntimeException('No creator found'); |
||
| 145 | } |
||
| 146 | |||
| 147 | public function open(string $filePath, string $editorId = null): string { |
||
| 148 | /** @var File $file */ |
||
| 149 | $file = $this->rootFolder->getUserFolder($this->userId)->get($filePath); |
||
| 150 | |||
| 151 | if ($editorId === null) { |
||
| 152 | $editorId = $this->findEditorForFile($file); |
||
| 153 | } |
||
| 154 | if (!array_key_exists($editorId, $this->editors)) { |
||
| 155 | throw new \RuntimeException("Editor $editorId is unknown"); |
||
| 156 | } |
||
| 157 | |||
| 158 | return $this->createToken($editorId, $file, $filePath); |
||
| 159 | } |
||
| 160 | |||
| 161 | private function findEditorForFile(File $file) { |
||
| 162 | foreach ($this->editors as $editor) { |
||
| 163 | if (in_array($file->getMimeType(), $editor->getMimetypes())) { |
||
| 164 | return $editor->getId(); |
||
| 165 | } |
||
| 166 | } |
||
| 167 | throw new \RuntimeException('No default editor found for files mimetype'); |
||
| 168 | } |
||
| 169 | |||
| 170 | public function edit(string $token): Response { |
||
| 171 | try { |
||
| 172 | /** @var IEditor $editor */ |
||
| 173 | $tokenObject = $this->getToken($token); |
||
| 174 | if ($tokenObject->hasBeenAccessed()) { |
||
| 175 | throw new \RuntimeException('Token has already been used and can only be used for followup requests'); |
||
| 176 | } |
||
| 177 | $editor = $this->getEditor($tokenObject->getEditor()); |
||
| 178 | $this->accessToken($token); |
||
| 179 | } catch (Throwable $throwable) { |
||
| 180 | $this->invalidateToken($token); |
||
| 181 | return new NotFoundResponse(); |
||
| 182 | } |
||
| 183 | return $editor->open($tokenObject); |
||
| 184 | } |
||
| 185 | |||
| 186 | public function editSecure(File $file, string $editorId): TemplateResponse { |
||
| 187 | // TODO: Implementation in follow up |
||
| 188 | } |
||
| 189 | |||
| 190 | private function getEditor($editorId): IEditor { |
||
| 191 | if (!array_key_exists($editorId, $this->editors)) { |
||
| 192 | throw new \RuntimeException('No editor found'); |
||
| 193 | } |
||
| 194 | return $this->editors[$editorId]; |
||
| 195 | } |
||
| 196 | |||
| 197 | public function getToken(string $token): IToken { |
||
| 198 | $query = $this->connection->getQueryBuilder(); |
||
| 199 | $query->select('*')->from(self::TABLE_TOKENS) |
||
| 200 | ->where($query->expr()->eq('token', $query->createNamedParameter($token, IQueryBuilder::PARAM_STR))); |
||
| 201 | $result = $query->execute(); |
||
| 202 | if ($tokenRow = $result->fetch(FetchMode::ASSOCIATIVE)) { |
||
| 203 | return new Token($this, $tokenRow); |
||
| 204 | } |
||
| 205 | throw new \RuntimeException('Failed to validate the token'); |
||
| 206 | } |
||
| 207 | |||
| 208 | public function cleanup(): int { |
||
| 213 | } |
||
| 214 | |||
| 215 | public function refreshToken(string $token): bool { |
||
| 216 | $query = $this->connection->getQueryBuilder(); |
||
| 217 | $query->update(self::TABLE_TOKENS) |
||
| 218 | ->set('timestamp', $query->createNamedParameter(time(), IQueryBuilder::PARAM_INT)) |
||
| 219 | ->where($query->expr()->eq('token', $query->createNamedParameter($token, IQueryBuilder::PARAM_STR))); |
||
| 220 | $result = $query->execute(); |
||
| 221 | return $result !== 0; |
||
| 222 | } |
||
| 223 | |||
| 224 | |||
| 225 | public function invalidateToken(string $token): bool { |
||
| 226 | $query = $this->connection->getQueryBuilder(); |
||
| 227 | $query->delete(self::TABLE_TOKENS) |
||
| 228 | ->where($query->expr()->eq('token', $query->createNamedParameter($token, IQueryBuilder::PARAM_STR))); |
||
| 229 | $result = $query->execute(); |
||
| 230 | return $result !== 0; |
||
| 231 | } |
||
| 232 | |||
| 233 | public function accessToken(string $token): bool { |
||
| 234 | $query = $this->connection->getQueryBuilder(); |
||
| 235 | $query->update(self::TABLE_TOKENS) |
||
| 236 | ->set('accessed', $query->createNamedParameter(true, IQueryBuilder::PARAM_BOOL)) |
||
| 237 | ->set('timestamp', $query->createNamedParameter(time(), IQueryBuilder::PARAM_INT)) |
||
| 238 | ->where($query->expr()->eq('token', $query->createNamedParameter($token, IQueryBuilder::PARAM_STR))); |
||
| 239 | $result = $query->execute(); |
||
| 240 | return $result !== 0; |
||
| 241 | } |
||
| 242 | |||
| 243 | public function invokeTokenScope($userId): void { |
||
| 246 | } |
||
| 247 | |||
| 248 | public function createToken($editorId, File $file, string $filePath, IShare $share = null): string { |
||
| 249 | $token = $this->random->generate(64, ISecureRandom::CHAR_HUMAN_READABLE); |
||
| 250 | $query = $this->connection->getQueryBuilder(); |
||
| 251 | $query->insert(self::TABLE_TOKENS) |
||
| 252 | ->values([ |
||
| 253 | 'token' => $query->createNamedParameter($token), |
||
| 254 | 'editor_id' => $query->createNamedParameter($editorId), |
||
| 255 | 'file_id' => $query->createNamedParameter($file->getId()), |
||
| 256 | 'file_path' => $query->createNamedParameter($filePath), |
||
| 257 | 'user_id' => $query->createNamedParameter($this->userId), |
||
| 258 | 'share_id' => $query->createNamedParameter($share !== null ? $share->getId(): null), |
||
| 259 | 'timestamp' => $query->createNamedParameter(time()) |
||
| 260 | ]); |
||
| 261 | $query->execute(); |
||
| 262 | return $token; |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param $userId |
||
| 267 | * @param $fileId |
||
| 268 | * @param null $filePath |
||
| 269 | * @return Node |
||
| 270 | * @throws NotFoundException |
||
| 271 | */ |
||
| 272 | public function getFileForToken($userId, $fileId, $filePath = null): Node { |
||
| 282 | } |
||
| 283 | |||
| 284 | public function isEnabled(): bool { |
||
| 285 | if (!$this->encryptionManager->isEnabled()) { |
||
| 286 | return true; |
||
| 287 | } |
||
| 288 | |||
| 289 | try { |
||
| 290 | $moduleId = $this->encryptionManager->getDefaultEncryptionModuleId(); |
||
| 302 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.