1
0
mirror of synced 2025-01-26 02:01:40 +03:00
DeliveryModuleBundle/Validator/Constraints/IntegrationModuleAccessValidator.php

45 lines
1.5 KiB
PHP
Raw Normal View History

2020-07-15 18:31:28 +03:00
<?php
namespace RetailCrm\DeliveryModuleBundle\Validator\Constraints;
use RetailCrm\DeliveryModuleBundle\Exception\AbstractModuleException;
use RetailCrm\DeliveryModuleBundle\Exception\ServerUnreachableException;
use RetailCrm\DeliveryModuleBundle\Service\ModuleManagerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class IntegrationModuleAccessValidator extends ConstraintValidator
{
/** @var ModuleManagerInterface */
private $moduleManager;
public function __construct(ModuleManagerInterface $moduleManager)
{
$this->moduleManager = $moduleManager;
}
public function validate($account, Constraint $constraint)
{
if (!($constraint instanceof IntegrationModuleAccess)) {
throw new UnexpectedTypeException($constraint, IntegrationModuleAccess::class);
}
try {
$this->moduleManager->checkAccess();
} catch (ServerUnreachableException $e) {
$this->context
->buildViolation('integration_module_access.server_unreachable_exception')
->atPath($constraint->path)
->addViolation()
;
} catch (AbstractModuleException $e) {
$this->context
->buildViolation('integration_module_access.module_access_exception')
->atPath($constraint->path)
->addViolation()
;
}
}
}