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

OrderSalutationSubscriber   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 48
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A updateOrderAddressWithNotSpecifiedSalutation() 0 14 1
A getSubscribedEvents() 0 5 1
A __construct() 0 2 1
A setDefaultSalutation() 0 13 5
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Checkout\Order\Subscriber;
4
5
use Doctrine\DBAL\Connection;
6
use Shopware\Core\Checkout\Order\OrderEvents;
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 OrderSalutationSubscriber 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
            OrderEvents::ORDER_ADDRESS_WRITTEN_EVENT => 'setDefaultSalutation',
30
            OrderEvents::ORDER_CUSTOMER_WRITTEN_EVENT => 'setDefaultSalutation',
31
        ];
32
    }
33
34
    public function setDefaultSalutation(EntityWrittenEvent $event): void
35
    {
36
        $payloads = $event->getPayloads();
37
        foreach ($payloads as $payload) {
38
            if (\array_key_exists('salutationId', $payload) && $payload['salutationId']) {
39
                continue;
40
            }
41
42
            if (!isset($payload['id'])) {
43
                continue;
44
            }
45
46
            $this->updateOrderAddressWithNotSpecifiedSalutation($payload['id']);
47
        }
48
    }
49
50
    private function updateOrderAddressWithNotSpecifiedSalutation(string $id): void
51
    {
52
        $this->connection->executeStatement(
53
            '
54
                UPDATE `order_address`
55
                SET `salutation_id` = (
56
                    SELECT `id`
57
                    FROM `salutation`
58
                    WHERE `salutation_key` = :notSpecified
59
                    LIMIT 1
60
                )
61
                WHERE `id` = :id AND `salutation_id` is NULL
62
            ',
63
            ['id' => Uuid::fromHexToBytes($id), 'notSpecified' => SalutationDefinition::NOT_SPECIFIED]
64
        );
65
    }
66
}
67