Passed
Push — master ( 84c178...0a4c2b )
by
unknown
10:55 queued 05:13
created

getChannelsNameByChannels()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 10
rs 10
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\Validator\Constraints;
13
14
use BitBag\SyliusMolliePlugin\Entity\GatewayConfigInterface;
15
use BitBag\SyliusMolliePlugin\Factory\MollieGatewayFactory;
16
use BitBag\SyliusMolliePlugin\Repository\PaymentMethodRepositoryInterface;
17
use Doctrine\Common\Collections\ArrayCollection;
18
use Doctrine\Common\Collections\Collection;
19
use Sylius\Component\Core\Model\ChannelInterface;
20
use Sylius\Component\Core\Model\PaymentMethodInterface;
21
use Symfony\Component\Validator\Constraint;
22
use Symfony\Component\Validator\ConstraintValidator as ConstraintValidatorAlias;
23
use Symfony\Contracts\Translation\TranslatorInterface;
24
25
final class PaymentMethodMollieChannelUniqueValidator extends ConstraintValidatorAlias
26
{
27
    /** @var PaymentMethodRepositoryInterface */
28
    private $paymentMethodRepository;
29
30
    /** @var TranslatorInterface */
31
    private $translator;
32
33
    public function __construct(
34
        PaymentMethodRepositoryInterface $paymentMethodRepository,
35
        TranslatorInterface $translator
36
    ) {
37
        $this->paymentMethodRepository = $paymentMethodRepository;
38
        $this->translator = $translator;
39
    }
40
41
    public function validate($value, Constraint $constraint): void
42
    {
43
        if ($value instanceof PaymentMethodInterface && null !== $value->getCode()) {
44
            false === $this->isMolliePaymentMethod($value) ?: $this->validateMolliePaymentMethod($value, $constraint);
45
        }
46
    }
47
48
    private function validateMolliePaymentMethod(PaymentMethodInterface $paymentMethod, Constraint $constraint): void
0 ignored issues
show
Unused Code introduced by
The parameter $constraint is not used and could be removed. ( Ignorable by Annotation )

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

48
    private function validateMolliePaymentMethod(PaymentMethodInterface $paymentMethod, /** @scrutinizer ignore-unused */ Constraint $constraint): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
    {
50
        $molliePaymentMethods = $this->paymentMethodRepository->findAllByFactoryNameAndCode($paymentMethod->getCode());
0 ignored issues
show
Bug introduced by
It seems like $paymentMethod->getCode() can also be of type null; however, parameter $code of BitBag\SyliusMolliePlugi...lByFactoryNameAndCode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

50
        $molliePaymentMethods = $this->paymentMethodRepository->findAllByFactoryNameAndCode(/** @scrutinizer ignore-type */ $paymentMethod->getCode());
Loading history...
51
52
        if (0 === count($molliePaymentMethods)) {
53
            return;
54
        }
55
56
        $alreadyUsedChannels = $this->getAlreadyUsedChannels($molliePaymentMethods);
57
58
        if ($this->isTheSameChannel($paymentMethod->getChannels(), $alreadyUsedChannels)) {
59
            $translation = $this->translator->trans('bitbag_sylius_mollie_plugin.form.channel_should_be_unique', [
60
                '{channels}' => $this->getChannelsNameByChannels($alreadyUsedChannels),
61
            ]);
62
63
            $this->context->buildViolation($translation)->atPath('channels')->addViolation();
64
        }
65
    }
66
67
    private function getAlreadyUsedChannels(array $molliePaymentMethods): Collection
68
    {
69
        $alreadyUsedChannels = new ArrayCollection();
70
71
        /** @var PaymentMethodInterface $molliePaymentMethod */
72
        foreach ($molliePaymentMethods as $molliePaymentMethod) {
73
            /** @var ChannelInterface $channel */
74
            foreach ($molliePaymentMethod->getChannels() as $channel) {
75
                if (!$alreadyUsedChannels->contains($channel)) {
76
                    $alreadyUsedChannels->add($channel);
77
                }
78
            }
79
        }
80
81
        return $alreadyUsedChannels;
82
    }
83
84
    private function isTheSameChannel(Collection $newChannels, Collection $paymentMethodExistingChannels): bool
85
    {
86
        foreach ($paymentMethodExistingChannels as $paymentMethodExistingChannel) {
87
            if ($newChannels->contains($paymentMethodExistingChannel)) {
88
                return true;
89
            }
90
        }
91
92
        return false;
93
    }
94
95
    private function isMolliePaymentMethod(PaymentMethodInterface $paymentMethod): bool
96
    {
97
        /** @var GatewayConfigInterface $gateway */
98
        $gateway = $paymentMethod->getGatewayConfig();
99
100
        return $gateway->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
        return /** @scrutinizer ignore-deprecated */ $gateway->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
    }
102
103
    private function getChannelsNameByChannels(Collection $alreadyUsedChannels): string
104
    {
105
        $channelsNames = '';
106
107
        /** @var ChannelInterface $channel */
108
        foreach ($alreadyUsedChannels as $channel) {
109
            $channelsNames .= \sprintf('%s ', $channel->getName());
110
        }
111
112
        return $channelsNames;
113
    }
114
}
115