Skip to content

Commit b9bbce7

Browse files
author
Ivan Lutokhin
committed
Added validators
1 parent 8fcd869 commit b9bbce7

10 files changed

Lines changed: 215 additions & 2 deletions
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace RetailCrm\DeliveryModuleBundle\Exception;
4+
5+
class RetailCrmApiException extends \Exception
6+
{
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
integration_module_access:
2+
server_unreachable_exception: 'Failed to connect to the integration server'
3+
module_access_exception: 'Invalid login or password'
4+
5+
retailcrm_access:
6+
access_denied: 'Access to the method "%method%" is denied'
7+
curl_exception: 'Failed to connect to API'
8+
service_unavailable: 'Service is temporarily unavailable'
9+
invalid_json: 'API returned a response in an unsupported format'
10+
requires_https: 'API address must start with https'
11+
wrong_api_key: 'Invalid API key'
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
integration_module_access:
2+
server_unreachable_exception: 'No se ha podido conectar al servidor de integración'
3+
module_access_exception: 'El Usuario o la contraseña no es correcta'
4+
5+
retailcrm_access:
6+
access_denied: 'Acceso al método "%method%" está prohibido'
7+
curl_exception: 'No se ha podido conectar al API'
8+
service_unavailable: 'Servicio no está disponible temporalmente'
9+
invalid_json: 'API ha devuelto la respuesta en el formato no soportado'
10+
requires_https: 'Dirección del API tiene que comenzar con https'
11+
wrong_api_key: 'El API-key no es correcto'
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
integration_module_access:
2+
server_unreachable_exception: 'Не удалось подключиться к интеграционному серверу'
3+
module_access_exception: 'Не верный логин или пароль'
4+
5+
retailcrm_access:
6+
access_denied: 'Доступ к методу "%method%" запрещен'
7+
curl_exception: 'Не удалось подключиться к API'
8+
service_unavailable: 'Сервис временно недоступен'
9+
invalid_json: 'API вернуло ответ в неподдерживаемом формате'
10+
requires_https: 'Адрес API должен начинаться с https'
11+
wrong_api_key: 'Неверный API-ключ'

Service/ModuleManagerInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public function getAccount(): ?Account;
2525

2626
public function setAccount(Account $account): self;
2727

28+
public function checkAccess(): bool;
29+
2830
public function updateModuleConfiguration(): bool;
2931

3032
public function calculateDelivery(RequestCalculate $data): array;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace RetailCrm\DeliveryModuleBundle\Validator\Constraints;
4+
5+
use Symfony\Component\Validator\Constraint;
6+
7+
/**
8+
* @Annotation
9+
* @Target({"CLASS"})
10+
*/
11+
class IntegrationModuleAccess extends Constraint
12+
{
13+
/** @var string */
14+
public $path = 'login';
15+
16+
/**
17+
* {@inheritdoc}
18+
*/
19+
public function getTargets()
20+
{
21+
return self::CLASS_CONSTRAINT;
22+
}
23+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace RetailCrm\DeliveryModuleBundle\Validator\Constraints;
4+
5+
use RetailCrm\DeliveryModuleBundle\Exception\AbstractModuleException;
6+
use RetailCrm\DeliveryModuleBundle\Exception\ServerUnreachableException;
7+
use RetailCrm\DeliveryModuleBundle\Service\ModuleManagerInterface;
8+
use Symfony\Component\Validator\Constraint;
9+
use Symfony\Component\Validator\ConstraintValidator;
10+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
11+
12+
class IntegrationModuleAccessValidator extends ConstraintValidator
13+
{
14+
/** @var ModuleManagerInterface */
15+
private $moduleManager;
16+
17+
public function __construct(ModuleManagerInterface $moduleManager)
18+
{
19+
$this->moduleManager = $moduleManager;
20+
}
21+
22+
public function validate($account, Constraint $constraint)
23+
{
24+
if (!($constraint instanceof IntegrationModuleAccess)) {
25+
throw new UnexpectedTypeException($constraint, IntegrationModuleAccess::class);
26+
}
27+
28+
try {
29+
$this->moduleManager->checkAccess();
30+
} catch (ServerUnreachableException $e) {
31+
$this->context
32+
->buildViolation('integration_module_access.server_unreachable_exception')
33+
->atPath($constraint->path)
34+
->addViolation()
35+
;
36+
} catch (AbstractModuleException $e) {
37+
$this->context
38+
->buildViolation('integration_module_access.module_access_exception')
39+
->atPath($constraint->path)
40+
->addViolation()
41+
;
42+
}
43+
}
44+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace App\Validator\Constraints;
4+
5+
use Symfony\Component\Validator\Constraint;
6+
7+
/**
8+
* @Annotation
9+
* @Target({"CLASS"})
10+
*/
11+
class RetailCrmAccess extends Constraint
12+
{
13+
/** @var array */
14+
public $requiredApiMethods = [];
15+
16+
/**
17+
* {@inheritdoc}
18+
*/
19+
public function getTargets()
20+
{
21+
return self::CLASS_CONSTRAINT;
22+
}
23+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace App\Validator\Constraints;
4+
5+
use RetailCrm\DeliveryModuleBundle\Exception\RetailCrmApiException;
6+
use RetailCrm\DeliveryModuleBundle\Service\ModuleManagerInterface;
7+
use RetailCrm\Exception\CurlException;
8+
use RetailCrm\Exception\InvalidJsonException;
9+
use RetailCrm\Exception\LimitException;
10+
use Symfony\Component\Validator\Constraint;
11+
use Symfony\Component\Validator\ConstraintValidator;
12+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
13+
14+
class RetailCrmAccessValidator extends ConstraintValidator
15+
{
16+
/** @var ModuleManagerInterface */
17+
private $moduleManager;
18+
19+
public function __construct(ModuleManagerInterface $moduleManager)
20+
{
21+
$this->moduleManager = $moduleManager;
22+
}
23+
24+
public function validate($account, Constraint $constraint)
25+
{
26+
if (!($constraint instanceof RetailCrmAccess)) {
27+
throw new UnexpectedTypeException($constraint, RetailCrmAccess::class);
28+
}
29+
30+
$client = $this->moduleManager->getRetailCrmClient();
31+
32+
try {
33+
$response = $client->request->credentials();
34+
if (!$response->isSuccessful()) {
35+
throw new RetailCrmApiException($response->offsetGet('errorMsg'));
36+
}
37+
38+
$credentials = $response->offsetGet('credentials');
39+
foreach ($constraint->requiredApiMethods as $method) {
40+
if (!in_array($method, $credentials)) {
41+
$this->context
42+
->buildViolation('retailcrm_access.access_denied', ['%method%' => $method])
43+
->atPath('crmApiKey')
44+
->addViolation()
45+
;
46+
}
47+
}
48+
} catch (CurlException $e) {
49+
$this->context
50+
->buildViolation('retailcrm_access.curl_exception')
51+
->atPath('crmUrl')
52+
->addViolation()
53+
;
54+
} catch (LimitException $e) {
55+
$this->context
56+
->buildViolation('retailcrm_access.service_unavailable')
57+
->atPath('crmUrl')
58+
->addViolation()
59+
;
60+
} catch (InvalidJsonException $e) {
61+
$this->context
62+
->buildViolation('retailcrm_access.invalid_json')
63+
->atPath('crmUrl')
64+
->addViolation()
65+
;
66+
} catch (\InvalidArgumentException $e) {
67+
$this->context
68+
->buildViolation('retailcrm_access.requires_https')
69+
->atPath('crmUrl')
70+
->addViolation()
71+
;
72+
} catch (RetailCrmApiException $e) {
73+
$this->context
74+
->buildViolation('retailcrm_access.wrong_api_key')
75+
->atPath('crmUrl')
76+
->addViolation()
77+
;
78+
}
79+
}
80+
}

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
"symfony/framework-bundle": "^3.4|^4.0|^5.1",
2525
"symfony/lock": "^5.1",
2626
"symfony/routing": "^5.1",
27-
"symfony/translation": "^5.1"
27+
"symfony/translation": "^5.1",
28+
"symfony/validator": "^5.1"
2829
},
2930
"require-dev": {
3031
"doctrine/doctrine-fixtures-bundle": "^3.3"
3132
}
32-
}
33+
}

0 commit comments

Comments
 (0)