DeviceValidator.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace DeviceBundle\Validator\Constraints;
  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 $deviceCheckUrl;
  12. /**
  13. * @var Webservice
  14. */
  15. private $webservice;
  16. /**
  17. * @param Webservice $webservice
  18. * @param string $deviceCheckUrl
  19. */
  20. public function __construct(Webservice $webservice, $deviceCheckUrl)
  21. {
  22. $this->webservice = $webservice;
  23. $this->deviceCheckUrl = $deviceCheckUrl;
  24. $this->enabled = true;
  25. }
  26. /**
  27. * @param object $entity
  28. * @param Constraint $constraint
  29. */
  30. public function validate($entity, Constraint $constraint)
  31. {
  32. if(!$this->enabled) return;
  33. $result = $this->webservice->makeGetRequest($this->deviceCheckUrl);
  34. $data = json_decode($result, true);
  35. if (is_null($entity->getId()) && isset($data['result']) && $data['result'] == false) {
  36. $this->context->buildViolation($constraint->message)
  37. ->addViolation();
  38. }
  39. }
  40. function remoteCheck($enable){
  41. $this->enable = $enable;
  42. }
  43. }