Passed
Push — trunk ( 0b4eb7...c6408d )
by Christian
11:59 queued 12s
created

setPaymentMethodHandlerRuntimeFields()   A

Complexity

Conditions 6
Paths 17

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 12
c 0
b 0
f 0
nc 17
nop 1
dl 0
loc 24
rs 9.2222

2 Methods

Rating   Name   Duplication   Size   Complexity  
A PaymentHandlerIdentifierSubscriber::isAsynchronous() 0 8 3
A PaymentHandlerIdentifierSubscriber::isSynchronous() 0 8 3
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Checkout\Payment\DataAbstractionLayer;
4
5
use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\AsynchronousPaymentHandlerInterface;
6
use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\PreparedPaymentHandlerInterface;
7
use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\RefundPaymentHandlerInterface;
8
use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\SynchronousPaymentHandlerInterface;
9
use Shopware\Core\Checkout\Payment\PaymentEvents;
10
use Shopware\Core\Framework\DataAbstractionLayer\Entity;
11
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
12
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
14
15
/**
16
 * @internal
17
 *
18
 * @package core
19
 */
20
class PaymentHandlerIdentifierSubscriber implements EventSubscriberInterface
21
{
22
    public static function getSubscribedEvents(): array
23
    {
24
        return [
25
            PaymentEvents::PAYMENT_METHOD_LOADED_EVENT => 'formatHandlerIdentifier',
26
            'payment_method.partial_loaded' => 'formatHandlerIdentifier',
27
        ];
28
    }
29
30
    public function formatHandlerIdentifier(EntityLoadedEvent $event): void
31
    {
32
        /** @var Entity $entity */
33
        foreach ($event->getEntities() as $entity) {
34
            $entity->assign([
35
                'shortName' => $this->getShortName($entity),
36
                'formattedHandlerIdentifier' => $this->getHandlerIdentifier($entity),
37
                'synchronous' => $this->isSynchronous($entity),
38
                'asynchronous' => $this->isAsynchronous($entity),
39
                'prepared' => $this->isPrepared($entity),
40
                'refundable' => $this->isRefundable($entity),
41
            ]);
42
        }
43
    }
44
45
    private function getHandlerIdentifier(Entity $entity): string
46
    {
47
        $explodedHandlerIdentifier = explode('\\', $entity->get('handlerIdentifier'));
48
49
        if (\count($explodedHandlerIdentifier) < 2) {
50
            return $entity->get('handlerIdentifier');
51
        }
52
53
        /** @var string|null $firstHandlerIdentifier */
54
        $firstHandlerIdentifier = array_shift($explodedHandlerIdentifier);
55
        $lastHandlerIdentifier = array_pop($explodedHandlerIdentifier);
56
        if ($firstHandlerIdentifier === null || $lastHandlerIdentifier === null) {
57
            return '';
58
        }
59
60
        return 'handler_'
61
            . mb_strtolower($firstHandlerIdentifier)
62
            . '_'
63
            . mb_strtolower($lastHandlerIdentifier);
64
    }
65
66
    private function isSynchronous(Entity $entity): bool
67
    {
68
        if (($app = $entity->get('appPaymentMethod')) !== null) {
69
            /** @var Entity $app */
70
            return !($app->get('payUrl') && $app->get('finalizeUrl'));
71
        }
72
73
        return \is_a($entity->get('handlerIdentifier'), SynchronousPaymentHandlerInterface::class, true);
74
    }
75
76
    private function isAsynchronous(Entity $entity): bool
77
    {
78
        if (($app = $entity->get('appPaymentMethod')) !== null) {
79
            /** @var Entity $app */
80
            return $app->get('payUrl') && $app->get('finalizeUrl');
81
        }
82
83
        return \is_a($entity->get('handlerIdentifier'), AsynchronousPaymentHandlerInterface::class, true);
84
    }
85
86
    private function getShortName(Entity $entity): string
87
    {
88
        $explodedHandlerIdentifier = explode('\\', $entity->get('handlerIdentifier'));
89
90
        $last = $explodedHandlerIdentifier[\count($explodedHandlerIdentifier) - 1];
91
92
        return (new CamelCaseToSnakeCaseNameConverter())->normalize($last);
93
    }
94
95
    private function isPrepared(Entity $entity): bool
96
    {
97
        if (($app = $entity->get('appPaymentMethod')) !== null) {
98
            /** @var Entity $app */
99
            return $app->get('validateUrl') && $app->get('captureUrl');
100
        }
101
102
        return \is_a($entity->get('handlerIdentifier'), PreparedPaymentHandlerInterface::class, true);
103
    }
104
105
    private function isRefundable(Entity $entity): bool
106
    {
107
        if (($app = $entity->get('appPaymentMethod')) !== null) {
108
            /** @var Entity $app */
109
            return $app->get('refundUrl') !== null;
110
        }
111
112
        return \is_a($entity->get('handlerIdentifier'), RefundPaymentHandlerInterface::class, true);
113
    }
114
}
115