BootstrapStatusClassExtension   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 80
rs 10
c 0
b 0
f 0
wmc 15

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getFunctions() 0 5 1
B getStatusColorClass() 0 20 7
B getStatusIconClass() 0 20 7
1
<?php
2
3
/**
4
 * Copyright (c) 2017 Francois-Xavier Soubirou.
5
 *
6
 * This file is part of ci-report.
7
 *
8
 * ci-report is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * ci-report is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with ci-report. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
declare(strict_types=1);
22
23
namespace App\Twig;
24
25
use App\Entity\Status;
26
use App\Entity\Test;
27
use Twig_Extension;
28
use Twig_SimpleFunction;
29
30
/**
31
 * Status class Twig extension.
32
 *
33
 * @category  ci-report app
34
 *
35
 * @author    Francois-Xavier Soubirou <[email protected]>
36
 * @copyright 2017 Francois-Xavier Soubirou
37
 * @license   http://www.gnu.org/licenses/   GPLv3
38
 *
39
 * @see      https://www.ci-report.io
40
 */
41
class BootstrapStatusClassExtension extends Twig_Extension
42
{
43
    const BOOTSTRAP_SUCCESS = 'success';
44
    const BOOTSTRAP_WARNING = 'warning';
45
    const BOOTSTRAP_DANGER = 'danger';
46
    const BOOTSTRAP_UNKNOWN = 'secondary';
47
    const FA_SUCCESS = 'fa-check-circle';
48
    const FA_WARNING = 'fa-exclamation-circle';
49
    const FA_DANGER = 'fa-times-circle';
50
    const FA_UNKNOWN = 'fa-question-circle';
51
52
    /**
53
     * Returns a list of functions to add to the existing list.
54
     *
55
     * @return array
56
     */
57
    public function getFunctions(): array
58
    {
59
        return array(
60
            new Twig_SimpleFunction('statusColorClass', array($this, 'getStatusColorClass')),
61
            new Twig_SimpleFunction('statusIconClass', array($this, 'getStatusIconClass')),
62
        );
63
    }
64
65
    /**
66
     * Return Bootstrap style class of status.
67
     *
68
     * @param int|string $status Status id (const of Status class)
69
     *
70
     * @return string
71
     */
72
    public function getStatusColorClass($status): string
73
    {
74
        switch ($status) {
75
            case Test::ERRORED:
76
            case Test::FAILED:
77
            case Status::FAILED:
78
                $color = self::BOOTSTRAP_DANGER;
79
                break;
80
            case Test::SKIPPED:
81
            case Status::WARNING:
82
                $color = self::BOOTSTRAP_WARNING;
83
                break;
84
            case Status::UNKNOWN:
85
                $color = self::BOOTSTRAP_UNKNOWN;
86
                break;
87
            default:
88
                $color = self::BOOTSTRAP_SUCCESS;
89
        }
90
91
        return $color;
92
    }
93
94
    /**
95
     * Return Font Awesome icon of status.
96
     *
97
     * @param int|string $status Status id (const of Status class)
98
     *
99
     * @return string
100
     */
101
    public function getStatusIconClass($status): string
102
    {
103
        switch ($status) {
104
            case Test::ERRORED:
105
            case Test::FAILED:
106
            case Status::FAILED:
107
                $icon = self::FA_DANGER;
108
                break;
109
            case Test::SKIPPED:
110
            case Status::WARNING:
111
                $icon = self::FA_WARNING;
112
                break;
113
            case Status::UNKNOWN:
114
                $icon = self::FA_UNKNOWN;
115
                break;
116
            default:
117
                $icon = self::FA_SUCCESS;
118
        }
119
120
        return $icon;
121
    }
122
}
123