From b9bbce70e2cfd3b013ffde4572873a005f64e65a Mon Sep 17 00:00:00 2001 From: Ivan Lutokhin Date: Wed, 15 Jul 2020 18:31:28 +0300 Subject: [PATCH] Added validators --- Exception/RetailCrmApiException.php | 7 ++ Resources/translations/validators.en.yaml | 11 +++ Resources/translations/validators.es.yaml | 11 +++ Resources/translations/validators.ru.yaml | 11 +++ Service/ModuleManagerInterface.php | 2 + .../Constraints/IntegrationModuleAccess.php | 23 ++++++ .../IntegrationModuleAccessValidator.php | 44 ++++++++++ Validator/Constraints/RetailCrmAccess.php | 23 ++++++ .../Constraints/RetailCrmAccessValidator.php | 80 +++++++++++++++++++ composer.json | 5 +- 10 files changed, 215 insertions(+), 2 deletions(-) create mode 100644 Exception/RetailCrmApiException.php create mode 100644 Resources/translations/validators.en.yaml create mode 100644 Resources/translations/validators.es.yaml create mode 100644 Resources/translations/validators.ru.yaml create mode 100644 Validator/Constraints/IntegrationModuleAccess.php create mode 100644 Validator/Constraints/IntegrationModuleAccessValidator.php create mode 100644 Validator/Constraints/RetailCrmAccess.php create mode 100644 Validator/Constraints/RetailCrmAccessValidator.php diff --git a/Exception/RetailCrmApiException.php b/Exception/RetailCrmApiException.php new file mode 100644 index 0000000..5eeaf5c --- /dev/null +++ b/Exception/RetailCrmApiException.php @@ -0,0 +1,7 @@ +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() + ; + } + } +} diff --git a/Validator/Constraints/RetailCrmAccess.php b/Validator/Constraints/RetailCrmAccess.php new file mode 100644 index 0000000..74811f9 --- /dev/null +++ b/Validator/Constraints/RetailCrmAccess.php @@ -0,0 +1,23 @@ +moduleManager = $moduleManager; + } + + public function validate($account, Constraint $constraint) + { + if (!($constraint instanceof RetailCrmAccess)) { + throw new UnexpectedTypeException($constraint, RetailCrmAccess::class); + } + + $client = $this->moduleManager->getRetailCrmClient(); + + try { + $response = $client->request->credentials(); + if (!$response->isSuccessful()) { + throw new RetailCrmApiException($response->offsetGet('errorMsg')); + } + + $credentials = $response->offsetGet('credentials'); + foreach ($constraint->requiredApiMethods as $method) { + if (!in_array($method, $credentials)) { + $this->context + ->buildViolation('retailcrm_access.access_denied', ['%method%' => $method]) + ->atPath('crmApiKey') + ->addViolation() + ; + } + } + } catch (CurlException $e) { + $this->context + ->buildViolation('retailcrm_access.curl_exception') + ->atPath('crmUrl') + ->addViolation() + ; + } catch (LimitException $e) { + $this->context + ->buildViolation('retailcrm_access.service_unavailable') + ->atPath('crmUrl') + ->addViolation() + ; + } catch (InvalidJsonException $e) { + $this->context + ->buildViolation('retailcrm_access.invalid_json') + ->atPath('crmUrl') + ->addViolation() + ; + } catch (\InvalidArgumentException $e) { + $this->context + ->buildViolation('retailcrm_access.requires_https') + ->atPath('crmUrl') + ->addViolation() + ; + } catch (RetailCrmApiException $e) { + $this->context + ->buildViolation('retailcrm_access.wrong_api_key') + ->atPath('crmUrl') + ->addViolation() + ; + } + } +} diff --git a/composer.json b/composer.json index 4fe2a89..80d4d99 100644 --- a/composer.json +++ b/composer.json @@ -24,9 +24,10 @@ "symfony/framework-bundle": "^3.4|^4.0|^5.1", "symfony/lock": "^5.1", "symfony/routing": "^5.1", - "symfony/translation": "^5.1" + "symfony/translation": "^5.1", + "symfony/validator": "^5.1" }, "require-dev": { "doctrine/doctrine-fixtures-bundle": "^3.3" } -} +} \ No newline at end of file