1 | <?php |
||
2 | |||
3 | namespace vasadibt\onesignal\resolver; |
||
4 | |||
5 | use Symfony\Component\OptionsResolver\Options; |
||
6 | use Symfony\Component\OptionsResolver\OptionsResolver; |
||
7 | use vasadibt\onesignal\OneSignal; |
||
8 | use yii\base\BaseObject; |
||
0 ignored issues
–
show
|
|||
9 | |||
10 | /** |
||
11 | * Class NotificationResolver |
||
12 | * @package vasadibt\onesignal\resolver |
||
13 | */ |
||
14 | class NotificationResolver extends BaseObject implements ResolverInterface |
||
15 | { |
||
16 | const SEND_AFTER_FORMAT = 'Y-m-d H:i:sO'; |
||
17 | const DELIVERY_TIME_OF_DAY_FORMAT = 'g:iA'; |
||
18 | |||
19 | /** |
||
20 | * @var OneSignal |
||
21 | */ |
||
22 | public $api; |
||
23 | |||
24 | /** |
||
25 | * {@inheritdoc} |
||
26 | */ |
||
27 | public function resolve(array $data) |
||
28 | { |
||
29 | return (new OptionsResolver()) |
||
30 | ->setDefined('contents') |
||
31 | ->setAllowedTypes('contents', 'array') |
||
32 | ->setDefined('headings') |
||
33 | ->setAllowedTypes('headings', 'array') |
||
34 | ->setDefined('subtitle') |
||
35 | ->setAllowedTypes('subtitle', 'array') |
||
36 | ->setDefined('isIos') |
||
37 | ->setAllowedTypes('isIos', 'bool') |
||
38 | ->setDefined('isAndroid') |
||
39 | ->setAllowedTypes('isAndroid', 'bool') |
||
40 | ->setDefined('isWP') |
||
41 | ->setAllowedTypes('isWP', 'bool') |
||
42 | ->setDefined('isWP_WNS') |
||
43 | ->setAllowedTypes('isWP_WNS', 'bool') |
||
44 | ->setDefined('isAdm') |
||
45 | ->setAllowedTypes('isAdm', 'bool') |
||
46 | ->setDefined('isChrome') |
||
47 | ->setAllowedTypes('isChrome', 'bool') |
||
48 | ->setDefined('isChromeWeb') |
||
49 | ->setAllowedTypes('isChromeWeb', 'bool') |
||
50 | ->setDefined('isFirefox') |
||
51 | ->setAllowedTypes('isFirefox', 'bool') |
||
52 | ->setDefined('isSafari') |
||
53 | ->setAllowedTypes('isSafari', 'bool') |
||
54 | ->setDefined('isAnyWeb') |
||
55 | ->setAllowedTypes('isAnyWeb', 'bool') |
||
56 | ->setDefined('included_segments') |
||
57 | ->setAllowedTypes('included_segments', 'array') |
||
58 | ->setDefined('excluded_segments') |
||
59 | ->setAllowedTypes('excluded_segments', 'array') |
||
60 | ->setDefined('include_player_ids') |
||
61 | ->setAllowedTypes('include_player_ids', 'array') |
||
62 | ->setDefined('include_external_user_ids') |
||
63 | ->setAllowedTypes('include_external_user_ids', 'array') |
||
64 | ->setDefined('include_ios_tokens') |
||
65 | ->setAllowedTypes('include_ios_tokens', 'array') |
||
66 | ->setDefined('include_android_reg_ids') |
||
67 | ->setAllowedTypes('include_android_reg_ids', 'array') |
||
68 | ->setDefined('include_wp_uris') |
||
69 | ->setAllowedTypes('include_wp_uris', 'array') |
||
70 | ->setDefined('include_wp_wns_uris') |
||
71 | ->setAllowedTypes('include_wp_wns_uris', 'array') |
||
72 | ->setDefined('include_amazon_reg_ids') |
||
73 | ->setAllowedTypes('include_amazon_reg_ids', 'array') |
||
74 | ->setDefined('include_chrome_reg_ids') |
||
75 | ->setAllowedTypes('include_chrome_reg_ids', 'array') |
||
76 | ->setDefined('include_chrome_web_reg_ids') |
||
77 | ->setAllowedTypes('include_chrome_web_reg_ids', 'array') |
||
78 | ->setDefined('app_ids') |
||
79 | ->setAllowedTypes('app_ids', 'array') |
||
80 | ->setDefined('filters') |
||
81 | ->setAllowedTypes('filters', 'array') |
||
82 | ->setNormalizer('filters', function (Options $options, array $values) { |
||
83 | return $this->normalizeFilters($options, $values); |
||
84 | }) |
||
85 | ->setDefined('ios_badgeType') |
||
86 | ->setAllowedTypes('ios_badgeType', 'string') |
||
87 | ->setAllowedValues('ios_badgeType', ['None', 'SetTo', 'Increase']) |
||
88 | ->setDefined('ios_badgeCount') |
||
89 | ->setAllowedTypes('ios_badgeCount', 'int') |
||
90 | ->setDefined('ios_sound') |
||
91 | ->setAllowedTypes('ios_sound', 'string') |
||
92 | ->setDefined('android_sound') |
||
93 | ->setAllowedTypes('android_sound', 'string') |
||
94 | ->setDefined('adm_sound') |
||
95 | ->setAllowedTypes('adm_sound', 'string') |
||
96 | ->setDefined('wp_sound') |
||
97 | ->setAllowedTypes('wp_sound', 'string') |
||
98 | ->setDefined('wp_wns_sound') |
||
99 | ->setAllowedTypes('wp_wns_sound', 'string') |
||
100 | ->setDefined('data') |
||
101 | ->setAllowedTypes('data', 'array') |
||
102 | ->setDefined('buttons') |
||
103 | ->setAllowedTypes('buttons', 'array') |
||
104 | ->setNormalizer('buttons', function (Options $options, array $values) { |
||
105 | return $this->normalizeButtons($values); |
||
106 | }) |
||
107 | ->setDefined('android_channel_id') |
||
108 | ->setAllowedTypes('android_channel_id', 'string') |
||
109 | ->setDefined('existing_android_channel_id') |
||
110 | ->setAllowedTypes('existing_android_channel_id', 'string') |
||
111 | ->setDefined('android_background_layout') |
||
112 | ->setAllowedTypes('android_background_layout', 'array') |
||
113 | ->setAllowedValues('android_background_layout', function ($layouts) { |
||
114 | return $this->filterAndroidBackgroundLayout($layouts); |
||
115 | }) |
||
116 | ->setDefined('small_icon') |
||
117 | ->setAllowedTypes('small_icon', 'string') |
||
118 | ->setDefined('large_icon') |
||
119 | ->setAllowedTypes('large_icon', 'string') |
||
120 | ->setDefined('ios_attachments') |
||
121 | ->setAllowedTypes('ios_attachments', 'array') |
||
122 | ->setAllowedValues('ios_attachments', function ($attachments) { |
||
123 | return $this->filterIosAttachments($attachments); |
||
124 | }) |
||
125 | ->setDefined('big_picture') |
||
126 | ->setAllowedTypes('big_picture', 'string') |
||
127 | ->setDefined('adm_small_icon') |
||
128 | ->setAllowedTypes('adm_small_icon', 'string') |
||
129 | ->setDefined('adm_large_icon') |
||
130 | ->setAllowedTypes('adm_large_icon', 'string') |
||
131 | ->setDefined('adm_big_picture') |
||
132 | ->setAllowedTypes('adm_big_picture', 'string') |
||
133 | ->setDefined('web_buttons') |
||
134 | ->setAllowedTypes('web_buttons', 'array') |
||
135 | ->setAllowedValues('web_buttons', function ($buttons) { |
||
136 | return $this->filterWebButtons($buttons); |
||
137 | }) |
||
138 | ->setDefined('ios_category') |
||
139 | ->setAllowedTypes('ios_category', 'string') |
||
140 | ->setDefined('chrome_icon') |
||
141 | ->setAllowedTypes('chrome_icon', 'string') |
||
142 | ->setDefined('chrome_big_picture') |
||
143 | ->setAllowedTypes('chrome_big_picture', 'string') |
||
144 | ->setDefined('chrome_web_icon') |
||
145 | ->setAllowedTypes('chrome_web_icon', 'string') |
||
146 | ->setDefined('chrome_web_image') |
||
147 | ->setAllowedTypes('chrome_web_image', 'string') |
||
148 | ->setDefined('firefox_icon') |
||
149 | ->setAllowedTypes('firefox_icon', 'string') |
||
150 | ->setDefined('url') |
||
151 | ->setAllowedTypes('url', 'string') |
||
152 | ->setAllowedValues('url', function ($value) { |
||
153 | return $this->filterUrl($value); |
||
154 | }) |
||
155 | ->setDefined('send_after') |
||
156 | ->setAllowedTypes('send_after', '\DateTimeInterface') |
||
157 | ->setNormalizer('send_after', function (Options $options, \DateTimeInterface $value) { |
||
158 | return $this->normalizeDateTime($options, $value, self::SEND_AFTER_FORMAT); |
||
159 | }) |
||
160 | ->setDefined('delayed_option') |
||
161 | ->setAllowedTypes('delayed_option', 'string') |
||
162 | ->setAllowedValues('delayed_option', ['timezone', 'last-active']) |
||
163 | ->setDefined('delivery_time_of_day') |
||
164 | ->setAllowedTypes('delivery_time_of_day', '\DateTimeInterface') |
||
165 | ->setNormalizer('delivery_time_of_day', function (Options $options, \DateTimeInterface $value) { |
||
166 | return $this->normalizeDateTime($options, $value, self::DELIVERY_TIME_OF_DAY_FORMAT); |
||
167 | }) |
||
168 | ->setDefined('android_led_color') |
||
169 | ->setAllowedTypes('android_led_color', 'string') |
||
170 | ->setDefined('android_accent_color') |
||
171 | ->setAllowedTypes('android_accent_color', 'string') |
||
172 | ->setDefined('android_visibility') |
||
173 | ->setAllowedTypes('android_visibility', 'int') |
||
174 | ->setAllowedValues('android_visibility', [-1, 0, 1]) |
||
175 | ->setDefined('collapse_id') |
||
176 | ->setAllowedTypes('collapse_id', 'string') |
||
177 | ->setDefined('content_available') |
||
178 | ->setAllowedTypes('content_available', 'bool') |
||
179 | ->setDefined('mutable_content') |
||
180 | ->setAllowedTypes('mutable_content', 'bool') |
||
181 | ->setDefined('android_background_data') |
||
182 | ->setAllowedTypes('android_background_data', 'bool') |
||
183 | ->setDefined('amazon_background_data') |
||
184 | ->setAllowedTypes('amazon_background_data', 'bool') |
||
185 | ->setDefined('template_id') |
||
186 | ->setAllowedTypes('template_id', 'string') |
||
187 | ->setDefined('android_group') |
||
188 | ->setAllowedTypes('android_group', 'string') |
||
189 | ->setDefined('android_group_message') |
||
190 | ->setAllowedTypes('android_group_message', 'array') |
||
191 | ->setDefined('adm_group') |
||
192 | ->setAllowedTypes('adm_group', 'string') |
||
193 | ->setDefined('adm_group_message') |
||
194 | ->setAllowedTypes('adm_group_message', 'array') |
||
195 | ->setDefined('ttl') |
||
196 | ->setAllowedTypes('ttl', 'int') |
||
197 | ->setDefined('priority') |
||
198 | ->setAllowedTypes('priority', 'int') |
||
199 | ->setDefault('app_id', $this->api->appId) |
||
200 | ->setAllowedTypes('app_id', 'string') |
||
201 | ->setDefined('email_subject') |
||
202 | ->setAllowedTypes('email_subject', 'string') |
||
203 | ->setDefined('email_body') |
||
204 | ->setAllowedTypes('email_body', 'string') |
||
205 | ->setDefined('email_from_name') |
||
206 | ->setAllowedTypes('email_from_name', 'string') |
||
207 | ->setDefined('email_from_address') |
||
208 | ->setAllowedTypes('email_from_address', 'string') |
||
209 | ->resolve($data); |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * @param Options $options |
||
214 | * @param array $values |
||
215 | * @return array |
||
216 | */ |
||
217 | private function normalizeFilters(Options $options, array $values) |
||
218 | { |
||
219 | $filters = []; |
||
220 | |||
221 | foreach ($values as $filter) { |
||
222 | if (isset($filter['field'])) { |
||
223 | $filters[] = $filter; |
||
224 | } elseif (isset($filter['operator'])) { |
||
225 | $filters[] = ['operator' => 'OR']; |
||
226 | } |
||
227 | } |
||
228 | |||
229 | return $filters; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * @param $value |
||
234 | * @return bool |
||
235 | */ |
||
236 | private function filterUrl($value) |
||
237 | { |
||
238 | return (bool) filter_var($value, FILTER_VALIDATE_URL); |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * @param $values |
||
243 | * @return array |
||
244 | */ |
||
245 | private function normalizeButtons($values) |
||
246 | { |
||
247 | $buttons = []; |
||
248 | |||
249 | foreach ($values as $button) { |
||
250 | if (!isset($button['text'])) { |
||
251 | continue; |
||
252 | } |
||
253 | |||
254 | $buttons[] = [ |
||
255 | 'id' => (isset($button['id']) ? $button['id'] : mt_rand()), |
||
256 | 'text' => $button['text'], |
||
257 | 'icon' => (isset($button['icon']) ? $button['icon'] : null), |
||
258 | ]; |
||
259 | } |
||
260 | |||
261 | return $buttons; |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * @param $layouts |
||
266 | * @return bool |
||
267 | */ |
||
268 | private function filterAndroidBackgroundLayout($layouts) |
||
269 | { |
||
270 | if (empty($layouts)) { |
||
271 | return false; |
||
272 | } |
||
273 | |||
274 | $requiredKeys = ['image', 'headings_color', 'contents_color']; |
||
275 | |||
276 | foreach ($layouts as $k => $v) { |
||
277 | if (!in_array($k, $requiredKeys) || !is_string($v)) { |
||
278 | return false; |
||
279 | } |
||
280 | } |
||
281 | |||
282 | return true; |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * @param $attachments |
||
287 | * @return bool |
||
288 | */ |
||
289 | private function filterIosAttachments($attachments) |
||
290 | { |
||
291 | foreach ($attachments as $key => $value) { |
||
292 | if (!is_string($key) || !is_string($value)) { |
||
293 | return false; |
||
294 | } |
||
295 | } |
||
296 | |||
297 | return true; |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * @param $buttons |
||
302 | * @return bool |
||
303 | */ |
||
304 | private function filterWebButtons($buttons) |
||
305 | { |
||
306 | $requiredKeys = ['id', 'text', 'icon', 'url']; |
||
307 | foreach ($buttons as $button) { |
||
308 | if (!is_array($button)) { |
||
309 | return false; |
||
310 | } |
||
311 | if (count(array_intersect_key(array_flip($requiredKeys), $button)) != count($requiredKeys)) { |
||
312 | return false; |
||
313 | } |
||
314 | } |
||
315 | |||
316 | return true; |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * @param Options $options |
||
321 | * @param \DateTimeInterface $value |
||
322 | * @param $format |
||
323 | * @return string |
||
324 | */ |
||
325 | private function normalizeDateTime(Options $options, \DateTimeInterface $value, $format) |
||
326 | { |
||
327 | return $value->format($format); |
||
328 | } |
||
329 | } |
||
330 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths