Passed
Push — master ( 06fadb...21c0e7 )
by Thomas
04:14 queued 01:30
created

BootstrapAlertField::FieldHolder()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 20
rs 9.8666
1
<?php
2
3
namespace LeKoala\Admini\Forms;
4
5
use SilverStripe\Forms\LiteralField;
6
7
/**
8
 * Bootstrap alert
9
 * Maps good/bad classes
10
 */
11
class BootstrapAlertField extends LiteralField
12
{
13
14
    /**
15
     * @var bool
16
     */
17
    protected $allowHTML = true;
18
19
    /**
20
     * @var string
21
     */
22
    protected $alertType = 'info';
23
24
    /**
25
     * @var bool
26
     */
27
    protected $messageClass = null;
28
29
    /**
30
     * @param string $name
31
     * @param string|FormField $content
0 ignored issues
show
Bug introduced by
The type LeKoala\Admini\Forms\FormField was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
     * @param string $type
33
     */
34
    public function __construct($name, $content = null, $type = null)
35
    {
36
        $this->setContent($content);
37
38
        parent::__construct($name, $content);
39
40
        if ($type) {
41
            $this->setAlertType($type);
42
        }
43
    }
44
45
    /**
46
     * @param array $properties
47
     *
48
     * @return string
49
     */
50
    public function FieldHolder($properties = array())
51
    {
52
        $content = parent::FieldHolder($properties);
53
        $type = $this->alertType;
54
55
        // Wrap in an alert
56
        $extraClasses = $this->extraClasses;
57
        if (is_array($extraClasses)) {
0 ignored issues
show
introduced by
The condition is_array($extraClasses) is always true.
Loading history...
58
            $extraClasses = implode(" ", $extraClasses);
59
        }
60
        if ($extraClasses) {
61
            $extraClasses = " " . $extraClasses;
62
        }
63
        $classes = 'alert alert-' . $type . '' . $extraClasses;
64
        if ($this->messageClass) {
65
            $classes .= " message " . $this->messageClass;
0 ignored issues
show
Bug introduced by
Are you sure $this->messageClass of type true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
            $classes .= " message " . /** @scrutinizer ignore-type */ $this->messageClass;
Loading history...
66
        }
67
        $content = "<div class=\"{$classes}\" role=\"alert\">$content</div>";
68
69
        return $content;
70
    }
71
72
    /**
73
     * Get the value of alertType
74
     */
75
    public function getAlertType()
76
    {
77
        return $this->alertType;
78
    }
79
80
    /**
81
     * Set the value of alertType
82
     *
83
     * @return $this
84
     */
85
    public function setAlertType($alertType)
86
    {
87
        switch ($alertType) {
88
            case 'bad':
89
                $this->messageClass = $alertType;
90
                $alertType = "danger";
91
                break;
92
            case 'good':
93
                $this->messageClass = $alertType;
94
                $alertType = "success";
95
                break;
96
        }
97
        $this->alertType = $alertType;
98
99
        return $this;
100
    }
101
}
102