custom/plugins/CbaxModulOrderImportGalaxus/src/Subscriber/BackendSubscriber.php line 43

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Cbax\ModulOrderImportGalaxus\Subscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Cbax\ModulExternalOrder\Core\Content\ExternalOrder\ExternalOrderEvents;
  5. use Cbax\ModulExternalOrder\Components\ExternalOrderHelper;
  6. use Cbax\ModulOrderImportGalaxus\Bootstrap\Database;
  7. use Cbax\ModulOrderImportGalaxus\Components\Client;
  8. class BackendSubscriber implements EventSubscriberInterface
  9. {
  10.     private $client;
  11.     private $externalOrderHelper;
  12.     private $marketplaceName Database::MARKETPLACE_NAME;
  13.     private $localFilesDir;
  14.     public function __construct(
  15.         Client $client,
  16.         ExternalOrderHelper $externalOrderHelper
  17.     ) {
  18.         $this->client $client;
  19.         $this->externalOrderHelper $externalOrderHelper;
  20.         $this->localFilesDir $client->localFilesDir;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             ExternalOrderEvents::CBAX_EXTERNAL_ORDER_IMPORT_FAILED_EVENT => 'onImportFailed',
  26.             ExternalOrderEvents::CBAX_EXTERNAL_ORDER_SAVE_EXTERNAL_ORDER_AFTER_SAVE_EVENT => 'onExternalOrderWritten'
  27.         ];
  28.     }
  29.     /**
  30.      * bei Fehlern beim Import Datei in *_error umbenennen
  31.      * @param $event
  32.      */
  33.     public function onImportFailed($event)
  34.     {
  35.         $failedResults $event->getData();
  36.         if (!empty($failedResults) && !empty($failedResults[$this->marketplaceName])) {
  37.             $localFilesDir $this->localFilesDir '/Orders/input/';
  38.             
  39.             foreach ($failedResults[$this->marketplaceName] as $order) {
  40.                 if (!empty($order['file'])) {
  41.                     $file trim((string) $order['file'][0]);
  42.                     rename($localFilesDir$file$localFilesDir str_replace('.xml''-error.xml'$file));
  43.                 }
  44.                 if (!empty($order->fileName)) {
  45.                     $file trim((string) $order->fileName[0]);
  46.                     rename($localFilesDir$file$localFilesDir str_replace('.xml''-error.xml'$file));
  47.                 }
  48.             }
  49.         }
  50.     }
  51.     /**
  52.      * das Speichern der Galaxus Order prüfen und bei Fehler Datei in *_error umbenennen bzw. bei Erfolg Datei löschen und Bestätigung erzeugen
  53.      * @param $event
  54.      */
  55.     public function onExternalOrderWritten($event)
  56.     {
  57.         $order $event->getData();
  58.         if ($order['marketplace'] === 'galaxus') {
  59.             $context $event->getContext();
  60.             $ordernumber $order['externalOrderNumber'];
  61.     
  62.             $path '/Orders/input/';
  63.             $localFilesDir $this->localFilesDir $path;
  64.     
  65.             $allFiles $this->client->getLocalFiles($path$context);
  66.     
  67.             $fileArray $this->client->filterByOrdernumber($allFiles$ordernumber);
  68.     
  69.             if (count($fileArray) > 0) {
  70.                 $fileName $fileArray[0];
  71.     
  72.                 // Bestellbestätigung im Upload Ordner erzeugen und per Cronjob hochladen
  73.                 $this->client->generateOrderResponse($order$context);
  74.     
  75.                 // später unlink (löschen) der Datei
  76.                 // rename($localFilesDir. $fileName, $localFilesDir . str_replace('.xml', '-completed.xml', $fileName));
  77.                 unlink($localFilesDir$fileName);
  78.             }
  79.         }
  80.     }
  81. }