File size: 10,197 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<?php

namespace Mautic\DashboardBundle\Event;

use Mautic\CacheBundle\Cache\CacheProvider;
use Mautic\CoreBundle\Event\CommonEvent;
use Mautic\CoreBundle\Helper\CacheStorageHelper;
use Mautic\CoreBundle\Security\Permissions\CorePermissions;
use Mautic\DashboardBundle\Entity\Widget;
use Mautic\DashboardBundle\Exception\CouldNotFormatDateTimeException;
use Symfony\Contracts\Translation\TranslatorInterface;

class WidgetDetailEvent extends CommonEvent
{
    protected $type;

    protected $template;

    protected $templateData = [];

    protected $errorMessage;

    protected $uniqueId;

    protected $cacheDir;

    protected $uniqueCacheDir;

    protected $cacheTimeout;

    protected float $startTime;

    protected $loadTime  = 0;

    private string $cacheKeyPath = 'dashboard.widget.';

    private bool $isPreview = false;

    public function __construct(private TranslatorInterface $translator, private CorePermissions $security, protected Widget $widget, private ?CacheProvider $cacheProvider = null)
    {
        $this->startTime = microtime(true);
        $this->setWidget($widget);
    }

    /**
     * Act as widget preview without data.
     */
    public function setPreview(bool $isPreview): void
    {
        $this->isPreview = $isPreview;
    }

    /**
     * Is preview without data?
     */
    public function isPreview(): bool
    {
        return $this->isPreview;
    }

    /**
     * Return unique key, uses legacy methods for BC.
     */
    public function getCacheKey(): string
    {
        $cacheKey = [
            $this->getUniqueWidgetId(),
        ];

        $params = $this->getWidget()->getParams();

        foreach (['dateTo', 'dateFrom'] as $dateParameter) {
            if (isset($params[$dateParameter])) {
                try {
                    $date       = $this->castDateTimeToString($params[$dateParameter]);
                    $cacheKey[] = $date;
                } catch (CouldNotFormatDateTimeException) {
                }
            }
        }

        // If there are no additional parameters we return uniqueWidgetId as a cache key
        // Otherwise we return hashed $cacheKey value
        $cacheKey = (1 == count($cacheKey)) ? $this->getUniqueWidgetId() : substr(md5(implode('', $cacheKey)), 0, 16);

        return $this->cacheKeyPath.$cacheKey;
    }

    /**
     * Set the cache dir.
     *
     * @param string     $cacheDir
     * @param mixed|null $uniqueCacheDir
     */
    public function setCacheDir($cacheDir, $uniqueCacheDir = null): void
    {
        $this->cacheDir       = $cacheDir;
        $this->uniqueCacheDir = $uniqueCacheDir;
    }

    /**
     * Set the cache timeout.
     *
     * @param string $cacheTimeout
     */
    public function setCacheTimeout($cacheTimeout): void
    {
        $this->cacheTimeout = (int) $cacheTimeout;
    }

    /**
     * Set the widget type.
     *
     * @param string $type
     */
    public function setType($type): void
    {
        $this->type = $type;
    }

    /**
     * Get the widget type.
     *
     * @return string $type
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * Set the widget entity.
     */
    public function setWidget(Widget $widget): void
    {
        $this->widget = $widget;

        $params = $widget->getParams();

        // Set required params if undefined
        if (!isset($params['timeUnit'])) {
            $params['timeUnit'] = null;
        }

        if (!isset($params['amount'])) {
            $params['amount'] = null;
        }

        if (!isset($params['dateFormat'])) {
            $params['dateFormat'] = null;
        }

        if (!isset($params['filter'])) {
            $params['filter'] = [];
        }

        $widget->setParams($params);

        $this->setType($widget->getType());
        $this->setCacheTimeout($widget->getCacheTimeout());
    }

    /**
     * Returns the widget entity.
     *
     * @return Widget $widget
     */
    public function getWidget()
    {
        return $this->widget;
    }

    /**
     * Set the widget template.
     *
     * @param string $template
     */
    public function setTemplate($template): void
    {
        $this->template = $template;
        $this->widget->setTemplate($template);
    }

    /**
     * Get the widget template.
     *
     * @return string $template
     */
    public function getTemplate()
    {
        return $this->template;
    }

