DeviceValidator.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. {
  42. $this->enabled = $enable;
  43. }
  44. /**
  45. * Se utiliza para las pruebas de phpunit
  46. * @param Webservice $webservice
  47. */
  48. public function setWebservice($webservice)
  49. {
  50. $this->webservice = $webservice;
  51. }
  52. }