summaryrefslogtreecommitdiffstats
path: root/library/Icingadb/Widget/CheckAttempt.php
blob: cf12de346aac6bd27f0843e537a8e7c631d19f54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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());
            }
        }
    }
}