| Total Complexity | 61 |
| Total Lines | 373 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like SyspassImport 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 SyspassImport, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 45 | final class SyspassImport extends XmlImportBase implements ImportInterface |
||
| 46 | { |
||
| 47 | /** |
||
| 48 | * Iniciar la importación desde sysPass. |
||
| 49 | * |
||
| 50 | * @throws ImportException |
||
| 51 | * @return ImportInterface |
||
| 52 | */ |
||
| 53 | public function doImport() |
||
| 54 | { |
||
| 55 | try { |
||
| 56 | $this->eventDispatcher->notifyEvent('run.import.syspass', |
||
| 57 | new Event($this, EventMessage::factory() |
||
| 58 | ->addDescription(__u('Importación XML sysPass'))) |
||
| 59 | ); |
||
| 60 | |||
| 61 | if ($this->importParams->getImportMasterPwd() !== '') { |
||
| 62 | $this->mPassValidHash = Hash::checkHashKey($this->importParams->getImportMasterPwd(), $this->configService->getByParam('masterPwd')); |
||
| 63 | } |
||
| 64 | |||
| 65 | $this->version = $this->getXmlVersion(); |
||
| 66 | |||
| 67 | if ($this->detectEncrypted()) { |
||
| 68 | if ($this->importParams->getImportPwd() === '') { |
||
| 69 | throw new ImportException(__u('Clave de encriptación no indicada'), ImportException::INFO); |
||
| 70 | } |
||
| 71 | |||
| 72 | $this->processEncrypted(); |
||
| 73 | } |
||
| 74 | |||
| 75 | $this->checkIntegrity(); |
||
| 76 | |||
| 77 | $this->processCategories(); |
||
| 78 | |||
| 79 | if ($this->version >= 300) { |
||
| 80 | $this->processClients(); |
||
| 81 | } else { |
||
| 82 | $this->processCustomers(); |
||
|
|
|||
| 83 | } |
||
| 84 | |||
| 85 | $this->processTags(); |
||
| 86 | $this->processAccounts(); |
||
| 87 | |||
| 88 | return $this; |
||
| 89 | } catch (ImportException $e) { |
||
| 90 | throw $e; |
||
| 91 | } catch (\Exception $e) { |
||
| 92 | throw new ImportException($e->getMessage(), ImportException::CRITICAL); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Obtener la versión del XML |
||
| 98 | */ |
||
| 99 | protected function getXmlVersion() |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Verificar si existen datos encriptados |
||
| 106 | * |
||
| 107 | * @return bool |
||
| 108 | */ |
||
| 109 | protected function detectEncrypted() |
||
| 110 | { |
||
| 111 | return ($this->xmlDOM->getElementsByTagName('Encrypted')->length > 0); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Procesar los datos encriptados y añadirlos al árbol DOM desencriptados |
||
| 116 | * |
||
| 117 | * @throws ImportException |
||
| 118 | */ |
||
| 119 | protected function processEncrypted() |
||
| 163 | ); |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Checks XML file's data integrity using the signed hash |
||
| 168 | */ |
||
| 169 | protected function checkIntegrity() |
||
| 170 | { |
||
| 171 | $key = $this->importParams->getImportPwd() ?: sha1($this->configData->getPasswordSalt()); |
||
| 172 | |||
| 173 | if (!XmlVerifyService::checkXmlHash($this->xmlDOM, $key)) { |
||
| 174 | $this->eventDispatcher->notifyEvent('run.import.syspass.process.verify', |
||
| 175 | new Event($this, EventMessage::factory() |
||
| 176 | ->addDescription(__u('Fallo en la verificación del hash de integridad')) |
||
| 177 | ->addDescription(__u('Si está importando un archivo exportado desde el mismo origen, los datos pueden estar comprometidos.'))) |
||
| 178 | ); |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Obtener las categorías y añadirlas a sysPass. |
||
| 184 | * |
||
| 185 | * @throws ImportException |
||
| 186 | */ |
||
| 187 | protected function processCategories() |
||
| 217 | } |
||
| 218 | }); |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Obtener los clientes y añadirlos a sysPass. |
||
| 223 | * |
||
| 224 | * @throws ImportException |
||
| 225 | */ |
||
| 226 | protected function processClients() |
||
| 227 | { |
||
| 228 | $this->getNodesData('Clients', 'Client', |
||
| 229 | function (\DOMElement $client) { |
||
| 230 | $clientData = new ClientData(); |
||
| 231 | |||
| 232 | foreach ($client->childNodes as $node) { |
||
| 233 | if (isset($node->tagName)) { |
||
| 234 | switch ($node->tagName) { |
||
| 235 | case 'name': |
||
| 236 | $clientData->setName($node->nodeValue); |
||
| 237 | break; |
||
| 238 | case 'description': |
||
| 239 | $clientData->setDescription($node->nodeValue); |
||
| 240 | break; |
||
| 241 | } |
||
| 242 | } |
||
| 243 | } |
||
| 244 | |||
| 245 | try { |
||
| 246 | $this->addWorkingItem('client', (int)$client->getAttribute('id'), $this->addClient($clientData)); |
||
| 247 | |||
| 248 | $this->eventDispatcher->notifyEvent('run.import.syspass.process.client', |
||
| 249 | new Event($this, EventMessage::factory() |
||
| 250 | ->addDetail(__u('Cliente importado'), $clientData->getName())) |
||
| 251 | ); |
||
| 252 | } catch (\Exception $e) { |
||
| 253 | processException($e); |
||
| 254 | |||
| 255 | $this->eventDispatcher->notifyEvent('exception', new Event($e)); |
||
| 256 | } |
||
| 257 | }); |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Obtener los clientes y añadirlos a sysPass. |
||
| 262 | * |
||
| 263 | * @throws ImportException |
||
| 264 | * @deprecated |
||
| 265 | */ |
||
| 266 | protected function processCustomers() |
||
| 267 | { |
||
| 268 | $this->getNodesData('Customers', 'Customer', |
||
| 269 | function (\DOMElement $client) { |
||
| 270 | $clientData = new ClientData(); |
||
| 271 | |||
| 272 | foreach ($client->childNodes as $node) { |
||
| 273 | if (isset($node->tagName)) { |
||
| 274 | switch ($node->tagName) { |
||
| 275 | case 'name': |
||
| 276 | $clientData->setName($node->nodeValue); |
||
| 277 | break; |
||
| 278 | case 'description': |
||
| 279 | $clientData->setDescription($node->nodeValue); |
||
| 280 | break; |
||
| 281 | } |
||
| 282 | } |
||
| 283 | } |
||
| 284 | |||
| 285 | try { |
||
| 286 | $this->addWorkingItem('client', (int)$client->getAttribute('id'), $this->addClient($clientData)); |
||
| 287 | |||
| 288 | $this->eventDispatcher->notifyEvent('run.import.syspass.process.customer', |
||
| 289 | new Event($this, EventMessage::factory() |
||
| 290 | ->addDetail(__u('Cliente importado'), $clientData->getName())) |
||
| 291 | ); |
||
| 292 | } catch (\Exception $e) { |
||
| 293 | processException($e); |
||
| 294 | |||
| 295 | $this->eventDispatcher->notifyEvent('exception', new Event($e)); |
||
| 296 | } |
||
| 297 | }); |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Obtener las etiquetas y añadirlas a sysPass. |
||
| 302 | * |
||
| 303 | * @throws ImportException |
||
| 304 | */ |
||
| 305 | protected function processTags() |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Obtener los datos de las cuentas de sysPass y crearlas. |
||
| 338 | * |
||
| 339 | * @throws ImportException |
||
| 340 | */ |
||
| 341 | protected function processAccounts() |
||
| 342 | { |
||
| 343 | $this->getNodesData('Accounts', 'Account', |
||
| 344 | function (\DOMElement $account) { |
||
| 345 | $accountRequest = new AccountRequest(); |
||
| 346 | |||
| 347 | /** @var \DOMElement $node */ |
||
| 348 | foreach ($account->childNodes as $node) { |
||
| 349 | if (isset($node->tagName)) { |
||
| 350 | switch ($node->tagName) { |
||
| 351 | case 'name'; |
||
| 352 | $accountRequest->name = $node->nodeValue; |
||
| 353 | break; |
||
| 354 | case 'login'; |
||
| 355 | $accountRequest->login = $node->nodeValue; |
||
| 356 | break; |
||
| 357 | case 'categoryId'; |
||
| 358 | $accountRequest->categoryId = $this->getWorkingItem('category', (int)$node->nodeValue); |
||
| 359 | break; |
||
| 360 | case 'clientId'; |
||
| 361 | case 'customerId'; |
||
| 362 | $accountRequest->clientId = $this->getWorkingItem('client', (int)$node->nodeValue); |
||
| 363 | break; |
||
| 364 | case 'url'; |
||
| 365 | $accountRequest->url = $node->nodeValue; |
||
| 366 | break; |
||
| 367 | case 'pass'; |
||
| 368 | $accountRequest->pass = $node->nodeValue; |
||
| 369 | break; |
||
| 370 | case 'key'; |
||
| 371 | $accountRequest->key = $node->nodeValue; |
||
| 372 | break; |
||
| 373 | case 'notes'; |
||
| 374 | $accountRequest->notes = $node->nodeValue; |
||
| 375 | break; |
||
| 376 | case 'tags': |
||
| 377 | $accountRequest->tags = $this->processAccountTags($node->childNodes); |
||
| 378 | } |
||
| 379 | } |
||
| 380 | } |
||
| 381 | |||
| 382 | try { |
||
| 383 | $this->addAccount($accountRequest); |
||
| 384 | |||
| 385 | $this->eventDispatcher->notifyEvent('run.import.syspass.process.account', |
||
| 386 | new Event($this, EventMessage::factory() |
||
| 387 | ->addDetail(__u('Cuenta importada'), $accountRequest->name)) |
||
| 388 | ); |
||
| 389 | } catch (\Exception $e) { |
||
| 390 | processException($e); |
||
| 391 | |||
| 392 | $this->eventDispatcher->notifyEvent('exception', new Event($e)); |
||
| 393 | } |
||
| 394 | }); |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Procesar las etiquetas de la cuenta |
||
| 399 | * |
||
| 400 | * @param \DOMNodeList $nodes |
||
| 401 | * |
||
| 402 | * @return array |
||
| 403 | */ |
||
| 404 | protected function processAccountTags(\DOMNodeList $nodes) |
||
| 418 | } |
||
| 419 | } |