1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?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->getData($this->deviceGetUrl, $filters);
- $log = '';
- if (count($device) && isset($device[0])) {
- $filters = array(
- 'device' => $device[0]['id'],
- );
- // $order = array(
- // 'created' => 'DESC'
- // );
- $deviceLogs = $this->webservice->getData($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';
- }
- }
|