Completed
Push — master ( 24ca76...1b53a9 )
by Roj
02:47
created

BootstrapThreePresenter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Rojtjo\Notifier\Presenters;
4
5
use Rojtjo\Notifier\Notification;
6
use Rojtjo\Notifier\Notifications;
7
use Rojtjo\Notifier\Presenter;
8
9
class BootstrapThreePresenter implements Presenter
10
{
11
    /**
12
     * @var array
13
     */
14
    private $classNameSuffixMap = [
15
        'success' => 'success',
16
        'error' => 'danger',
17
        'warning' => 'warning',
18
        'info' => 'info',
19
    ];
20
21
    /**
22
     * @param array $classNameSuffixMap
23
     */
24 9
    public function __construct(array $classNameSuffixMap = [])
25
    {
26 9
        $this->classNameSuffixMap = array_merge($this->classNameSuffixMap, $classNameSuffixMap);
27 9
    }
28
29
    /**
30
     * @param Notifications $notifications
31
     * @return string
32
     */
33 6
    public function render(Notifications $notifications)
34
    {
35 6
        $output = '';
36 6
        foreach ($notifications as $notification) {
37 6
            $output .= $this->renderNotification($notification);
38 6
        }
39
40 6
        return $output;
41
    }
42
43
    /**
44
     * @param Notification $notification
45
     * @return string
46
     */
47 6
    private function renderNotification(Notification $notification)
48
    {
49 6
        $classNameSuffix = $this->getClassNameSuffix($notification->getType());
50
        return <<<HTML
51 6
<div class="alert alert-{$classNameSuffix}" role="alert">
52 6
    {$notification->getMessage()}
53
</div>
54
55 6
HTML;
56
    }
57
58
    /**
59
     * @param string $type
60
     * @return string
61
     */
62 6
    private function getClassNameSuffix($type)
63
    {
64 6
        if (!isset($this->classNameSuffixMap[$type])) {
65
            return 'default';
66
        }
67
68 6
        return $this->classNameSuffixMap[$type];
69
    }
70
}
71