UpdatePaymentStateCommand   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A configure() 0 6 1
A execute() 0 33 4
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\SyliusQuadPayPlugin\Command;
14
15
use BitBag\SyliusQuadPayPlugin\QuadPayGatewayFactory;
16
use BitBag\SyliusQuadPayPlugin\Repository\PaymentRepositoryInterface;
17
use BitBag\SyliusQuadPayPlugin\Resolver\PaymentStateResolverInterface;
18
use Psr\Log\LoggerInterface;
19
use Sylius\Component\Core\Model\PaymentInterface;
20
use Symfony\Component\Console\Command\Command;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Output\OutputInterface;
23
24
final class UpdatePaymentStateCommand extends Command
25
{
26
    /** @var PaymentRepositoryInterface */
27
    private $paymentRepository;
28
29
    /** @var PaymentStateResolverInterface */
30
    private $paymentStateResolver;
31
32
    /** @var LoggerInterface */
33
    private $logger;
34
35
    public function __construct(
36
        PaymentRepositoryInterface $paymentRepository,
37
        PaymentStateResolverInterface $paymentStateResolver,
38
        LoggerInterface $logger
39
    ) {
40
        parent::__construct();
41
42
        $this->paymentRepository = $paymentRepository;
43
        $this->paymentStateResolver = $paymentStateResolver;
44
        $this->logger = $logger;
45
    }
46
47
    protected function configure(): void
48
    {
49
        $this
50
            ->setName('bitbag:quadpay:update-payment-state')
51
            ->setDescription('Updates the payments state.')
52
            ->setHelp('This command allows you to update the payments state for QuadPay gateway.')
53
        ;
54
    }
55
56
    protected function execute(InputInterface $input, OutputInterface $output): void
57
    {
58
        /** @var PaymentInterface[] $payments */
59
        $payments = $this->paymentRepository->findAllActiveByGatewayFactoryName(QuadPayGatewayFactory::FACTORY_NAME);
60
61
        $updatesCount = 0;
62
63
        foreach ($payments as $payment) {
64
            $oldState = $payment->getState();
65
66
            $orderNumber = $payment->getOrder()->getNumber();
67
68
            try {
69
                $this->paymentStateResolver->resolve($payment);
70
            } catch (\Exception $exception) {
71
                $message = sprintf('An error occurred for the order #%s: %s', $orderNumber, $exception->getMessage());
72
73
                $this->logger->error($message);
74
75
                $output->writeln($message);
76
77
                continue;
78
            }
79
80
            if ($oldState !== $payment->getState()) {
81
                ++$updatesCount;
82
83
                $output->writeln(sprintf('Update payment state for order #%s: %s -> %s', $orderNumber, $oldState, $payment->getState()));
84
            }
85
        }
86
87
        $output->writeln('');
88
        $output->writeln(sprintf('Updated: %d', $updatesCount));
89
    }
90
}
91