    /**
     * Set the widget template data.
     *
     * @param bool|null $skipCache
     *
     * @throws \Psr\Cache\InvalidArgumentException
     */
    public function setTemplateData(array $templateData, $skipCache = false): void
    {
        $this->templateData = $templateData;
        $this->widget->setTemplateData($templateData);
        $this->widget->setLoadTime(abs(microtime(true) - $this->startTime));

        if ($this->usesLegacyCache()) {
            // Store the template data to the cache
            if (!$skipCache && $this->cacheDir && $this->widget->getCacheTimeout() > 0) {
                $cache = new CacheStorageHelper(CacheStorageHelper::ADAPTOR_FILESYSTEM, $this->uniqueCacheDir, null, $this->cacheDir);
                // must pass a DateTime object or a int of seconds to expire as 3rd attribute to set().
                $expireTime = $this->widget->getCacheTimeout() * 60;

                $cache->set($this->getUniqueWidgetId(), $templateData, (int) $expireTime);
            }
        }

        $cItem = $this->cacheProvider->getItem($this->getCacheKey());
        if ($this->widget->getCacheTimeout()) {
            $cItem->expiresAfter((int) $this->widget->getCacheTimeout() * 60);  // This is in minutes
        }
        $cItem->set($templateData);

        $this->cacheProvider->save($cItem);
    }

    /**
     * Get the widget template data.
     *
     * @return array<mixed> $templateData
     */
    public function getTemplateData()
    {
        return $this->templateData;
    }

    /**
     * Set en error message.
     *
     * @param string $errorMessage
     */
    public function setErrorMessage($errorMessage): void
    {
        $this->errorMessage = $errorMessage;
        $this->widget->setErrorMessage($errorMessage);
    }

    /**
     * Get an error message.
     *
     * @return string $errorMessage
     */
    public function getErrorMessage()
    {
        return $this->errorMessage;
    }

    /**
     * Build a unique ID from type and widget params.
     *
     * @return string
     */
    public function getUniqueWidgetId()
    {
        if ($this->uniqueId) {
            return $this->uniqueId;
        }

        $params = $this->getWidget()->getParams();
        // Unset dateFrom and dateTo since they constantly change
        unset($params['dateFrom'], $params['dateTo']);

        $uniqueSettings = [
            'params' => $params,
            'width'  => $this->getWidget()->getWidth(),
            'height' => $this->getWidget()->getHeight(),
            'locale' => $this->translator->getLocale(),
        ];

        return $this->uniqueId = $this->getType().'_'.substr(md5(json_encode($uniqueSettings)), 0, 16);
    }

    /**
     * @throws \Psr\Cache\InvalidArgumentException
     *                                             Checks the cache for the widget data.
     *                                             If cache exists, it sets the TemplateData.
     */
    public function isCached(): bool
    {
        if (!$this->cacheDir && $this->usesLegacyCache()) {
            return false;
        }

        if ($this->usesLegacyCache()) {
            $cache = new CacheStorageHelper(CacheStorageHelper::ADAPTOR_FILESYSTEM, $this->uniqueCacheDir, null, $this->cacheDir);
            $data  = $cache->get($this->getUniqueWidgetId(), $this->cacheTimeout);

            if ($data) {
                $this->widget->setCached(true);
                $this->setTemplateData($data, true);

                return true;
            }

            return false;
        }
        $cachedItem = $this->cacheProvider->getItem($this->getCacheKey());
        if (!$cachedItem->isHit()) {
            return false;
        }

        $this->widget->setCached(true);
        $this->setTemplateData($cachedItem->get());

        return true;
    }

    /**
     * Get the Translator object.
     *
     * @return TranslatorInterface
     */
    public function getTranslator()
    {
        return $this->translator;
    }

    /**
     * Set security object to check the perimissions.
     *
     * @depreacated
     */
    public function setSecurity(CorePermissions $security): void
    {
        $this->security = $security;
    }

    /**
     * Check if the user has at least one permission of defined array of permissions.
     */
    public function hasPermissions(array $permissions): bool
    {
        if (!$this->security) {
            return true;
        }
        $perm = $this->security->isGranted($permissions, 'RETURN_ARRAY');

        return in_array(true, $perm);
    }

    /**
     * Check if the user has defined permission to see the widgets.
     *
     * @param string $permission
     *
     * @return bool
     */
    public function hasPermission($permission)
    {
        if (!$this->security) {
            return true;
        }

        return $this->security->isGranted($permission);
    }

    /**
     * Checks for cache type. This event should be created by factory thus not legacy approach.
     */
    private function usesLegacyCache(): bool
    {
        return is_null($this->cacheProvider);
    }

    /**
     * We need to cast DateTime objects to strings to use them in the cache key.
     *
     * @param mixed|null $value
     *
     * @throws CouldNotFormatDateTimeException
     */
    private function castDateTimeToString($value): string
    {
        if ($value instanceof \DateTimeInterface) {
            // We use RFC 2822 format because it includes timezone
            return $value->format('r');
        }

        try {
            $value = strval($value);
        } catch (\Exception) {
            throw new CouldNotFormatDateTimeException();
        }

        return $value;
    }
}