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, [ |
|
|
|
|
49
|
|
|
'{{ currencies }}' => implode(', ', MollieSubscriptionGatewayFactory::CURRENCIES_AVAILABLE), |
50
|
|
|
])->atPath('channels')->addViolation(); |
51
|
|
|
|
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|