Ver código fonte

DeviceInterface

Guillermo Espinoza 8 anos atrás
pai
commit
0469a3768c

+ 51 - 0
EventListener/DeviceListener.php

@@ -0,0 +1,51 @@
+<?php
+
+namespace DeviceBundle\EventListener;
+
+use Doctrine\ORM\Event\LifecycleEventArgs;
+use DeviceBundle\Interfaces\DeviceInterface;
+use WebserviceBundle\Services\Webservice;
+use Buzz\Message\RequestInterface as HttpRequestInterface;
+
+class DeviceListener
+{
+
+    /**
+     * @var Webservice
+     */
+    private $webservice;
+
+    /**
+     * @var string
+     */
+    private $devicePostUrl;
+
+
+    /**
+     * @param Webservice $webservice
+     * @param string $devicePostUrl
+     */
+    public function __construct(Webservice $webservice, $devicePostUrl)
+    {
+        $this->webservice = $webservice;
+        $this->devicePostUrl = $devicePostUrl;
+    }
+
+    /**
+     * @param LifecycleEventArgs $args
+     */
+    public function postPersist(LifecycleEventArgs $args)
+    {
+        $entity = $args->getEntity();
+        if ($entity instanceof DeviceInterface) {
+            $data = array(
+                'deviceType' => get_class($entity),
+                'deviceId' => $entity->getId(),
+                'ip' => $entity->getIp(),
+            );
+            
+            $result = $this->webservice->makeGetRequest($this->devicePostUrl, HttpRequestInterface::METHOD_POST, $data);
+        }
+    }
+
+}

+ 7 - 0
Interfaces/DeviceInterface.php

@@ -0,0 +1,7 @@
+<?php
+
+namespace DeviceBundle\Interfaces;
+
+interface DeviceInterface
+{
+}

+ 6 - 1
Resources/config/services.yml

@@ -3,4 +3,9 @@ services:
         class: DeviceBundle\Validator\Constraints\DeviceValidator
         arguments: ["@webservice","%device_check_url%"]
         tags:
-            - { name: validator.constraint_validator }
+            - { name: validator.constraint_validator }
+    device.device_listener:
+        class: DeviceBundle\EventListener\DeviceListener
+        arguments: ["@webservice","%device_post_url%"]
+        tags:
+            - { name: doctrine.event_listener, event: postPersist }