123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?php
- namespace DeviceBundle\Twig;
- use WebserviceBundle\Services\Webservice;
- class DeviceLogExtension extends \Twig_Extension
- {
- /**
- * @var Webservice
- */
- protected $webservice;
- /**
- * @var string
- */
- protected $deviceGetUrl;
- /**
- * @var string
- */
- protected $deviceLogGetUrl;
- /**
- * @param Webservice $webservice
- * @param string $deviceGetUrl
- * @param string $deviceLogGetUrl
- */
- public function __construct(Webservice $webservice, $deviceGetUrl, $deviceLogGetUrl)
- {
- $this->webservice = $webservice;
- $this->deviceGetUrl = $deviceGetUrl;
- $this->deviceLogGetUrl = $deviceLogGetUrl;
- }
- /**
- * @return array
- */
- public function getFunctions()
- {
- return array(
- new \Twig_SimpleFunction('device_log', array($this, 'getDeviceLog')),
- );
- }
- /**
- * @param Entity $object
- * @param int $limit
- * @param int $offset
- *
- * @return array
- */
- public function getDeviceLog($object, $limit = 50, $offset = 0)
- {
- $filters = array(
- 'deviceType' => get_class($object),
- 'deviceId' => $object->getId(),
- 'disableTenancy' => true
- );
-
- $device = $this->webservice->get($this->deviceGetUrl, $filters);
-
- $log = '';
- if (count($device) && isset($device[0])) {
- $filters = array(
- 'device' => $device[0]['id'],
- );
- // $order = array(
- // 'created' => 'DESC'
- // );
- $deviceLogs = $this->webservice->get($this->deviceLogGetUrl, $filters, array(), $limit, $offset);
-
- foreach ($deviceLogs as $deviceLog) {
- unset($deviceLog['device']);
- $created = new \DateTime($deviceLog['created']);
- $log .= $created->format('d/m/Y H:i') . ' - ' . $deviceLog['message'] . PHP_EOL;
- }
- }
- return $log;
- }
- /**
- * @return string
- */
- public function getName()
- {
- return 'device_log_extension';
- }
- }
|