Passed
Push — master ( aa303d...e332d7 )
by Antonio
04:02
created

CurrencyValidator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 10
c 0
b 0
f 0
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
C validate() 0 26 7
1
<?php
2
3
namespace Locastic\SyliusHTPayWayPlugin\Validator\Constraints;
4
5
use Locastic\SyliusHTPayWayPlugin\HTPayWayFactoryInterface;
6
use Locastic\SyliusHTPayWayPlugin\HTPayWayOffsiteGatewayFactory;
7
use Sylius\Component\Core\Model\ChannelInterface;
8
use Sylius\Component\Core\Model\PaymentMethodInterface;
9
use Symfony\Component\Validator\Constraint;
10
use Symfony\Component\Validator\ConstraintValidator;
11
use Webmozart\Assert\Assert;
12
13
14
final class CurrencyValidator extends ConstraintValidator
15
{
16
    public function validate($paymentMethod, Constraint $constraint): void
17
    {
18
        Assert::isInstanceOf($paymentMethod, PaymentMethodInterface::class);
19
        Assert::isInstanceOf($constraint, Currency::class);
20
21
        $gatewayConfig = $paymentMethod->getGatewayConfig();
22
23
        if (null === $gatewayConfig || $gatewayConfig->getFactoryName(
24
            ) !== HTPayWayOffsiteGatewayFactory::FACTORY_NAME) {
25
            return;
26
        }
27
28
        /** @var ChannelInterface $channel */
29
        foreach ($paymentMethod->getChannels() as $channel) {
30
            if (
31
                null === $channel->getBaseCurrency() ||
32
                false === in_array(
33
                    strtoupper($channel->getBaseCurrency()->getCode()),
34
                    HTPayWayFactoryInterface::CURRENCIES_AVAILABLE
35
                )
36
            ) {
37
                $message = isset($constraint->message) ? $constraint->message : null;
38
39
                $this->context->buildViolation($message)->atPath('channels')->addViolation();
40
41
                return;
42
            }
43
        }
44
    }
45
}
46