Passed
Push — trunk ( ad06ea...8f56e6 )
by Christian
23:45 queued 17s
created

RuleIndexerSubscriber   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 37
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A onRuleWritten() 0 3 1
A refreshPlugin() 0 8 1
A __construct() 0 4 1
A getSubscribedEvents() 0 9 1
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Content\Rule\DataAbstractionLayer;
4
5
use Doctrine\DBAL\Connection;
6
use Shopware\Core\Checkout\Cart\CartRuleLoader;
7
use Shopware\Core\Content\Rule\RuleEvents;
8
use Shopware\Core\Framework\DataAbstractionLayer\Doctrine\RetryableQuery;
9
use Shopware\Core\Framework\Log\Package;
10
use Shopware\Core\Framework\Plugin\Event\PluginPostActivateEvent;
11
use Shopware\Core\Framework\Plugin\Event\PluginPostDeactivateEvent;
12
use Shopware\Core\Framework\Plugin\Event\PluginPostInstallEvent;
13
use Shopware\Core\Framework\Plugin\Event\PluginPostUninstallEvent;
14
use Shopware\Core\Framework\Plugin\Event\PluginPostUpdateEvent;
15
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16
17
/**
18
 * @internal
19
 */
20
#[Package('business-ops')]
21
class RuleIndexerSubscriber implements EventSubscriberInterface
22
{
23
    /**
24
     * @internal
25
     */
26
    public function __construct(
27
        private readonly Connection $connection,
28
        private readonly CartRuleLoader $cartRuleLoader
29
    ) {
30
    }
31
32
    public static function getSubscribedEvents(): array
33
    {
34
        return [
35
            PluginPostInstallEvent::class => 'refreshPlugin',
36
            PluginPostActivateEvent::class => 'refreshPlugin',
37
            PluginPostUpdateEvent::class => 'refreshPlugin',
38
            PluginPostDeactivateEvent::class => 'refreshPlugin',
39
            PluginPostUninstallEvent::class => 'refreshPlugin',
40
            RuleEvents::RULE_WRITTEN_EVENT => 'onRuleWritten',
41
        ];
42
    }
43
44
    public function refreshPlugin(): void
45
    {
46
        // Delete the payload and invalid flag of all rules
47
        $update = new RetryableQuery(
48
            $this->connection,
49
            $this->connection->prepare('UPDATE `rule` SET `payload` = null, `invalid` = 0')
50
        );
51
        $update->execute();
52
    }
53
54
    public function onRuleWritten(): void
55
    {
56
        $this->cartRuleLoader->invalidate();
57
    }
58
}
59