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

SitemapGenerateTaskHandler::generate()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 3
nop 1
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Content\Sitemap\ScheduledTask;
4
5
use Shopware\Core\Content\Sitemap\Event\SitemapSalesChannelCriteriaEvent;
6
use Shopware\Core\Content\Sitemap\Service\SitemapExporterInterface;
7
use Shopware\Core\Defaults;
8
use Shopware\Core\Framework\Context;
9
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
10
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
11
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
12
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
13
use Shopware\Core\Framework\MessageQueue\ScheduledTask\ScheduledTaskHandler;
14
use Shopware\Core\System\SalesChannel\Aggregate\SalesChannelDomain\SalesChannelDomainEntity;
0 ignored issues
show
Bug introduced by
The type Shopware\Core\System\Sal...alesChannelDomainEntity was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Shopware\Core\System\SalesChannel\SalesChannelEntity;
0 ignored issues
show
Bug introduced by
The type Shopware\Core\System\Sal...nnel\SalesChannelEntity was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Shopware\Core\System\SystemConfig\SystemConfigService;
17
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
18
use Symfony\Component\Messenger\MessageBusInterface;
19
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
20
21
/**
22
 * @package sales-channel
23
 *
24
 * @internal
25
 */
26
#[AsMessageHandler(handles: SitemapGenerateTask::class)]
27
final class SitemapGenerateTaskHandler extends ScheduledTaskHandler
0 ignored issues
show
Deprecated Code introduced by
The class Shopware\Core\Framework\...sk\ScheduledTaskHandler has been deprecated: tag:v6.6.0 - reason:class-hierarchy-change - Won't implement MessageSubscriberInterface anymore, tag all ScheduledTaskHandlers with #[AsMessageHandler] instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

27
final class SitemapGenerateTaskHandler extends /** @scrutinizer ignore-deprecated */ ScheduledTaskHandler
Loading history...
28
{
29
    /**
30
     * @internal
31
     */
32
    public function __construct(
33
        EntityRepository $scheduledTaskRepository,
34
        private EntityRepository $salesChannelRepository,
35
        private  SystemConfigService $systemConfigService,
36
        private MessageBusInterface $messageBus,
37
        private EventDispatcherInterface $eventDispatcher
38
    ) {
39
        parent::__construct($scheduledTaskRepository);
40
    }
41
42
    public function run(): void
43
    {
44
        $sitemapRefreshStrategy = $this->systemConfigService->getInt('core.sitemap.sitemapRefreshStrategy');
45
        if ($sitemapRefreshStrategy !== SitemapExporterInterface::STRATEGY_SCHEDULED_TASK) {
46
            return;
47
        }
48
49
        $criteria = new Criteria();
50
        $criteria->addAssociation('domains');
51
        $criteria->addFilter(new NotFilter(
52
            NotFilter::CONNECTION_AND,
53
            [new EqualsFilter('domains.id', null)]
54
        ));
55
56
        $criteria->addAssociation('type');
57
        $criteria->addFilter(new EqualsFilter('type.id', Defaults::SALES_CHANNEL_TYPE_STOREFRONT));
58
59
        $context = Context::createDefaultContext();
60
61
        $this->eventDispatcher->dispatch(
62
            new SitemapSalesChannelCriteriaEvent($criteria, $context)
63
        );
64
65
        $salesChannels = $this->salesChannelRepository->search($criteria, $context)->getEntities();
66
67
        /** @var SalesChannelEntity $salesChannel */
68
        foreach ($salesChannels as $salesChannel) {
69
            if ($salesChannel->getDomains() === null) {
70
                continue;
71
            }
72
73
            $languageIds = $salesChannel->getDomains()->map(function (SalesChannelDomainEntity $salesChannelDomain) {
74
                return $salesChannelDomain->getLanguageId();
75
            });
76
77
            $languageIds = array_unique($languageIds);
78
79
            foreach ($languageIds as $languageId) {
80
                $this->messageBus->dispatch(new SitemapMessage($salesChannel->getId(), $languageId, null, null, false));
81
            }
82
        }
83
    }
84
}
85