Skip to content

Commit

Permalink
Added base value validation
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioperic committed May 15, 2018
1 parent aa303d0 commit e332d7c
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/HTPayWayFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Locastic\SyliusHTPayWayPlugin;

interface HTPayWayFactoryInterface
{
public const LOCALES_AVAILABLE = ['en_US', 'hr_HR', 'de_CH', 'de_DE', 'fr_BE', 'fr_FR', 'it_IT', 'ru_RU'];

public const CURRENCIES_AVAILABLE = ['HRK'];
}
2 changes: 1 addition & 1 deletion src/HTPayWayOffsiteGatewayFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Payum\Core\Bridge\Spl\ArrayObject;
use Locastic\TcomPayWay\AuthorizeForm\Model\Payment as PaymentOffsite;

final class HTPayWayOffsiteGatewayFactory extends GatewayFactory
final class HTPayWayOffsiteGatewayFactory extends GatewayFactory implements HTPayWayFactoryInterface
{
const FACTORY_NAME = 'ht_payway_offsite';

Expand Down
5 changes: 5 additions & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
services:
locastic.sylius_ht_payway_plugin.validator.currency:
class: Locastic\SyliusHTPayWayPlugin\Validator\Constraints\CurrencyValidator
tags:
- { name: validator.constraint_validator, alias: locastic_sylius_ht_payway_plugin_currency }

locastic.sylius_ht_payway_plugin.form.type.ht_payway_gateway_configuration:
class: Locastic\SyliusHTPayWayPlugin\Form\Type\HTPayWayOffsiteGatewayConfigurationType
tags:
Expand Down
5 changes: 5 additions & 0 deletions src/Resources/config/validation/PaymentMethod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Sylius\Component\Core\Model\PaymentMethod:
constraints:
- Locastic\SyliusHTPayWayPlugin\Validator\Constraints\Currency:
groups: ['sylius']
message: 'locastic.sylius_ht_payway_plugin.channel.required_currency'
4 changes: 3 additions & 1 deletion src/Resources/translations/validators.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ locastic:
sylius_ht_payway_plugin:
form:
shop_id_not_blank: Shop ID cannot be blank
secret_key_not_blank: Secret key cannot be blank
secret_key_not_blank: Secret key cannot be blank
channel:
required_currency: The base currency of the channel must be HRK.
4 changes: 3 additions & 1 deletion src/Resources/translations/validators.hr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ locastic:
sylius_ht_payway_plugin:
form:
shop_id_not_blank: Shop ID ne može biti prazan
secret_key_not_blank: Tajni ključ ne može biti prazan
secret_key_not_blank: Tajni ključ ne može biti prazan
channel:
required_currency: Osnovna vrijednost mora biti u HRK.
20 changes: 20 additions & 0 deletions src/Validator/Constraints/Currency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Locastic\SyliusHTPayWayPlugin\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

final class Currency extends Constraint
{
public $message;

public function validatedBy(): string
{
return 'locastic_sylius_ht_payway_plugin_currency';
}

public function getTargets(): string
{
return self::CLASS_CONSTRAINT;
}
}
45 changes: 45 additions & 0 deletions src/Validator/Constraints/CurrencyValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Locastic\SyliusHTPayWayPlugin\Validator\Constraints;

use Locastic\SyliusHTPayWayPlugin\HTPayWayFactoryInterface;
use Locastic\SyliusHTPayWayPlugin\HTPayWayOffsiteGatewayFactory;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Webmozart\Assert\Assert;


final class CurrencyValidator extends ConstraintValidator
{
public function validate($paymentMethod, Constraint $constraint): void
{
Assert::isInstanceOf($paymentMethod, PaymentMethodInterface::class);
Assert::isInstanceOf($constraint, Currency::class);

$gatewayConfig = $paymentMethod->getGatewayConfig();

if (null === $gatewayConfig || $gatewayConfig->getFactoryName(
) !== HTPayWayOffsiteGatewayFactory::FACTORY_NAME) {
return;
}

/** @var ChannelInterface $channel */
foreach ($paymentMethod->getChannels() as $channel) {
if (
null === $channel->getBaseCurrency() ||
false === in_array(
strtoupper($channel->getBaseCurrency()->getCode()),
HTPayWayFactoryInterface::CURRENCIES_AVAILABLE
)
) {
$message = isset($constraint->message) ? $constraint->message : null;

$this->context->buildViolation($message)->atPath('channels')->addViolation();

return;
}
}
}
}

0 comments on commit e332d7c

Please sign in to comment.