Completed
Push — dev-master ( 0f2097...40fdeb )
by Derek Stephen
01:59
created

AlertBox   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 0
loc 49
ccs 14
cts 21
cp 0.6667
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A alertBox() 0 7 1
A getClass() 0 10 3
A renderMessage() 0 15 3
1
<?php
2
3
namespace Bone\View\Helper;
4
5
class AlertBox
6
{
7
    /**
8
     * @param array $message array of messages, last item in array should be alert class
9
     * @return bool|string
10
     */
11 1
    public function alertBox(array $message)
12
    {
13 1
        $class = $this->getClass($message);
14 1
        $alert = '<div class="alert ' . $class . '"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>' . $this->renderMessage($message) . '</div>';
15
16 1
        return $alert;
17
    }
18
19
    /**
20
     * @param array $message
21
     * @return string
22
     */
23 1
    private function getClass(array $message)
24
    {
25 1
        if (count($message) < 2) {
26 1
            return 'alert-info';
27
        }
28
        $class = array_pop($message);
29
        $class = (!strstr($class, 'alert-')) ? 'alert-' . $class : '';
30
31
        return $class;
32
    }
33
34
    /**
35
     * @param array $message
36
     * @return string
37
     */
38 1
    private function renderMessage(array $message)
39
    {
40 1
        $alert = '';
41 1
        array_pop($message);
42 1
        $num = count($message);
43 1
        $x = 1;
44 1
        foreach ($message as $msg) {
45
            $alert .= $msg;
46
            if ($x < $num) {
47
                $alert .= '<br />';
48
            }
49
            $x++;
50
        }
51 1
        return $alert;
52
    }
53
}