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