Passed
Push — master ( aff96c...cdda13 )
by
unknown
14:50 queued 08:02
created

isMolliePaymentMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 1
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\Collection;
18
use Sylius\Component\Core\Model\PaymentMethodInterface;
19
use Symfony\Component\Validator\Constraint;
20
use Symfony\Component\Validator\ConstraintValidator as ConstraintValidatorAlias;
21
22
final class PaymentMethodMollieChannelUniqueValidator extends ConstraintValidatorAlias
23
{
24
    /** @var PaymentMethodRepositoryInterface */
25
    private $paymentMethodRepository;
26
27
    public function __construct(PaymentMethodRepositoryInterface $paymentMethodRepository)
28
    {
29
        $this->paymentMethodRepository = $paymentMethodRepository;
30
    }
31
32
    public function validate($value, Constraint $constraint): void
33
    {
34
        if ($value instanceof PaymentMethodInterface && null !== $value->getCode()) {
35
            false === $this->isMolliePaymentMethod($value) ?: $this->validateMolliePaymentMethod($value, $constraint);
36
        }
37
    }
38
39
    private function validateMolliePaymentMethod(PaymentMethodInterface $paymentMethod, Constraint $constraint): void
40
    {
41
        $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

41
        $molliePaymentMethods = $this->paymentMethodRepository->findAllByFactoryNameAndCode(/** @scrutinizer ignore-type */ $paymentMethod->getCode());
Loading history...
42
43
        if (0 === count($molliePaymentMethods)) {
44
            return;
45
        }
46
47
        /** @var PaymentMethodInterface $molliePaymentMethod */
48
        foreach ($molliePaymentMethods as $molliePaymentMethod) {
49
            if (true === $this->isTheSameChannel($paymentMethod->getChannels(), $molliePaymentMethod->getChannels())) {
50
                $this->context->buildViolation($constraint->message)->atPath('channels')->addViolation();
51
            }
52
        }
53
    }
54
55
    private function isTheSameChannel(Collection $newChannels, Collection $existingChannels): bool
56
    {
57
        foreach ($existingChannels as $existingChannel) {
58
            if ($newChannels->contains($existingChannel)) {
59
                return true;
60
            }
61
        }
62
63
        return false;
64
    }
65
66
    private function isMolliePaymentMethod(PaymentMethodInterface $paymentMethod): bool
67
    {
68
        /** @var GatewayConfigInterface $gateway */
69
        $gateway = $paymentMethod->getGatewayConfig();
70
71
        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

71
        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...
72
    }
73
}
74