1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- <?php
- namespace DeviceBundle\Validator\Constraints;
- use Symfony\Component\Validator\Constraint;
- use Symfony\Component\Validator\ConstraintValidator;
- use WebserviceBundle\Services\Webservice;
- class DeviceValidator extends ConstraintValidator
- {
- /**
- * @var string
- */
- private $deviceCheckUrl;
- /**
- * @var Webservice
- */
- private $webservice;
- /**
- * @param Webservice $webservice
- * @param string $deviceCheckUrl
- */
- public function __construct(Webservice $webservice, $deviceCheckUrl)
- {
- $this->webservice = $webservice;
- $this->deviceCheckUrl = $deviceCheckUrl;
- $this->enabled = true;
- }
- /**
- * @param object $entity
- * @param Constraint $constraint
- */
- public function validate($entity, Constraint $constraint)
- {
- if (!$this->enabled) return;
- $result = $this->webservice->makeGetRequest($this->deviceCheckUrl);
- $data = json_decode($result, true);
- if (is_null($entity->getId()) && isset($data['result']) && $data['result'] == false) {
- $this->context->buildViolation($constraint->message)
- ->addViolation();
- }
- }
- function remoteCheck($enable)
- {
- $this->enabled = $enable;
- }
- /**
- * Se utiliza para las pruebas de phpunit
- * @param Webservice $webservice
- */
- public function setWebservice($webservice)
- {
- $this->webservice = $webservice;
- }
- }
|