AbandonedPaymentLinkCreator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 78
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
B create() 0 47 6
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * You can find more information about us on https://bitbag.io and write us
7
 * an email on [email protected].
8
 */
9
10
declare(strict_types=1);
11
12
namespace BitBag\SyliusMolliePlugin\Creator;
13
14
use BitBag\SyliusMolliePlugin\Entity\GatewayConfigInterface;
15
use BitBag\SyliusMolliePlugin\Entity\OrderInterface;
16
use BitBag\SyliusMolliePlugin\Entity\TemplateMollieEmailInterface;
17
use BitBag\SyliusMolliePlugin\Factory\MollieGatewayFactory;
18
use BitBag\SyliusMolliePlugin\Preparer\PaymentLinkEmailPreparerInterface;
19
use BitBag\SyliusMolliePlugin\Repository\OrderRepositoryInterface;
20
use BitBag\SyliusMolliePlugin\Repository\PaymentMethodRepositoryInterface;
21
use BitBag\SyliusMolliePlugin\Resolver\PaymentlinkResolverInterface;
22
use Sylius\Component\Channel\Context\ChannelContextInterface;
23
use Sylius\Component\Core\Model\PaymentInterface;
24
use Sylius\Component\Payment\Model\PaymentMethodInterface;
25
26
final class AbandonedPaymentLinkCreator implements AbandonedPaymentLinkCreatorInterface
27
{
28
    /** @var PaymentlinkResolverInterface */
29
    private $paymentLinkResolver;
30
31
    /** @var OrderRepositoryInterface */
32
    private $orderRepository;
33
34
    /** @var PaymentLinkEmailPreparerInterface */
35
    private $emailPreparer;
36
37
    /** @var PaymentMethodRepositoryInterface */
38
    private $paymentMethodRepository;
39
40
    /** @var ChannelContextInterface */
41
    private $channelContext;
42
43
    public function __construct(
44
        PaymentlinkResolverInterface $paymentLinkResolver,
45
        OrderRepositoryInterface $orderRepository,
46
        PaymentLinkEmailPreparerInterface $emailPreparer,
47
        PaymentMethodRepositoryInterface $paymentMethodRepository,
48
        ChannelContextInterface $channelContext
49
    ) {
50
        $this->paymentLinkResolver = $paymentLinkResolver;
51
        $this->orderRepository = $orderRepository;
52
        $this->emailPreparer = $emailPreparer;
53
        $this->paymentMethodRepository = $paymentMethodRepository;
54
        $this->channelContext = $channelContext;
55
    }
56
57
    public function create(): void
58
    {
59
        $paymentMethod = $this->paymentMethodRepository->findOneByChannelAndGatewayFactoryName(
60
            $this->channelContext->getChannel(),
61
            MollieGatewayFactory::FACTORY_NAME
62
        );
63
64
        if (null === $paymentMethod) {
65
            return;
66
        }
67
68
        /** @var GatewayConfigInterface $gateway */
69
        $gateway = $paymentMethod->getGatewayConfig();
70
71
        if (null === $gateway) {
72
            return;
73
        }
74
75
        $abandonedEnabled = $gateway->getConfig()['abandoned_email_enabled'] ?? false;
76
77
        if (false === $abandonedEnabled) {
78
            return;
79
        }
80
81
        $abandonedDuration = $gateway->getConfig()['abandoned_hours'] ?? 4;
82
83
        $dateTime = new \DateTime('now');
84
        $duration = new \DateInterval(\sprintf('PT%sH', $abandonedDuration));
85
        $dateTime->sub($duration);
86
87
        $orders = $this->orderRepository->findAbandonedByDateTime($dateTime);
88
89
        /** @var OrderInterface $order */
90
        foreach ($orders as $order) {
91
            /** @var PaymentInterface $payment */
92
            $payment = $order->getPayments()->first();
93
94
            /** @var PaymentMethodInterface $paymentMethod */
95
            $paymentMethod = $payment->getMethod();
96
97
            /** @var \Payum\Core\Model\GatewayConfigInterface $gatewayConfig */
98
            $gatewayConfig = $paymentMethod->getGatewayConfig();
0 ignored issues
show
Bug introduced by
The method getGatewayConfig() does not exist on Sylius\Component\Payment...\PaymentMethodInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Sylius\Component\Payment\Model\PaymentMethod. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

98
            /** @scrutinizer ignore-call */ 
99
            $gatewayConfig = $paymentMethod->getGatewayConfig();
Loading history...
99
100
            if ($gatewayConfig->getFactoryName() === MollieGatewayFactory::FACTORY_NAME) {
0 ignored issues
show
Deprecated Code introduced by
The function Payum\Core\Model\Gateway...rface::getFactoryName() has been deprecated: since 1.3.3 will be removed in 2.0. set factory option inside the config ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

100
            if (/** @scrutinizer ignore-deprecated */ $gatewayConfig->getFactoryName() === MollieGatewayFactory::FACTORY_NAME) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
101
                $this->paymentLinkResolver->resolve($order, [], TemplateMollieEmailInterface::PAYMENT_LINK_ABANDONED);
102
                $order->setAbandonedEmail(true);
103
                $this->orderRepository->add($order);
104
            }
105
        }
106
    }
107
}
108