diff options
Diffstat (limited to 'library/Icingadb/Widget/CheckAttempt.php')
-rw-r--r-- | library/Icingadb/Widget/CheckAttempt.php | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/library/Icingadb/Widget/CheckAttempt.php b/library/Icingadb/Widget/CheckAttempt.php new file mode 100644 index 0000000..cf12de3 --- /dev/null +++ b/library/Icingadb/Widget/CheckAttempt.php @@ -0,0 +1,54 @@ +<?php + +/* Icinga DB Web | (c) 2020 Icinga GmbH | GPLv2 */ + +namespace Icinga\Module\Icingadb\Widget; + +use ipl\Html\BaseHtmlElement; +use ipl\Html\FormattedString; + +/** + * Visually represents the check attempts taken out of max check attempts. + */ +class CheckAttempt extends BaseHtmlElement +{ + protected $tag = 'div'; + + protected $defaultAttributes = ['class' => 'check-attempt']; + + /** @var int Current attempt */ + protected $attempt; + + /** @var int Max check attempts */ + protected $maxAttempts; + + /** + * Create a new check attempt widget + * + * @param int $attempt Current check attempt + * @param int $maxAttempts Max check attempts + */ + public function __construct(int $attempt, int $maxAttempts) + { + $this->attempt = $attempt; + $this->maxAttempts = $maxAttempts; + } + + protected function assemble() + { + if ($this->attempt == $this->maxAttempts) { + return; + } + + if ($this->maxAttempts > 5) { + $this->add(FormattedString::create('%d/%d', $this->attempt, $this->maxAttempts)); + } else { + for ($i = 0; $i < $this->attempt; ++$i) { + $this->add(new AttemptBall(true)); + } + for ($i = $this->attempt; $i < $this->maxAttempts; ++$i) { + $this->add(new AttemptBall()); + } + } + } +} |