123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- <?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;
- }
- }
|