CurrencyValidator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 32
rs 10
c 0
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 30 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\Validator\Constraints;
13
14
use BitBag\SyliusMolliePlugin\Factory\MollieSubscriptionGatewayFactory;
15
use Sylius\Component\Core\Model\ChannelInterface;
16
use Sylius\Component\Core\Model\PaymentMethodInterface;
17
use Symfony\Component\Validator\Constraint;
18
use Symfony\Component\Validator\ConstraintValidator;
19
use Webmozart\Assert\Assert;
20
21
final class CurrencyValidator extends ConstraintValidator
22
{
23
    public function validate($paymentMethod, Constraint $constraint): void
24
    {
25
        Assert::isInstanceOf($paymentMethod, PaymentMethodInterface::class);
26
27
        Assert::isInstanceOf($constraint, Currency::class);
28
29
        $gatewayConfig = $paymentMethod->getGatewayConfig();
30
31
        if (
32
            null === $gatewayConfig ||
33
            (
34
                $gatewayConfig->getFactoryName() !== MollieSubscriptionGatewayFactory::FACTORY_NAME
35
            )
36
        ) {
37
            return;
38
        }
39
40
        /** @var ChannelInterface $channel */
41
        foreach ($paymentMethod->getChannels() as $channel) {
42
            if (
43
                null === $channel->getBaseCurrency() ||
44
                false === in_array(strtoupper($channel->getBaseCurrency()->getCode()), MollieSubscriptionGatewayFactory::CURRENCIES_AVAILABLE)
45
            ) {
46
                $message = $constraint->message ?? null;
47
48
                $this->context->buildViolation($message, [
0 ignored issues
show
Bug introduced by
It seems like $message can also be of type null; however, parameter $message of Symfony\Component\Valida...rface::buildViolation() 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

48
                $this->context->buildViolation(/** @scrutinizer ignore-type */ $message, [
Loading history...
49
                    '{{ currencies }}' => implode(', ', MollieSubscriptionGatewayFactory::CURRENCIES_AVAILABLE),
50
                ])->atPath('channels')->addViolation();
51
52
                return;
53
            }
54
        }
55
    }
56
}
57