Completed
Push — master ( 4d6ab5...69baaa )
by
unknown
18:44 queued 08:09
created

AbandonedPaymentLinkCreator::create()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
c 2
b 0
f 0
dl 0
loc 32
rs 9.3888
cc 5
nc 5
nop 0
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
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusMolliePlugin\Creator;
14
15
use BitBag\SyliusMolliePlugin\Entity\GatewayConfigInterface;
16
use BitBag\SyliusMolliePlugin\Entity\OrderInterface;
0 ignored issues
show
Bug introduced by
The type BitBag\SyliusMolliePlugin\Entity\OrderInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use BitBag\SyliusMolliePlugin\Entity\TemplateMollieEmailInterface;
0 ignored issues
show
Bug introduced by
The type BitBag\SyliusMolliePlugi...ateMollieEmailInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use BitBag\SyliusMolliePlugin\Factory\MollieGatewayFactory;
19
use BitBag\SyliusMolliePlugin\Preparer\PaymentLinkEmailPreparerInterface;
0 ignored issues
show
Bug introduced by
The type BitBag\SyliusMolliePlugi...kEmailPreparerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use BitBag\SyliusMolliePlugin\Repository\OrderRepositoryInterface;
0 ignored issues
show
Bug introduced by
The type BitBag\SyliusMolliePlugi...rderRepositoryInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use BitBag\SyliusMolliePlugin\Resolver\PaymentlinkResolverInterface;
0 ignored issues
show
Bug introduced by
The type BitBag\SyliusMolliePlugi...ntlinkResolverInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use Sylius\Component\Core\Model\PaymentInterface;
23
use Sylius\Component\Resource\Repository\RepositoryInterface;
24
25
final class AbandonedPaymentLinkCreator implements AbandonedPaymentLinkCreatorInterface
0 ignored issues
show
Bug introduced by
The type BitBag\SyliusMolliePlugi...entLinkCreatorInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
{
27
    /** @var PaymentlinkResolverInterface */
28
    private $paymentlinkResolver;
29
30
    /** @var OrderRepositoryInterface */
31
    private $orderRepository;
32
33
    /** @var PaymentLinkEmailPreparerInterface */
34
    private $emailPreparer;
35
36
    /** @var RepositoryInterface */
37
    private $gatewayConfigRepository;
38
39
    public function __construct(
40
        PaymentlinkResolverInterface $paymentlinkResolver,
41
        OrderRepositoryInterface $orderRepository,
42
        PaymentLinkEmailPreparerInterface $emailPreparer,
43
        RepositoryInterface $gatewayConfigRepository
44
    ) {
45
        $this->paymentlinkResolver = $paymentlinkResolver;
46
        $this->orderRepository = $orderRepository;
47
        $this->emailPreparer = $emailPreparer;
48
        $this->gatewayConfigRepository = $gatewayConfigRepository;
49
    }
50
51
    public function create(): void
52
    {
53
        /** @var GatewayConfigInterface $gateway */
54
        $gateway = $this->gatewayConfigRepository->findOneBy(['factoryName' => MollieGatewayFactory::FACTORY_NAME]);
55
56
        if (null === $gateway) {
57
            return;
58
        }
59
60
        $abandonedEnabled = $gateway->getConfig()['abandoned_email_enabled'] ?? false;
61
62
        if (false === $abandonedEnabled) {
63
            return;
64
        }
65
66
        $abandonedDuration = $gateway->getConfig()['abandoned_hours'] ?? 4;
67
68
        $dateTime = new \DateTime('now');
69
        $duration = new \DateInterval(\sprintf('PT%sH', $abandonedDuration));
70
        $dateTime->sub($duration);
71
72
        $orders = $this->orderRepository->findAbandonedByDateTime($dateTime);
73
74
        /** @var OrderInterface $order */
75
        foreach ($orders as $order) {
76
            /** @var PaymentInterface $payment */
77
            $payment = $order->getPayments()->first();
78
79
            if ($payment->getMethod()->getGatewayConfig()->getFactoryName() === MollieGatewayFactory::FACTORY_NAME) {
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

79
            if ($payment->getMethod()->/** @scrutinizer ignore-call */ getGatewayConfig()->getFactoryName() === MollieGatewayFactory::FACTORY_NAME) {
Loading history...
80
                $this->paymentlinkResolver->resolve($order, [], TemplateMollieEmailInterface::PAYMENT_LINK_ABANDONED);
81
                $order->setAbandonedEmail(true);
82
                $this->orderRepository->add($order);
83
            }
84
        }
85
    }
86
}
87