Passed
Push — trunk ( dee18d...92a8fe )
by Christian
12:13 queued 12s
created

SitemapMessageHandler::generate()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
nc 3
nop 1
dl 0
loc 12
rs 10
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Content\Sitemap\ScheduledTask;
4
5
use Psr\Log\LoggerInterface;
6
use Shopware\Core\Content\Sitemap\Exception\AlreadyLockedException;
7
use Shopware\Core\Content\Sitemap\Service\SitemapExporterInterface;
8
use Shopware\Core\System\SalesChannel\Context\AbstractSalesChannelContextFactory;
9
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
10
use Shopware\Core\System\SystemConfig\SystemConfigService;
11
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
12
13
/**
14
 * @package sales-channel
15
 *
16
 * @internal
17
 */
18
#[AsMessageHandler]
19
final class SitemapMessageHandler
20
{
21
    /**
22
     * @internal
23
     */
24
    public function __construct(
25
        private AbstractSalesChannelContextFactory $salesChannelContextFactory,
26
        private SitemapExporterInterface $sitemapExporter,
27
        private LoggerInterface $logger,
28
        private  SystemConfigService $systemConfigService,
29
    ) {
30
    }
31
32
    public function __invoke(SitemapMessage $message): void
33
    {
34
        $sitemapRefreshStrategy = $this->systemConfigService->getInt('core.sitemap.sitemapRefreshStrategy');
35
        if ($sitemapRefreshStrategy !== SitemapExporterInterface::STRATEGY_SCHEDULED_TASK) {
36
            return;
37
        }
38
39
        $this->generate($message);
40
    }
41
42
    private function generate(SitemapMessage $message): void
43
    {
44
        if ($message->getLastSalesChannelId() === null || $message->getLastLanguageId() === null) {
0 ignored issues
show
introduced by
The condition $message->getLastLanguageId() === null is always false.
Loading history...
45
            return;
46
        }
47
48
        $context = $this->salesChannelContextFactory->create('', $message->getLastSalesChannelId(), [SalesChannelContextService::LANGUAGE_ID => $message->getLastLanguageId()]);
49
50
        try {
51
            $this->sitemapExporter->generate($context, true, $message->getLastProvider(), $message->getNextOffset());
52
        } catch (AlreadyLockedException $exception) {
53
            $this->logger->error(sprintf('ERROR: %s', $exception->getMessage()));
54
        }
55
    }
56
}
57