Completed
Push — master ( 550a5f...93d693 )
by Derek Stephen
11:28
created

AlertBox::alertBox()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Del\Common\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 2
    public function alertBox(array $message)
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
12
    {
13 2
        $class = $this->getClass($message);
14
15 2
        $alert = '<div class="alert '.$class.'"><button type="button" class="close" data-dismiss="alert" aria-label="Close">
16
                      <span aria-hidden="true">&times;</span>
17 2
                    </button>'.$this->renderMessage($message).'</div>';
18 2
        return $alert;
19
    }
20
21
    /**
22
     * @param array $message
23
     * @return string
24
     */
25 2
    private function getClass(array $message)
26
    {
27 2
        if(count($message) < 2) {
28 1
            return 'info';
29
        }
30 1
        $class = array_pop($message);
31 1
        $class = ($class != 'alert') ? 'alert-'.$class : '';
32 1
        return $class;
33
    }
34
35
    /**
36
     * @param array $message
37
     * @return string
38
     */
39 2
    private function renderMessage(array $message)
40
    {
41 2
        $alert = '';
42 2
        $num = count($message);
43 2
        $x = 1;
44 2
        foreach($message as $msg)
45
        {
46 2
            $alert .= $msg;
47 2
            if($x < $num){$alert .= '<br />';}
48 2
            $x ++;
49 2
        }
50 2
        return $alert;
51
    }
52
}