Guillermo Espinoza пре 8 година
комит
4b8e3d3be1

+ 65 - 0
README.md

@@ -0,0 +1,65 @@
+# ValidatorsBundle
+
+- [Installation](#installation)
+- [Validator](#validators)
+
+## Installation
+
+**composer.json**:
+
+```javascript
+"repositories": [
+    {
+        "type": "vcs",
+        "url":  "ssh://git@infra.flowdat.com:222/VendorSoftwareFlowdat3/ValidatorsBundle.git",
+        "options": {
+                "local_pk": "./keys/bitbucket.id_rsa"
+        }
+    }
+],
+"require": {
+    "ik/validators-bundle": "dev-master"
+},
+```
+
+**app/AppKernel.php**:
+
+```php
+public function registerBundles()
+{
+    $bundles = [
+        new ValidatorsBundle\ValidatorsBundle(),
+    ];
+    .
+    .
+}
+```
+
+**app/config/config.yml**:
+
+```yml
+imports:
+    - { resource: "@ValidatorsBundle/Resources/config/services.yml" }
+```
+
+## Validators
+
+
+- **ValidatorsBundle\Validator\DeviceValidator**: Validator que chequea si se puede agregar un dispositivo dependiendo de la licencia actual. Para agregarlo por ej en la entidad OLT:
+
+```php
+
+use ValidatorsBundle\Validator as Validator;
+
+...
+
+/**
+ * @ORM\Entity
+ * @UniqueEntity("ip")
+ * 
+ * @Validator\DeviceValidator
+ */
+class OLT
+{
+
+```

+ 6 - 0
Resources/config/services.yml

@@ -0,0 +1,6 @@
+services:
+    validators.device_validator:
+        class: ValidatorsBundle\Validator\DeviceValidator
+        arguments: ["@webservice","%device_check_url%"]
+        tags:
+            - { name: validator.constraint_validator }

+ 2 - 0
Resources/translations/validators.es.yml

@@ -0,0 +1,2 @@
+error:
+    device_create: Se produjo un error al crear. Se alcanzo el máximo de dispositivos en la licencia

+ 52 - 0
Validator/DeviceValidator.php

@@ -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();
+        }
+    }
+
+}

+ 9 - 0
ValidatorsBundle.php

@@ -0,0 +1,9 @@
+<?php
+
+namespace ValidatorsBundle;
+
+use Symfony\Component\HttpKernel\Bundle\Bundle;
+
+class ValidatorsBundle extends Bundle
+{
+}

+ 16 - 0
composer.json

@@ -0,0 +1,16 @@
+{
+    "name": "ik/validators-bundle",
+    "description": "Flowdat 3 Validators Bundle",
+    "keywords": ["Admin Generator", "admin", "validators", "bundle"],
+    "autoload": {
+        "psr-4": { "ValidatorsBundle\\": "" }
+    },
+    "repositories": [
+        {
+            "type": "vcs",
+            "url": "ssh://git@infra.flowdat.com:222/VendorSoftwareFlowdat3/ValidatorsBundle.git"
+        }
+    ],
+    "version": "1.0",
+    "minimum-stability": "stable"
+}