Passed
Push — trunk ( aa92c3...83ea4e )
by Christian
11:27 queued 13s
created

setDefaultSalutation()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 7
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 13
rs 9.6111
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Content\Newsletter\Subscriber;
4
5
use Doctrine\DBAL\Connection;
6
use Shopware\Core\Content\Newsletter\NewsletterEvents;
7
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
8
use Shopware\Core\Framework\Log\Package;
9
use Shopware\Core\Framework\Uuid\Uuid;
10
use Shopware\Core\System\Salutation\SalutationDefinition;
11
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
12
13
/**
14
 * @internal
15
 */
16
#[Package('customer-order')]
17
class NewsletterRecipientSalutationSubscriber implements EventSubscriberInterface
18
{
19
    public function __construct(private readonly Connection $connection)
20
    {
21
    }
22
23
    /**
24
     * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, string|arr...y{0: string, 1?: int}>> at position 17 could not be parsed: Expected '>' at position 17, but found 'list'.
Loading history...
25
     */
26
    public static function getSubscribedEvents(): array
27
    {
28
        return [
29
            NewsletterEvents::NEWSLETTER_RECIPIENT_WRITTEN_EVENT => 'setDefaultSalutation',
30
        ];
31
    }
32
33
    public function setDefaultSalutation(EntityWrittenEvent $event): void
34
    {
35
        $payloads = $event->getPayloads();
36
        foreach ($payloads as $payload) {
37
            if (\array_key_exists('salutationId', $payload) && $payload['salutationId']) {
38
                continue;
39
            }
40
41
            if (!isset($payload['id'])) {
42
                continue;
43
            }
44
45
            $this->updateNewsletterRecipientWithNotSpecifiedSalutation($payload['id']);
46
        }
47
    }
48
49
    private function updateNewsletterRecipientWithNotSpecifiedSalutation(string $id): void
50
    {
51
        $this->connection->executeStatement(
52
            '
53
                UPDATE `newsletter_recipient`
54
                SET `salutation_id` = (
55
                    SELECT `id`
56
                    FROM `salutation`
57
                    WHERE `salutation_key` = :notSpecified
58
                    LIMIT 1
59
                )
60
                WHERE `id` = :id AND `salutation_id` is NULL
61
            ',
62
            ['id' => Uuid::fromHexToBytes($id), 'notSpecified' => SalutationDefinition::NOT_SPECIFIED]
63
        );
64
    }
65
}
66