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()); |
|
|
|
|
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; |
|
|
|
|
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|