vendor/store.shopware.com/netinexteasycoupon/src/Subscriber/ExportSubscriber.php line 58

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace NetInventors\NetiNextEasyCoupon\Subscriber;
  4. use NetInventors\NetiNextEasyCoupon\Core\Content\Condition\ConditionCollection;
  5. use NetInventors\NetiNextEasyCoupon\Core\Content\Transaction\TransactionCollection;
  6. use NetInventors\NetiNextEasyCoupon\Service\Account\VoucherTransactionsService;
  7. use NetInventors\NetiNextEasyCoupon\Service\ExportService;
  8. use Psr\Log\LoggerInterface;
  9. use Shopware\Core\Content\ImportExport\Event\ImportExportBeforeExportRecordEvent;
  10. use Shopware\Core\Framework\Context;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class ExportSubscriber implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var EntityRepositoryInterface
  21.      */
  22.     private $conditionRepository;
  23.     /**
  24.      * @var VoucherTransactionsService
  25.      */
  26.     private                 $voucherTransactionsService;
  27.     private ExportService   $exportService;
  28.     /**
  29.      * ExportSubscriber constructor.
  30.      *
  31.      * @param EntityRepositoryInterface $conditionRepository
  32.      */
  33.     public function __construct(
  34.         EntityRepositoryInterface  $conditionRepository,
  35.         VoucherTransactionsService $voucherTransactionsService,
  36.         ExportService              $exportService
  37.     ) {
  38.         $this->conditionRepository        $conditionRepository;
  39.         $this->voucherTransactionsService $voucherTransactionsService;
  40.         $this->exportService              $exportService;
  41.     }
  42.     public static function getSubscribedEvents()
  43.     {
  44.         return [
  45.             ImportExportBeforeExportRecordEvent::class => 'beforeExport',
  46.         ];
  47.     }
  48.     public function beforeExport(ImportExportBeforeExportRecordEvent $event)
  49.     {
  50.         if (
  51.             'neti_easy_coupon' !== $event->getConfig()->get('sourceEntity')
  52.             || null !== $event->getConfig()->getMapping()->get('_error')
  53.         ) {
  54.             return;
  55.         }
  56.         $record $event->getRecord();
  57.         /** @var ConditionCollection $conditions */
  58.         $conditions $this->getCouponConditions($record['id'])->getEntities();
  59.         /** @var TransactionCollection $voucherTransactions */
  60.         $voucherTransactions $this->voucherTransactionsService->getTransactionsForVouchers([$record['id']])->getEntities();
  61.         $record['virtual_import'] = \json_encode(
  62.             $this->exportService->buildVirtualImport(
  63.                 $conditions,
  64.                 $voucherTransactions,
  65.                 $record
  66.             )
  67.         );
  68.         $this->exportService->handleVoucherValues($record$voucherTransactions);
  69.         $event->setRecord($record);
  70.     }
  71.     /**
  72.      * @param string $couponId
  73.      *
  74.      * @return EntitySearchResult
  75.      */
  76.     private function getCouponConditions(string $couponId): EntitySearchResult
  77.     {
  78.         $criteria = new Criteria();
  79.         $criteria->addFilter(new EqualsFilter('couponId'$couponId));
  80.         $criteria->addSorting(new FieldSorting('createdAt'FieldSorting::ASCENDING));
  81.         return $this->conditionRepository->search($criteriaContext::createDefaultContext());
  82.     }
  83. }