|
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\ArrayCollection; |
|
18
|
|
|
use Doctrine\Common\Collections\Collection; |
|
19
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
|
20
|
|
|
use Sylius\Component\Core\Model\PaymentMethodInterface; |
|
21
|
|
|
use Symfony\Component\Validator\Constraint; |
|
22
|
|
|
use Symfony\Component\Validator\ConstraintValidator as ConstraintValidatorAlias; |
|
23
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
24
|
|
|
|
|
25
|
|
|
final class PaymentMethodMollieChannelUniqueValidator extends ConstraintValidatorAlias |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var PaymentMethodRepositoryInterface */ |
|
28
|
|
|
private $paymentMethodRepository; |
|
29
|
|
|
|
|
30
|
|
|
/** @var TranslatorInterface */ |
|
31
|
|
|
private $translator; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct( |
|
34
|
|
|
PaymentMethodRepositoryInterface $paymentMethodRepository, |
|
35
|
|
|
TranslatorInterface $translator |
|
36
|
|
|
) { |
|
37
|
|
|
$this->paymentMethodRepository = $paymentMethodRepository; |
|
38
|
|
|
$this->translator = $translator; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function validate($value, Constraint $constraint): void |
|
42
|
|
|
{ |
|
43
|
|
|
if ($value instanceof PaymentMethodInterface && null !== $value->getCode()) { |
|
44
|
|
|
false === $this->isMolliePaymentMethod($value) ?: $this->validateMolliePaymentMethod($value, $constraint); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
private function validateMolliePaymentMethod(PaymentMethodInterface $paymentMethod, Constraint $constraint): void |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
|
|
$molliePaymentMethods = $this->paymentMethodRepository->findAllByFactoryNameAndCode($paymentMethod->getCode()); |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
if (0 === count($molliePaymentMethods)) { |
|
53
|
|
|
return; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$alreadyUsedChannels = $this->getAlreadyUsedChannels($molliePaymentMethods); |
|
57
|
|
|
|
|
58
|
|
|
if ($this->isTheSameChannel($paymentMethod->getChannels(), $alreadyUsedChannels)) { |
|
59
|
|
|
$translation = $this->translator->trans('bitbag_sylius_mollie_plugin.form.channel_should_be_unique', [ |
|
60
|
|
|
'{channels}' => $this->getChannelsNameByChannels($alreadyUsedChannels), |
|
61
|
|
|
]); |
|
62
|
|
|
|
|
63
|
|
|
$this->context->buildViolation($translation)->atPath('channels')->addViolation(); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
private function getAlreadyUsedChannels(array $molliePaymentMethods): Collection |
|
68
|
|
|
{ |
|
69
|
|
|
$alreadyUsedChannels = new ArrayCollection(); |
|
70
|
|
|
|
|
71
|
|
|
/** @var PaymentMethodInterface $molliePaymentMethod */ |
|
72
|
|
|
foreach ($molliePaymentMethods as $molliePaymentMethod) { |
|
73
|
|
|
/** @var ChannelInterface $channel */ |
|
74
|
|
|
foreach ($molliePaymentMethod->getChannels() as $channel) { |
|
75
|
|
|
if (!$alreadyUsedChannels->contains($channel)) { |
|
76
|
|
|
$alreadyUsedChannels->add($channel); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $alreadyUsedChannels; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
private function isTheSameChannel(Collection $newChannels, Collection $paymentMethodExistingChannels): bool |
|
85
|
|
|
{ |
|
86
|
|
|
foreach ($paymentMethodExistingChannels as $paymentMethodExistingChannel) { |
|
87
|
|
|
if ($newChannels->contains($paymentMethodExistingChannel)) { |
|
88
|
|
|
return true; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
private function isMolliePaymentMethod(PaymentMethodInterface $paymentMethod): bool |
|
96
|
|
|
{ |
|
97
|
|
|
/** @var GatewayConfigInterface $gateway */ |
|
98
|
|
|
$gateway = $paymentMethod->getGatewayConfig(); |
|
99
|
|
|
|
|
100
|
|
|
return $gateway->getFactoryName() === MollieGatewayFactory::FACTORY_NAME; |
|
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
private function getChannelsNameByChannels(Collection $alreadyUsedChannels): string |
|
104
|
|
|
{ |
|
105
|
|
|
$channelsNames = ''; |
|
106
|
|
|
|
|
107
|
|
|
/** @var ChannelInterface $channel */ |
|
108
|
|
|
foreach ($alreadyUsedChannels as $channel) { |
|
109
|
|
|
$channelsNames .= \sprintf('%s ', $channel->getName()); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $channelsNames; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.