1
1
<?php
2
+
2
3
declare (strict_types=1 );
3
4
4
5
namespace Mkk \NtfyBundle \Message ;
5
6
7
+ use Symfony \Component \Notifier \Exception \LogicException ;
6
8
use Symfony \Component \Notifier \Message \MessageOptionsInterface ;
7
9
use Symfony \Component \Notifier \Notification \Notification ;
8
10
9
11
final class NtfyOptions implements MessageOptionsInterface
10
12
{
11
13
private array $ options ;
12
- const PRIORITY_MAX = 5 ;
13
- const PRIORITY_URGENT = 5 ;
14
- const PRIORITY_HIGH = 4 ;
15
- const PRIORITY_DEFAULT = 3 ;
16
- const PRIORITY_LOW = 2 ;
17
- const PRIORITY_MIN = 1 ;
14
+ public const PRIORITY_URGENT = 5 ;
15
+ public const PRIORITY_HIGH = 4 ;
16
+ public const PRIORITY_DEFAULT = 3 ;
17
+ public const PRIORITY_LOW = 2 ;
18
+ public const PRIORITY_MIN = 1 ;
18
19
19
20
public function __construct (array $ options = [])
20
21
{
@@ -28,6 +29,7 @@ public static function fromNotification(Notification $notification): self
28
29
$ options ->setMessage ($ notification ->getContent ());
29
30
$ options ->setStringPriority ($ notification ->getImportance ());
30
31
$ options ->addTag ($ notification ->getEmoji ());
32
+
31
33
return $ options ;
32
34
}
33
35
@@ -44,12 +46,14 @@ public function getRecipientId(): ?string
44
46
public function setMessage (string $ message ): self
45
47
{
46
48
$ this ->options ['message ' ] = $ message ;
49
+
47
50
return $ this ;
48
51
}
49
52
50
53
public function setTitle (string $ title ): self
51
54
{
52
55
$ this ->options ['title ' ] = $ title ;
56
+
53
57
return $ this ;
54
58
}
55
59
@@ -69,82 +73,101 @@ public function setStringPriority(string $priority): self
69
73
70
74
public function setPriority (int $ priority ): self
71
75
{
72
- if (in_array ($ priority , [
73
- self ::PRIORITY_MIN , self ::PRIORITY_LOW , self ::PRIORITY_DEFAULT , self ::PRIORITY_HIGH , self ::PRIORITY_URGENT , self :: PRIORITY_MAX
76
+ if (\ in_array ($ priority , [
77
+ self ::PRIORITY_MIN , self ::PRIORITY_LOW , self ::PRIORITY_DEFAULT , self ::PRIORITY_HIGH , self ::PRIORITY_URGENT ,
74
78
])) {
75
79
$ this ->options ['priority ' ] = $ priority ;
76
80
}
81
+
77
82
return $ this ;
78
83
}
79
84
80
85
public function addTag (string $ tag ): self
81
86
{
82
87
$ this ->options ['tags ' ][] = $ tag ;
88
+
83
89
return $ this ;
84
90
}
85
91
86
92
public function setTags (array $ tags ): self
87
93
{
88
94
$ this ->options ['tags ' ] = $ tags ;
95
+
89
96
return $ this ;
90
97
}
91
98
92
99
public function setDelay (\DateTimeInterface $ dateTime ): self
93
100
{
94
- $ this ->options ['delay ' ] = $ dateTime ->getTimestamp ();
101
+ if ($ dateTime > (new \DateTime ())) {
102
+ $ this ->options ['delay ' ] = (string ) $ dateTime ->getTimestamp ();
103
+ } else {
104
+ throw new LogicException ('Delayed date must be defined in the future. ' );
105
+ }
106
+
95
107
return $ this ;
96
108
}
97
109
98
110
public function setActions (array $ actions ): self
99
111
{
100
112
$ this ->options ['actions ' ] = $ actions ;
113
+
101
114
return $ this ;
102
115
}
103
116
104
117
public function addAction (array $ action ): self
105
118
{
106
119
$ this ->options ['actions ' ][] = $ action ;
120
+
107
121
return $ this ;
108
122
}
109
123
110
124
public function setClick (string $ url ): self
111
125
{
112
126
$ this ->options ['click ' ] = $ url ;
127
+
113
128
return $ this ;
114
129
}
115
130
116
131
public function setAttachment (string $ attachment ): self
117
132
{
118
133
$ this ->options ['attach ' ] = $ attachment ;
134
+
119
135
return $ this ;
120
136
}
121
137
122
138
public function setFilename (string $ filename ): self
123
139
{
124
140
$ this ->options ['filename ' ] = $ filename ;
141
+
125
142
return $ this ;
126
143
}
127
144
128
145
public function setEmail (string $ email ): self
129
146
{
130
147
$ this ->options ['email ' ] = $ email ;
148
+
131
149
return $ this ;
132
150
}
133
151
134
- public function setCache (bool $ enable )
152
+ public function setCache (bool $ enable ): self
135
153
{
136
154
if (!$ enable ) {
137
155
$ this ->options ['cache ' ] = 'no ' ;
138
156
} else {
139
157
unset($ this ->options ['cache ' ]);
140
158
}
159
+
160
+ return $ this ;
141
161
}
142
- public function setFirebase (bool $ enable )
162
+
163
+ public function setFirebase (bool $ enable ): self
143
164
{
144
165
if (!$ enable ) {
145
166
$ this ->options ['firebase ' ] = 'no ' ;
146
167
} else {
147
168
unset($ this ->options ['firebase ' ]);
148
169
}
170
+
171
+ return $ this ;
149
172
}
150
- }
173
+ }
0 commit comments