From 2ffcaef1d60e28efc59e97a8017f18bef6fd5669 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20D?= Date: Sun, 3 Mar 2024 13:06:29 +0100 Subject: [PATCH] chore: Use generator instead of callback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rubén D --- .../Domain/Import/Services/SyspassImport.php | 359 ++++++++---------- .../Domain/Import/Services/XmlImportBase.php | 45 +-- 2 files changed, 173 insertions(+), 231 deletions(-) diff --git a/lib/SP/Domain/Import/Services/SyspassImport.php b/lib/SP/Domain/Import/Services/SyspassImport.php index 9daa8422..99f3f6a1 100644 --- a/lib/SP/Domain/Import/Services/SyspassImport.php +++ b/lib/SP/Domain/Import/Services/SyspassImport.php @@ -208,234 +208,193 @@ final class SyspassImport extends XmlImportBase implements ItemsImportService /** * Obtener las categorías y añadirlas a sysPass. - * - * @throws ImportException */ protected function processCategories(): void { - $this->getNodesData( - 'Categories', - 'Category', - function (DOMElement $category) { - $nodesIterator = new CallbackFilterIterator( - $category->childNodes->getIterator(), - static fn(DOMElement $element) => isset($element->tagName) - ); + foreach ($this->getNodesData('Categories', 'Category') as $category) { + $nodesIterator = new CallbackFilterIterator( + $category->childNodes->getIterator(), + static fn(DOMElement $element) => isset($element->tagName) + ); - $data = []; + $data = []; - /** @var DOMElement $node */ - foreach ($nodesIterator as $node) { - $data[$node->tagName] = $node->nodeValue; - } - - try { - $this->addCategory(new Category($data)); - - $this->eventDispatcher->notify( - 'run.import.syspass.process.category', - new Event( - $this, - EventMessage::factory() - ->addDetail(__u('Category imported'), $data['name']) - ) - ); - } catch (Exception $e) { - processException($e); - - $this->eventDispatcher->notify('exception', new Event($e)); - } + /** @var DOMElement $node */ + foreach ($nodesIterator as $node) { + $data[$node->tagName] = $node->nodeValue; } - ); + + try { + $this->addCategory(new Category($data)); + + $this->eventDispatcher->notify( + 'run.import.syspass.process.category', + new Event( + $this, + EventMessage::factory() + ->addDetail(__u('Category imported'), $data['name']) + ) + ); + } catch (Exception $e) { + processException($e); + + $this->eventDispatcher->notify('exception', new Event($e)); + } + } } - /** - * Obtener los clientes y añadirlos a sysPass. - * - * @throws ImportException - */ - protected function processClients(): void + private function processClients(): void { - $this->getNodesData( - 'Clients', - 'Client', - function (DOMElement $client) { - $nodesIterator = new CallbackFilterIterator( - $client->childNodes->getIterator(), - static fn(DOMElement $element) => isset($element->tagName) - ); + foreach ($this->getNodesData('Clients', 'Client') as $client) { + $nodesIterator = new CallbackFilterIterator( + $client->childNodes->getIterator(), + static fn(DOMElement $element) => isset($element->tagName) + ); - $data = []; + $data = []; - /** @var DOMElement $node */ - foreach ($nodesIterator as $node) { - $data[$node->tagName] = $node->nodeValue; - } - - try { - $this->addClient(new Client($data)); - - $this->eventDispatcher->notify( - 'run.import.syspass.process.client', - new Event( - $this, - EventMessage::factory() - ->addDetail(__u('Client imported'), $data['name']) - ) - ); - } catch (Exception $e) { - processException($e); - - $this->eventDispatcher->notify('exception', new Event($e)); - } + /** @var DOMElement $node */ + foreach ($nodesIterator as $node) { + $data[$node->tagName] = $node->nodeValue; } - ); + + try { + $this->addClient(new Client($data)); + + $this->eventDispatcher->notify( + 'run.import.syspass.process.client', + new Event( + $this, + EventMessage::factory() + ->addDetail(__u('Client imported'), $data['name']) + ) + ); + } catch (Exception $e) { + processException($e); + + $this->eventDispatcher->notify('exception', new Event($e)); + } + } } /** - * Obtener los clientes y añadirlos a sysPass. - * - * @throws ImportException * @deprecated */ - protected function processCustomers(): void + private function processCustomers(): void { - $this->getNodesData( - 'Customers', - 'Customer', - function (DOMElement $client) { - $nodesIterator = new CallbackFilterIterator( - $client->childNodes->getIterator(), - static fn(DOMElement $element) => isset($element->tagName) - ); + foreach ($this->getNodesData('Customers', 'Customer') as $customer) { + $nodesIterator = new CallbackFilterIterator( + $customer->childNodes->getIterator(), + static fn(DOMElement $element) => isset($element->tagName) + ); - $data = []; + $data = []; - /** @var DOMElement $node */ - foreach ($nodesIterator as $node) { - $data[$node->tagName] = $node->nodeValue; - } - - try { - $this->addClient(new Client($data)); - - $this->eventDispatcher->notify( - 'run.import.syspass.process.customer', - new Event( - $this, - EventMessage::factory()->addDetail(__u('Client imported'), $data['name']) - ) - ); - } catch (Exception $e) { - processException($e); - - $this->eventDispatcher->notify('exception', new Event($e)); - } + /** @var DOMElement $node */ + foreach ($nodesIterator as $node) { + $data[$node->tagName] = $node->nodeValue; } - ); + + try { + $this->addClient(new Client($data)); + + $this->eventDispatcher->notify( + 'run.import.syspass.process.customer', + new Event( + $this, + EventMessage::factory()->addDetail(__u('Client imported'), $data['name']) + ) + ); + } catch (Exception $e) { + processException($e); + + $this->eventDispatcher->notify('exception', new Event($e)); + } + } } - /** - * Obtener las etiquetas y añadirlas a sysPass. - * - * @throws ImportException - */ - protected function processTags(): void + private function processTags(): void { - $this->getNodesData( - 'Tags', - 'Tag', - function (DOMElement $tag) { - $nodesIterator = new CallbackFilterIterator( - $tag->childNodes->getIterator(), - static fn(DOMElement $element) => isset($element->tagName) - ); + foreach ($this->getNodesData('Tags', 'Tag') as $tag) { + $nodesIterator = new CallbackFilterIterator( + $tag->childNodes->getIterator(), + static fn(DOMElement $element) => isset($element->tagName) + ); - $data = []; + $data = []; - /** @var DOMElement $node */ - foreach ($nodesIterator as $node) { - if ($node->tagName === 'name') { - $data['name'] = $node->nodeValue; - $data['id'] = $node->getAttribute('id'); - } - } - - try { - $this->addTag(new Tag($data)); - - $this->eventDispatcher->notify( - 'run.import.syspass.process.tag', - new Event( - $this, - EventMessage::factory()->addDetail(__u('Tag imported'), $data['name']) - ) - ); - } catch (Exception $e) { - processException($e); - - $this->eventDispatcher->notify('exception', new Event($e)); - } - }, - false - ); - } - - /** - * Obtener los datos de las cuentas de sysPass y crearlas. - * - * @throws ImportException - */ - protected function processAccounts(ImportParamsDto $importParams): void - { - $this->getNodesData( - 'Accounts', - 'Account', - function (DOMElement $account) use ($importParams) { - $data = []; - - $nodesIterator = new CallbackFilterIterator( - $account->childNodes->getIterator(), - static fn(DOMElement $element) => isset($element->tagName) - ); - - /** @var DOMElement $node */ - foreach ($nodesIterator as $node) { - switch ($node->tagName) { - case 'categoryId': - $data['categoryId'] = - $this->getOrSetCache(self::ITEM_CATEGORY, (int)$node->nodeValue); - break; - case 'clientId': - case 'customerId': - $data['clientId'] = - $this->getOrSetCache(self::ITEM_CLIENT, (int)$node->nodeValue); - break; - case 'tags': - $data['tags'] = $this->processAccountTags($node->childNodes); - break; - default: - $data[$node->tagName] = $node->nodeValue; - } - } - - try { - $this->addAccount(AccountCreateDto::fromAccount(new Account($data)), $importParams, true); - - $this->eventDispatcher->notify( - 'run.import.syspass.process.account', - new Event( - $this, - EventMessage::factory()->addDetail(__u('Account imported'), $data['name']) - ) - ); - } catch (Exception $e) { - processException($e); - - $this->eventDispatcher->notify('exception', new Event($e)); + /** @var DOMElement $node */ + foreach ($nodesIterator as $node) { + if ($node->tagName === 'name') { + $data['name'] = $node->nodeValue; + $data['id'] = $node->getAttribute('id'); } } - ); + + try { + $this->addTag(new Tag($data)); + + $this->eventDispatcher->notify( + 'run.import.syspass.process.tag', + new Event( + $this, + EventMessage::factory()->addDetail(__u('Tag imported'), $data['name']) + ) + ); + } catch (Exception $e) { + processException($e); + + $this->eventDispatcher->notify('exception', new Event($e)); + } + } + } + + private function processAccounts(ImportParamsDto $importParams): void + { + foreach ($this->getNodesData('Accounts', 'Account') as $account) { + $data = []; + + $nodesIterator = new CallbackFilterIterator( + $account->childNodes->getIterator(), + static fn(DOMElement $element) => isset($element->tagName) + ); + + /** @var DOMElement $node */ + foreach ($nodesIterator as $node) { + switch ($node->tagName) { + case 'categoryId': + $data['categoryId'] = + $this->getOrSetCache(self::ITEM_CATEGORY, (int)$node->nodeValue); + break; + case 'clientId': + case 'customerId': + $data['clientId'] = + $this->getOrSetCache(self::ITEM_CLIENT, (int)$node->nodeValue); + break; + case 'tags': + $data['tags'] = $this->processAccountTags($node->childNodes); + break; + default: + $data[$node->tagName] = $node->nodeValue; + } + } + + try { + $this->addAccount(AccountCreateDto::fromAccount(new Account($data)), $importParams, true); + + $this->eventDispatcher->notify( + 'run.import.syspass.process.account', + new Event( + $this, + EventMessage::factory()->addDetail(__u('Account imported'), $data['name']) + ) + ); + } catch (Exception $e) { + processException($e); + + $this->eventDispatcher->notify('exception', new Event($e)); + } + } } /** diff --git a/lib/SP/Domain/Import/Services/XmlImportBase.php b/lib/SP/Domain/Import/Services/XmlImportBase.php index 0a6d8850..43b94af7 100644 --- a/lib/SP/Domain/Import/Services/XmlImportBase.php +++ b/lib/SP/Domain/Import/Services/XmlImportBase.php @@ -26,13 +26,11 @@ namespace SP\Domain\Import\Services; use DOMDocument; use DOMElement; +use Iterator; use SP\Core\Application; use SP\Domain\Config\Ports\ConfigDataInterface; use SP\Domain\Core\Crypt\CryptInterface; -use function SP\__; -use function SP\__u; - /** * Class XmlImportBase * @@ -56,40 +54,25 @@ abstract class XmlImportBase extends ImportBase } /** - * Obtener los datos de los nodos + * Get data from child nodes * - * @param string $nodeName Nombre del nodo principal - * @param string $childNodeName Nombre de los nodos hijos - * @param callable $callback Método a ejecutar - * @param bool $required Indica si el nodo es requerido + * @param string $nodeName Parent node name + * @param string $childNodeName Child node name * - * @throws ImportException + * @return iterable */ - protected function getNodesData( - string $nodeName, - string $childNodeName, - callable $callback, - bool $required = true - ): void { - $nodeList = $this->document->getElementsByTagName($nodeName); + protected function getNodesData(string $nodeName, string $childNodeName): iterable + { + /** @var Iterator $outerNodeList */ + $outerNodeList = $this->document->getElementsByTagName($nodeName)->getIterator(); - if ($nodeList->length > 0) { - if (!is_callable($callback)) { - throw ImportException::warning(__u('Invalid Method'), $callback); - } + foreach ($outerNodeList as $outerNode) { + /** @var Iterator $innerNodeList */ + $innerNodeList = $outerNode->getElementsByTagName($childNodeName)->getIterator(); - /** @var DOMElement $nodes */ - foreach ($nodeList as $nodes) { - /** @var DOMElement $Account */ - foreach ($nodes->getElementsByTagName($childNodeName) as $node) { - $callback($node); - } + foreach ($innerNodeList as $innerNode) { + yield $innerNode; } - } elseif ($required === true) { - throw ImportException::warning( - __u('Invalid XML format'), - sprintf(__('"%s" node doesn\'t exist'), $nodeName) - ); } } }