|
@@ -0,0 +1,52 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace ValidatorsBundle\Validator;
|
|
|
+
|
|
|
+use Symfony\Component\Validator\Constraint;
|
|
|
+use Symfony\Component\Validator\ConstraintValidator;
|
|
|
+use WebserviceBundle\Services\Webservice;
|
|
|
+
|
|
|
+class DeviceValidator extends ConstraintValidator
|
|
|
+{
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var string
|
|
|
+ */
|
|
|
+ private $message = 'error.device_create';
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @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;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param object $entity
|
|
|
+ * @param Constraint $constraint
|
|
|
+ */
|
|
|
+ public function validate($entity, Constraint $constraint)
|
|
|
+ {
|
|
|
+ $result = $this->webservice->makeGetRequest($this->deviceCheckUrl);
|
|
|
+ $data = json_decode($result, true);
|
|
|
+ if ($data['result'] == false) {
|
|
|
+ $this->context->buildViolation($constraint->message ? $constraint->message : $this->message)
|
|
|
+ ->addViolation();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|