Use a common base class for volatile stateful lists

This commit is contained in:
Johannes Meyer 2019-10-02 10:56:51 +02:00 committed by Eric Lippmann
parent 9ad4c40f14
commit bec94c7be8
3 changed files with 75 additions and 60 deletions

View file

@ -3,8 +3,6 @@
namespace Icinga\Module\Eagle\Controllers;
use Icinga\Module\Eagle\Model\Host;
use Icinga\Module\Eagle\Model\HostState;
use Icinga\Module\Eagle\Redis\VolatileHostState;
use Icinga\Module\Eagle\Web\Controller;
use Icinga\Module\Eagle\Widget\HostList;
@ -12,7 +10,6 @@ class HostsController extends Controller
{
public function indexAction()
{
$this->setTitle($this->translate('Hosts'));
$db = $this->getDb();
@ -21,7 +18,7 @@ class HostsController extends Controller
$hosts->limit(25);
$hostList = (new HostList($hosts))
->setVolatileState(new VolatileHostState($this->getRedis()));
->setRedis($this->getRedis());
$this->addContent($hostList);
}

View file

@ -2,69 +2,17 @@
namespace Icinga\Module\Eagle\Widget;
use Icinga\Module\Eagle\Redis\VolatileState;
use ipl\Html\BaseHtmlElement;
/**
* Host list.
* Host list
*/
class HostList extends BaseHtmlElement
class HostList extends StateList
{
protected $tag = 'ul';
protected $defaultAttributes = ['class' => 'object-list', 'data-base-target' => '_next'];
/** @var iterable Data source of the list */
protected $hosts;
/** @var VolatileState $volatileState Helper to fetch volatile states */
protected $volatileState;
/**
* Create a new host list
*
* @param iterable $hosts Data source of the list
*/
public function __construct($hosts)
protected function getItemClass()
{
if (! is_iterable($hosts)) {
throw new \InvalidArgumentException('Data must be an array or an instance of Traversable');
}
$this->hosts = $hosts;
}
/**
* Get the helper to fetch volatile states
*
* @return VolatileState
*/
public function getVolatileState()
{
return $this->volatileState;
}
/**
* Set the helper to fetch volatile states
*
* @param VolatileState $volatileState
*
* @return $this
*/
public function setVolatileState(VolatileState $volatileState)
{
$this->volatileState = $volatileState;
return $this;
}
protected function assemble()
{
foreach ($this->hosts as $host) {
$this->volatileState->add($host);
$this->add(new HostListItem($host));
}
$this->volatileState->fetch();
return HostListItem::class;
}
}

View file

@ -0,0 +1,70 @@
<?php
namespace Icinga\Module\Eagle\Widget;
use InvalidArgumentException;
use Redis;
use Icinga\Module\Eagle\Redis\VolatileState;
use ipl\Html\BaseHtmlElement;
abstract class StateList extends BaseHtmlElement
{
/** @var iterable Data source of the list */
protected $data;
/** @var Redis Redis connection to fetch volatile states from */
protected $redis;
/**
* Create a new state list
*
* @param iterable $data Data source of the list
*/
public function __construct($data)
{
if (! is_iterable($data)) {
throw new InvalidArgumentException('Data must be an array or an instance of Traversable');
}
$this->data = $data;
}
/**
* Set the Redis connection to fetch volatile states from
*
* @param Redis $redis
*
* @return $this
*/
public function setRedis(Redis $redis)
{
$this->redis = $redis;
return $this;
}
abstract protected function getItemClass();
/**
* Get the helper to fetch volatile states
*
* @return VolatileState
*/
public function getVolatileState()
{
return new VolatileState($this->redis);
}
protected function assemble()
{
$itemClass = $this->getItemClass();
$volatileState = $this->getVolatileState();
foreach ($this->data as $object) {
$volatileState->add($object);
$this->add(new $itemClass($object));
}
$volatileState->fetch();
}
}