DeviceValidator.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace ValidatorsBundle\Validator;
  3. use Symfony\Component\Validator\Constraint;
  4. use Symfony\Component\Validator\ConstraintValidator;
  5. use WebserviceBundle\Services\Webservice;
  6. class DeviceValidator extends ConstraintValidator
  7. {
  8. /**
  9. * @var string
  10. */
  11. private $message = 'error.device_create';
  12. /**
  13. * @var string
  14. */
  15. private $deviceCheckUrl;
  16. /**
  17. * @var Webservice
  18. */
  19. private $webservice;
  20. /**
  21. * @param Webservice $webservice
  22. * @param string $deviceCheckUrl
  23. */
  24. public function __construct(Webservice $webservice, $deviceCheckUrl)
  25. {
  26. $this->webservice = $webservice;
  27. $this->deviceCheckUrl = $deviceCheckUrl;
  28. }
  29. /**
  30. * @param object $entity
  31. * @param Constraint $constraint
  32. */
  33. public function validate($entity, Constraint $constraint)
  34. {
  35. $result = $this->webservice->makeGetRequest($this->deviceCheckUrl);
  36. $data = json_decode($result, true);
  37. if ($data['result'] == false) {
  38. $this->context->buildViolation($constraint->message ? $constraint->message : $this->message)
  39. ->addViolation();
  40. }
  41. }
  42. }