Passed
Push — master ( 735be0...2f0f13 )
by Christian
12:13 queued 10s
created

ProductExportGenerateTaskHandler::run()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 46
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 28
nc 6
nop 0
dl 0
loc 46
rs 8.8497
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Content\ProductExport\ScheduledTask;
4
5
use Shopware\Core\Content\ProductExport\ProductExportEntity;
6
use Shopware\Core\Defaults;
7
use Shopware\Core\Framework\Context;
8
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
9
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
10
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
11
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
12
use Shopware\Core\Framework\MessageQueue\ScheduledTask\ScheduledTaskHandler;
13
use Shopware\Core\Framework\Uuid\Uuid;
14
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextFactory;
15
use Symfony\Component\Messenger\MessageBusInterface;
16
17
class ProductExportGenerateTaskHandler extends ScheduledTaskHandler
18
{
19
    /**
20
     * @var SalesChannelContextFactory
21
     */
22
    private $salesChannelContextFactory;
23
24
    /**
25
     * @var EntityRepositoryInterface
26
     */
27
    private $salesChannelRepository;
28
29
    /**
30
     * @var EntityRepositoryInterface
31
     */
32
    private $productExportRepository;
33
34
    /**
35
     * @var MessageBusInterface
36
     */
37
    private $messageBus;
38
39
    public function __construct(
40
        EntityRepositoryInterface $scheduledTaskRepository,
41
        SalesChannelContextFactory $salesChannelContextFactory,
42
        EntityRepositoryInterface $salesChannelRepository,
43
        EntityRepositoryInterface $productExportRepository,
44
        MessageBusInterface $messageBus
45
    ) {
46
        parent::__construct($scheduledTaskRepository);
47
48
        $this->salesChannelContextFactory = $salesChannelContextFactory;
49
        $this->salesChannelRepository = $salesChannelRepository;
50
        $this->productExportRepository = $productExportRepository;
51
        $this->messageBus = $messageBus;
52
    }
53
54
    public static function getHandledMessages(): iterable
55
    {
56
        return [
57
            ProductExportGenerateTask::class,
58
        ];
59
    }
60
61
    public function run(): void
62
    {
63
        $criteria = new Criteria();
64
        $criteria
65
            ->addFilter(new EqualsFilter('typeId', Defaults::SALES_CHANNEL_TYPE_STOREFRONT))
66
            ->addFilter(new EqualsFilter('active', true));
67
68
        $salesChannelIds = $this->salesChannelRepository->searchIds($criteria, Context::createDefaultContext());
69
70
        foreach ($salesChannelIds->getIds() as $salesChannelId) {
71
            $salesChannelContext = $this->salesChannelContextFactory->create(Uuid::randomHex(), $salesChannelId);
72
73
            $criteria = new Criteria();
74
            $criteria
75
                ->addAssociation('salesChannel')
76
                ->addAssociation('salesChannelDomain.salesChannel')
77
                ->addAssociation('salesChannelDomain.language.locale')
78
                ->addAssociation('productStream.filters.queries')
79
                ->addFilter(new EqualsFilter('generateByCronjob', true))
80
                ->addFilter(
81
                    new MultiFilter(
82
                        'OR',
83
                        [
84
                            new EqualsFilter('storefrontSalesChannelId', $salesChannelContext->getSalesChannel()->getId()),
85
                            new EqualsFilter('salesChannelDomain.salesChannel.id', $salesChannelContext->getSalesChannel()->getId()),
86
                        ]
87
                    )
88
                );
89
90
            $productExports = $this->productExportRepository->search($criteria, $salesChannelContext->getContext());
91
92
            if ($productExports->count() === 0) {
93
                return;
94
            }
95
96
            $now = new \DateTimeImmutable('now', new \DateTimeZone('UTC'));
97
98
            /** @var ProductExportEntity $productExport */
99
            foreach ($productExports as $productExport) {
100
                // Make sure the product export is due to be exported
101
                if ($productExport->getGeneratedAt() !== null) {
102
                    if ($now->getTimestamp() - $productExport->getGeneratedAt()->getTimestamp() < $productExport->getInterval()) {
103
                        continue;
104
                    }
105
                }
106
                $this->messageBus->dispatch(new ProductExportPartialGeneration($productExport->getId(), $salesChannelId));
107
            }
108
        }
109
    }
110
}
111