|
| 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 | + private const REQUIRED_METHODS = [ |
| 17 | + '/api/integration-modules/{code}', |
| 18 | + '/api/integration-modules/{code}/edit', |
| 19 | + '/api/delivery/generic/setting/{code}', |
| 20 | + '/api/delivery/generic/setting/{code}/edit', |
| 21 | + '/api/delivery/generic/{code}/tracking', |
| 22 | + '/api/delivery/shipments', |
| 23 | + '/api/delivery/shipments/{id}', |
| 24 | + '/api/delivery/shipments/create', |
| 25 | + '/api/delivery/shipments/{id}/edit', |
| 26 | + '/api/reference/sites', |
| 27 | + '/api/reference/stores', |
| 28 | + '/api/reference/payment-types', |
| 29 | + '/api/reference/statuses', |
| 30 | + ]; |
| 31 | + |
| 32 | + /** @var ModuleManagerInterface */ |
| 33 | + private $moduleManager; |
| 34 | + |
| 35 | + public function __construct(ModuleManagerInterface $moduleManager) |
| 36 | + { |
| 37 | + $this->moduleManager = $moduleManager; |
| 38 | + } |
| 39 | + |
| 40 | + public function validate($account, Constraint $constraint) |
| 41 | + { |
| 42 | + if (!($constraint instanceof RetailCrmAccess)) { |
| 43 | + throw new UnexpectedTypeException($constraint, RetailCrmAccess::class); |
| 44 | + } |
| 45 | + |
| 46 | + $client = $this->moduleManager->getRetailCrmClient(); |
| 47 | + |
| 48 | + try { |
| 49 | + $response = $client->request->credentials(); |
| 50 | + if (!$response->isSuccessful()) { |
| 51 | + throw new RetailCrmApiException($response->offsetGet('errorMsg')); |
| 52 | + } |
| 53 | + |
| 54 | + $credentials = $response->offsetGet('credentials'); |
| 55 | + foreach (static::REQUIRED_METHODS as $method) { |
| 56 | + if (!in_array($method, $credentials)) { |
| 57 | + $this->context |
| 58 | + ->buildViolation('retailcrm_access.access_denied', ['%method%' => $method]) |
| 59 | + ->atPath('crmApiKey') |
| 60 | + ->addViolation() |
| 61 | + ; |
| 62 | + } |
| 63 | + } |
| 64 | + } catch (CurlException $e) { |
| 65 | + $this->context |
| 66 | + ->buildViolation('retailcrm_access.curl_exception') |
| 67 | + ->atPath('crmUrl') |
| 68 | + ->addViolation() |
| 69 | + ; |
| 70 | + } catch (LimitException $e) { |
| 71 | + $this->context |
| 72 | + ->buildViolation('retailcrm_access.service_unavailable') |
| 73 | + ->atPath('crmUrl') |
| 74 | + ->addViolation() |
| 75 | + ; |
| 76 | + } catch (InvalidJsonException $e) { |
| 77 | + $this->context |
| 78 | + ->buildViolation('retailcrm_access.invalid_json') |
| 79 | + ->atPath('crmUrl') |
| 80 | + ->addViolation() |
| 81 | + ; |
| 82 | + } catch (\InvalidArgumentException $e) { |
| 83 | + $this->context |
| 84 | + ->buildViolation('retailcrm_access.requires_https') |
| 85 | + ->atPath('crmUrl') |
| 86 | + ->addViolation() |
| 87 | + ; |
| 88 | + } catch (RetailCrmApiException $e) { |
| 89 | + $this->context |
| 90 | + ->buildViolation('retailcrm_access.wrong_api_key') |
| 91 | + ->atPath('crmUrl') |
| 92 | + ->addViolation() |
| 93 | + ; |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments