Issues (3627)

DashboardBundle/Event/WidgetDetailEvent.php (6 issues)

1
<?php
2
3
/*
4
 * @copyright   2014 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\DashboardBundle\Event;
13
14
use Mautic\CoreBundle\Event\CommonEvent;
15
use Mautic\CoreBundle\Helper\CacheStorageHelper;
16
use Mautic\CoreBundle\Security\Permissions\CorePermissions;
17
use Mautic\DashboardBundle\Entity\Widget;
18
use Symfony\Component\Translation\TranslatorInterface;
19
20
/**
21
 * Class WidgetDetailEvent.
22
 */
23
class WidgetDetailEvent extends CommonEvent
24
{
25
    protected $widget;
26
    protected $type;
27
    protected $template;
28
    protected $templateData = [];
29
    protected $errorMessage;
30
    protected $uniqueId;
31
    protected $cacheDir;
32
    protected $uniqueCacheDir;
33
    protected $cacheTimeout;
34
    protected $startTime = 0;
35
    protected $loadTime  = 0;
36
    protected $translator;
37
38
    /**
39
     * @var CorePermissions
40
     */
41
    protected $security;
42
43
    public function __construct(TranslatorInterface $translator)
44
    {
45
        $this->translator = $translator;
46
        $this->startTime  = microtime(true);
47
    }
48
49
    /**
50
     * Set the cache dir.
51
     *
52
     * @param string $cacheDir
53
     */
54
    public function setCacheDir($cacheDir, $uniqueCacheDir = null)
55
    {
56
        $this->cacheDir       = $cacheDir;
57
        $this->uniqueCacheDir = $uniqueCacheDir;
58
    }
59
60
    /**
61
     * Set the cache timeout.
62
     *
63
     * @param string $cacheTimeout
64
     */
65
    public function setCacheTimeout($cacheTimeout)
66
    {
67
        $this->cacheTimeout = (int) $cacheTimeout;
68
    }
69
70
    /**
71
     * Set the widget type.
72
     *
73
     * @param string $type
74
     */
75
    public function setType($type)
76
    {
77
        $this->type = $type;
78
    }
79
80
    /**
81
     * Get the widget type.
82
     *
83
     * @return string $type
84
     */
85
    public function getType()
86
    {
87
        return $this->type;
88
    }
89
90
    /**
91
     * Set the widget entity.
92
     */
93
    public function setWidget(Widget $widget)
94
    {
95
        $this->widget = $widget;
96
97
        $params = $widget->getParams();
98
99
        // Set required params if undefined
100
        if (!isset($params['timeUnit'])) {
101
            $params['timeUnit'] = null;
102
        }
103
104
        if (!isset($params['amount'])) {
105
            $params['amount'] = null;
106
        }
107
108
        if (!isset($params['dateFormat'])) {
109
            $params['dateFormat'] = null;
110
        }
111
112
        if (!isset($params['filter'])) {
113
            $params['filter'] = [];
114
        }
115
116
        $widget->setParams($params);
117
118
        $this->setType($widget->getType());
119
        $this->setCacheTimeout($widget->getCacheTimeout());
120
    }
121
122
    /**
123
     * Returns the widget entity.
124
     *
125
     * @return Widget $widget
126
     */
127
    public function getWidget()
128
    {
129
        return $this->widget;
130
    }
131
132
    /**
133
     * Set the widget template.
134
     *
135
     * @param string $template
136
     */
137
    public function setTemplate($template)
138
    {
139
        $this->template = $template;
140
        $this->widget->setTemplate($template);
141
    }
142
143
    /**
144
     * Get the widget template.
145
     *
146
     * @return string $template
147
     */
148
    public function getTemplate()
149
    {
150
        return $this->template;
151
    }
152
153
    /**
154
     * Set the widget template data.
155
     */
156
    public function setTemplateData(array $templateData, $skipCache = false)
157
    {
158
        $this->templateData = $templateData;
159
        $this->widget->setTemplateData($templateData);
160
        $this->widget->setLoadTime(abs(microtime(true) - $this->startTime));
161
162
        // Store the template data to the cache
163
        if (!$skipCache && $this->cacheDir && $this->widget->getCacheTimeout() > 0) {
164
            $cache = new CacheStorageHelper(CacheStorageHelper::ADAPTOR_FILESYSTEM, $this->uniqueCacheDir, null, $this->cacheDir);
165
            // must pass a DateTime object or a int of seconds to expire as 3rd attribute to set().
166
            $expireTime = $this->widget->getCacheTimeout() * 60;
167
            $cache->set($this->getUniqueWidgetId(), $templateData, (int) $expireTime);
168
        }
169
    }
170
171
    /**
172
     * Get the widget template data.
173
     *
174
     * @return string $templateData
175
     */
176
    public function getTemplateData()
177
    {
178
        return $this->templateData;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->templateData returns the type array which is incompatible with the documented return type string.
Loading history...
179
    }
180
181
    /**
182
     * Set en error message.
183
     *
184
     * @param array $errorMessage
185
     */
186
    public function setErrorMessage($errorMessage)
187
    {
188
        $this->errorMessage = $errorMessage;
189
        $this->widget->setErrorMessage($errorMessage);
190
    }
191
192
    /**
193
     * Get an error message.
194
     *
195
     * @return string $errorMessage
196
     */
197
    public function getErrorMessage()
198
    {
199
        return $this->errorMessage;
200
    }
201
202
    /**
203
     * Build a unique ID from type and widget params.
204
     *
205
     * @return string
206
     */
207
    public function getUniqueWidgetId()
208
    {
209
        if ($this->uniqueId) {
210
            return $this->uniqueId;
211
        }
212
213
        $params = $this->getWidget()->getParams();
214
        // Unset dateFrom and dateTo since they constantly change
215
        unset($params['dateFrom'], $params['dateTo']);
216
217
        $uniqueSettings = [
218
            'params' => $params,
219
            'width'  => $this->getWidget()->getWidth(),
220
            'height' => $this->getWidget()->getHeight(),
221
            'locale' => $this->translator->getLocale(),
222
        ];
223
224
        return $this->uniqueId = $this->getType().'_'.substr(md5(json_encode($uniqueSettings)), 0, 16);
225
    }
226
227
    /**
228
     * Checks the cache for the widget data.
229
     * If cache exists, it sets the TemplateData.
230
     *
231
     * @return string
232
     */
233
    public function isCached()
234
    {
235
        if (!$this->cacheDir) {
236
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
237
        }
238
239
        $cache = new CacheStorageHelper(CacheStorageHelper::ADAPTOR_FILESYSTEM, $this->uniqueCacheDir, null, $this->cacheDir);
240
        $data  = $cache->get($this->getUniqueWidgetId(), $this->cacheTimeout);
0 ignored issues
show
Deprecated Code introduced by
The function Mautic\CoreBundle\Helper\CacheStorageHelper::get() has been deprecated. ( Ignorable by Annotation )

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

240
        $data  = /** @scrutinizer ignore-deprecated */ $cache->get($this->getUniqueWidgetId(), $this->cacheTimeout);
Loading history...
241
242
        if ($data) {
243
            $this->widget->setCached(true);
244
            $this->setTemplateData($data, true);
0 ignored issues
show
It seems like $data can also be of type true; however, parameter $templateData of Mautic\DashboardBundle\E...vent::setTemplateData() does only seem to accept array, 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

244
            $this->setTemplateData(/** @scrutinizer ignore-type */ $data, true);
Loading history...
245
246
            return true;
0 ignored issues
show
Bug Best Practice introduced by
The expression return true returns the type true which is incompatible with the documented return type string.
Loading history...
247
        }
248
249
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
250
    }
251
252
    /**
253
     * Get the Translator object.
254
     *
255
     * @return TranslatorInterface
256
     */
257
    public function getTranslator()
258
    {
259
        return $this->translator;
260
    }
261
262
    /**
263
     * Set security object to check the perimissions.
264
     */
265
    public function setSecurity(CorePermissions $security)
266
    {
267
        $this->security = $security;
268
    }
269
270
    /**
271
     * Check if the user has at least one permission of defined array of permissions.
272
     *
273
     * @return bool
274
     */
275
    public function hasPermissions(array $permissions)
276
    {
277
        if (!$this->security) {
278
            return true;
279
        }
280
        $perm = $this->security->isGranted($permissions, 'RETURN_ARRAY');
281
282
        return in_array(true, $perm);
283
    }
284
285
    /**
286
     * Check if the user has defined permission to see the widgets.
287
     *
288
     * @param string $permission
289
     *
290
     * @return bool
291
     */
292
    public function hasPermission($permission)
293
    {
294
        if (!$this->security) {
295
            return true;
296
        }
297
298
        return $this->security->isGranted($permission);
299
    }
300
}
301