diff --git a/application/controllers/HostsController.php b/application/controllers/HostsController.php index 3b7bfac8..2efb1b3b 100644 --- a/application/controllers/HostsController.php +++ b/application/controllers/HostsController.php @@ -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); } diff --git a/library/Eagle/Widget/HostList.php b/library/Eagle/Widget/HostList.php index 2b3e2023..63e55667 100644 --- a/library/Eagle/Widget/HostList.php +++ b/library/Eagle/Widget/HostList.php @@ -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; } } diff --git a/library/Eagle/Widget/StateList.php b/library/Eagle/Widget/StateList.php new file mode 100644 index 00000000..dea3a645 --- /dev/null +++ b/library/Eagle/Widget/StateList.php @@ -0,0 +1,70 @@ +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(); + } +}