custom/plugins/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.         if (isset($record['virtual_import']) && $record['virtual_import'] !== '') {
  58.             return;
  59.         }
  60.         /** @var ConditionCollection $conditions */
  61.         $conditions $this->getCouponConditions($record['id'])->getEntities();
  62.         /** @var TransactionCollection $voucherTransactions */
  63.         $voucherTransactions $this->voucherTransactionsService->getTransactionsForVouchers([$record['id']])->getEntities();
  64.         $record['virtual_import'] = \json_encode(
  65.             $this->exportService->buildVirtualImport(
  66.                 $conditions,
  67.                 $voucherTransactions,
  68.                 $record
  69.             )
  70.         );
  71.         $this->exportService->handleVoucherValues($record$voucherTransactions);
  72.         $event->setRecord($record);
  73.     }
  74.     /**
  75.      * @param string $couponId
  76.      *
  77.      * @return EntitySearchResult
  78.      */
  79.     private function getCouponConditions(string $couponId): EntitySearchResult
  80.     {
  81.         $criteria = new Criteria();
  82.         $criteria->addFilter(new EqualsFilter('couponId'$couponId));
  83.         $criteria->addSorting(new FieldSorting('createdAt'FieldSorting::ASCENDING));
  84.         return $this->conditionRepository->search($criteriaContext::createDefaultContext());
  85.     }
  86. }