Notify::escapeSingleQuote()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Yoeunes\Notify;
4
5
use Illuminate\Config\Repository;
6
use Illuminate\Session\SessionManager;
7
use function in_array;
8
use RuntimeException;
9
use Yoeunes\Notify\Notifiers\NotifierInterface;
10
11
class Notify
12
{
13
    const NOTIFICATIONS_NAMESPACE = 'notify::notifications';
14
15
    /**
16
     * Added notifications.
17
     *
18
     * @var array
19
     */
20
    protected $notifications = [];
21
22
    /**
23
     * Illuminate Session.
24
     *
25
     * @var \Illuminate\Session\SessionManager
26
     */
27
    protected $session;
28
29
    /**
30
     * Notification config.
31
     *
32
     * @var \Illuminate\Config\Repository
33
     */
34
    protected $config;
35
36
    /** @var NotifyInterface */
37
    protected $notifier;
38
39
    /**
40
     * Notification constructor.
41
     *
42
     * @param NotifierInterface $notifier
43
     * @param SessionManager $session
44
     * @param Repository $config
45
     */
46
    public function __construct(NotifierInterface $notifier, SessionManager $session, Repository $config)
47
    {
48
        $this->notifier = $notifier;
0 ignored issues
show
Documentation Bug introduced by
It seems like $notifier of type object<Yoeunes\Notify\No...iers\NotifierInterface> is incompatible with the declared type object<Yoeunes\Notify\NotifyInterface> of property $notifier.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
49
50
        $this->session = $session;
51
52
        $this->config = $config;
53
54
        $this->notifications = $this->session->get(self::NOTIFICATIONS_NAMESPACE, []);
55
    }
56
57
    /**
58
     * Add a notification.
59
     *
60
     * @param string $type    Could be error, info, success, or warning.
61
     * @param string $message The notification's message
62
     * @param string $title   The notification's title
63
     * @param array $options
64
     *
65
     * @return self
66
     */
67
    public function addNotification(string $type, string $message, string $title = '', array $options = []): self
68
    {
69
        if (!in_array($type, $this->notifier->getAllowedTypes(), true)) {
70
            throw new RuntimeException('Invalid type "'.$type.'" for the "'.$this->notifier->getName().'"');
71
        }
72
73
        $this->notifications[] = [
74
            'type'    => $type,
75
            'title'   => $this->escapeSingleQuote($title),
76
            'message' => $this->escapeSingleQuote($message),
77
            'options' => json_encode($options),
78
        ];
79
80
        $this->session->flash(self::NOTIFICATIONS_NAMESPACE, $this->notifications);
81
82
        return $this;
83
    }
84
85
    /**
86
     * helper function to escape single quote for example for french words.
87
     *
88
     * @param string $value
89
     *
90
     * @return string
91
     */
92
    private function escapeSingleQuote(string $value): string
93
    {
94
        return str_replace("'", "\\'", $value);
95
    }
96
97
    /**
98
     * Render the notifications' script tag.
99
     *
100
     * @return string
101
     */
102
    public function render(): string
103
    {
104
        $notification = $this->notifier->render($this->notifications);
105
106
        $this->session->forget(self::NOTIFICATIONS_NAMESPACE);
107
108
        return $notification;
109
    }
110
111
    /**
112
     * Clear all notifications.
113
     *
114
     * @return self
115
     */
116
    public function clear(): self
117
    {
118
        $this->notifications = [];
119
120
        $this->session->forget(self::NOTIFICATIONS_NAMESPACE);
121
122
        return $this;
123
    }
124
125
    /**
126
     * @param $method
127
     * @param $arguments
128
     */
129
    public function __call($method, $arguments)
130
    {
131
        if (!in_array($method, $this->notifier->getAllowedTypes(), true)) {
132
            throw new RuntimeException('Invalid type "'.$method.'" for the "'.$this->notifier->getName().'"');
133
        }
134
135
        $this->addNotification($method, ...$arguments);
136
    }
137
}
138