12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?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;
- /**
- * @var string
- */
- private $deviceDeletePostUrl;
- /**
- * @param Webservice $webservice
- * @param string $devicePostUrl
- * @param string $deviceDeletePostUrl
- */
- public function __construct(Webservice $webservice, $devicePostUrl, $deviceDeletePostUrl)
- {
- $this->webservice = $webservice;
- $this->devicePostUrl = $devicePostUrl;
- $this->deviceDeletePostUrl = $deviceDeletePostUrl;
- }
- /**
- * @param LifecycleEventArgs $args
- */
- public function postPersist(LifecycleEventArgs $args)
- {
- $this->send($args, $this->devicePostUrl, HttpRequestInterface::METHOD_POST);
- }
- /**
- * @param LifecycleEventArgs $args
- */
- public function preRemove(LifecycleEventArgs $args)
- {
- $this->send($args, $this->deviceDeletePostUrl, HttpRequestInterface::METHOD_DELETE);
- }
- /**
- * @param LifecycleEventArgs $arg
- * @param string $url
- * @param string $method
- */
- private function send(LifecycleEventArgs $args, $url, $method)
- {
- $entity = $args->getEntity();
- if ($entity instanceof DeviceInterface) {
- $data = array(
- 'deviceType' => get_class($entity),
- 'deviceId' => $entity->getId(),
- 'ip' => $entity->getIp(),
- );
- return $this->webservice->makeGetRequest($url, $method, $data);
- }
- }
- }
|