Passed
Push — master ( 08bc4f...c35bca )
by Thomas
02:57
created

Toasts::sessionMessage()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 25
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 7
eloc 21
c 2
b 0
f 1
nc 12
nop 3
dl 0
loc 25
rs 8.6506
1
<?php
2
3
namespace LeKoala\Admini\Traits;
4
5
use Exception;
6
use SilverStripe\View\ArrayData;
7
use SilverStripe\View\Requirements;
8
use SilverStripe\Control\HTTPRequest;
9
use SilverStripe\Control\HTTPResponse;
10
use SilverStripe\ORM\ValidationResult;
11
12
/**
13
 * @link https://getbootstrap.com/docs/5.0/components/toasts/
14
 */
15
trait Toasts
16
{
17
    /**
18
     * @return HTTPRequest
19
     */
20
    abstract public function getRequest();
21
22
    /**
23
     * @return HTTPResponse
24
     */
25
    abstract public function getResponse();
26
27
    /**
28
     * Set a message to the session, for display next time a page is shown.
29
     *
30
     * @param string $message the text of the message
31
     * @param string $type Should be set to good, bad, or warning.
32
     * @param string|bool $cast Cast type; One of the CAST_ constant definitions.
33
     * @return void
34
     */
35
    public function sessionMessage($message, $type = ValidationResult::TYPE_ERROR, $cast = ValidationResult::CAST_TEXT)
36
    {
37
        $color = "primary";
38
        switch ($type) {
39
            case "bad":
40
            case ValidationResult::TYPE_ERROR:
41
                $color = "danger";
42
                break;
43
            case "success":
44
            case ValidationResult::TYPE_GOOD:
45
                $color = "success";
46
                break;
47
            case ValidationResult::TYPE_WARNING:
48
                $color = "warning";
49
                break;
50
        }
51
52
        if ($this->getRequest()->isAjax()) {
53
            $this->getResponse()->addHeader('X-Status', $message . '|' . $type);
54
        } else {
55
            $this->getRequest()->getSession()->set('ToastMessage', [
56
                'Message' => $message,
57
                'Type' => $type,
58
                'ThemeColor' => $color,
59
                'Cast' => $cast,
60
            ]);
61
        }
62
    }
63
64
    /**
65
     * An helper for sessionMessage
66
     */
67
    public function successMessage(string $message, string $cast = ValidationResult::CAST_TEXT)
68
    {
69
        $this->sessionMessage($message, ValidationResult::TYPE_GOOD, $cast);
70
    }
71
72
    /**
73
     * An helper for sessionMessage
74
     */
75
    public function errorMessage(string $message, string $cast = ValidationResult::CAST_TEXT)
76
    {
77
        $this->sessionMessage($message, ValidationResult::TYPE_ERROR, $cast);
78
    }
79
80
    /**
81
     * An helper for sessionMessage
82
     */
83
    public function warningMessage(string $message, string $cast = ValidationResult::CAST_TEXT)
84
    {
85
        $this->sessionMessage($message, ValidationResult::TYPE_WARNING, $cast);
86
    }
87
88
    public function showToasterMessage()
89
    {
90
        $ToastMessage = $this->ToastMessage();
91
        if ($ToastMessage) {
0 ignored issues
show
introduced by
$ToastMessage is of type SilverStripe\View\ArrayData, thus it always evaluated to true.
Loading history...
92
            $Body = addslashes($ToastMessage->Message);
0 ignored issues
show
Bug introduced by
It seems like $ToastMessage->Message can also be of type null; however, parameter $string of addslashes() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

92
            $Body = addslashes(/** @scrutinizer ignore-type */ $ToastMessage->Message);
Loading history...
93
94
            // Don't hide errors automatically
95
            $autohide = $ToastMessage->Type == ValidationResult::TYPE_GOOD ? "true" : "false";
96
            $toastScript = <<<JS
97
toaster({
98
    body: '{$Body}',
99
    className: 'border-0 bg-{$ToastMessage->ThemeColor} text-white',
100
    autohide: {$autohide}
101
});
102
JS;
103
            Requirements::customScript($toastScript, __FUNCTION__);
104
        }
105
    }
106
107
    /**
108
     * Get the toast message
109
     *
110
     * @return ArrayData
111
     */
112
    public function ToastMessage()
113
    {
114
        $session = $this->getRequest()->getSession();
115
        try {
116
            $ToastMessage = $session->get('ToastMessage');
117
        } catch (Exception $ex) {
118
            $ToastMessage = null; // Session can be null (eg : Security)
119
        }
120
        if (!$ToastMessage) {
121
            return;
122
        }
123
        $session->clear('ToastMessage');
124
        return new ArrayData($ToastMessage);
125
    }
126
}
127