DeviceValidator.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. }
  25. /**
  26. * @param object $entity
  27. * @param Constraint $constraint
  28. */
  29. public function validate($entity, Constraint $constraint)
  30. {
  31. $result = $this->webservice->makeGetRequest($this->deviceCheckUrl);
  32. $data = json_decode($result, true);
  33. if (is_null($entity->getId()) && isset($data['result']) && $data['result'] == false) {
  34. $this->context->buildViolation($constraint->message)
  35. ->addViolation();
  36. }
  37. }
  38. }