Passed
Push — master ( 6f7323...f623c6 )
by Christian
12:47 queued 12s
created

CustomerVatIdsDeprecationUpdater   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A updateVatIdsInCustomer() 0 16 2
A __construct() 0 3 1
A updateByEvent() 0 5 3
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Checkout\Customer\DataAbstractionLayer;
4
5
use Doctrine\DBAL\Connection;
6
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
7
use Shopware\Core\Framework\Uuid\Uuid;
8
9
/**
10
 * @feature-deprecated (flag:FEATURE_NEXT_10559) tag:v6.4.0 - class will be removed in 6.4.0
11
 */
12
class CustomerVatIdsDeprecationUpdater
13
{
14
    /**
15
     * @var Connection
16
     */
17
    private $connection;
18
19
    public function __construct(Connection $connection)
20
    {
21
        $this->connection = $connection;
22
    }
23
24
    public function updateByEvent(EntityWrittenEvent $event): void
25
    {
26
        foreach ($event->getPayloads() as $payload) {
27
            if (isset($payload['vatIds'])) {
28
                $this->updateVatIdsInCustomer($payload['vatIds'], $payload['id']);
29
            }
30
        }
31
    }
32
33
    private function updateVatIdsInCustomer(array $vatIds, string $customerId): void
34
    {
35
        if (!empty($vatIds)) {
36
            $this->connection->executeUpdate(
37
                'UPDATE `customer_address` SET `vat_id` = :vatId
38
                    WHERE `customer_address`.`customer_id` = :customerId
39
                    AND (customer_address.vat_id <> :vatId OR customer_address.vat_id IS NULL)',
40
                [
41
                    'vatId' => $vatIds[0],
42
                    'customerId' => Uuid::fromHexToBytes($customerId),
43
                ]
44
            );
45
        } else {
46
            $this->connection->executeUpdate(
47
                'UPDATE `customer_address` SET `vat_id` = NULL WHERE `customer_id` = :customerId',
48
                ['customerId' => Uuid::fromHexToBytes($customerId)]
49
            );
50
        }
51
    }
52
}
53