. */ namespace SP\Domain\Export\Services; use DOMElement; use Exception; use SP\Core\Application; use SP\Core\Events\Event; use SP\Core\Events\EventMessage; use SP\Domain\Client\Ports\ClientService; use SP\Domain\Common\Services\ServiceException; use function SP\__u; /** * Class XmlClientExport */ final class XmlClientExport extends XmlExportEntityBase { public function __construct( Application $application, private readonly ClientService $clientService ) { parent::__construct($application); } /** * Crear el nodo con los datos * * @throws ServiceException * @throws ServiceException */ public function export(): DOMElement { try { $this->eventDispatcher->notify( 'run.export.process.client', new Event($this, EventMessage::factory()->addDescription(__u('Exporting clients'))) ); $clients = $this->clientService->getAll(); $nodeClients = $this->document->createElement('Clients'); if ($nodeClients === false) { throw ServiceException::error(__u('Unable to create node')); } if (count($clients) === 0) { return $nodeClients; } foreach ($clients as $client) { $nodeClient = $this->document->createElement('Client'); $nodeClients->appendChild($nodeClient); $nodeClient->setAttribute('id', $client->getId()); $nodeClient->appendChild( $this->document->createElement( 'name', $this->document->createTextNode($client->getName())->nodeValue ) ); $nodeClient->appendChild( $this->document->createElement( 'description', $this->document->createTextNode($client->getDescription())->nodeValue ) ); } return $nodeClients; } catch (Exception $e) { throw ServiceException::error($e->getMessage(), __FUNCTION__); } } }