Completed
Push — dev-master ( d5c1da...5a3865 )
by Derek Stephen
02:04
created

AlertBox   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 0
dl 0
loc 51
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A alertBox() 0 9 1
A getClass() 0 9 3
A renderMessage() 0 16 4
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
15 1
        $alert = '<div class="alert '.$class.'"><button type="button" class="close" data-dismiss="alert" aria-label="Close">
16
                      <span aria-hidden="true">&times;</span>
17 1
                    </button>'.$this->renderMessage($message).'</div>';
18 1
        return $alert;
19
    }
20
21
    /**
22
     * @param array $message
23
     * @return string
24
     */
25 1
    private function getClass(array $message)
26
    {
27 1
        if(count($message) < 2) {
28 1
            return 'alert-info';
29
        }
30 1
        $class = array_pop($message);
31 1
        $class = (!strstr($class, 'alert-')) ? 'alert-'.$class : '';
32 1
        return $class;
33
    }
34
35
    /**
36
     * @param array $message
37
     * @return string
38
     */
39 1
    private function renderMessage(array $message)
40
    {
41 1
        if (isset($message[1])) {
42 1
            unset($message[1]);
43
        }
44 1
        $alert = '';
45 1
        $num = count($message);
46 1
        $x = 1;
47 1
        foreach($message as $msg)
48
        {
49 1
            $alert .= $msg;
50 1
            if($x < $num){$alert .= '<br />';}
51 1
            $x ++;
52
        }
53 1
        return $alert;
54
    }
55
}