Issues (3627)

app/bundles/DashboardBundle/Entity/Widget.php (2 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\Entity;
13
14
use Doctrine\DBAL\Types\Type;
15
use Doctrine\ORM\Mapping as ORM;
16
use Mautic\CoreBundle\Doctrine\Mapping\ClassMetadataBuilder;
17
use Mautic\CoreBundle\Entity\FormEntity;
18
use Mautic\CoreBundle\Helper\InputHelper;
19
use Symfony\Component\Validator\Constraints\NotBlank;
20
use Symfony\Component\Validator\Mapping\ClassMetadata;
21
22
/**
23
 * Class Widget.
24
 */
25
class Widget extends FormEntity
26
{
27
    /**
28
     * @var int
29
     */
30
    private $id;
31
32
    /**
33
     * @var string
34
     */
35
    private $name;
36
37
    /**
38
     * @var int
39
     */
40
    private $width;
41
42
    /**
43
     * @var int
44
     */
45
    private $height;
46
47
    /**
48
     * @var int
49
     */
50
    private $ordering;
51
52
    /**
53
     * @var string
54
     */
55
    private $type;
56
57
    /**
58
     * @var array
59
     */
60
    private $params = [];
61
62
    /**
63
     * @var string
64
     */
65
    private $template;
66
67
    /**
68
     * @var string
69
     */
70
    private $errorMessage;
71
72
    /**
73
     * @var bool
74
     */
75
    private $cached = false;
76
77
    /**
78
     * @var int
79
     */
80
    private $loadTime = 0;
81
82
    /**
83
     * @var int (minutes)
84
     */
85
    private $cacheTimeout;
86
87
    /**
88
     * @var array
89
     */
90
    private $templateData = [];
91
92
    public function __clone()
93
    {
94
        $this->id = null;
95
96
        parent::__clone();
97
    }
98
99
    public static function loadMetadata(ORM\ClassMetadata $metadata)
100
    {
101
        $builder = new ClassMetadataBuilder($metadata);
102
        $builder->setTable('widgets');
103
        $builder->setCustomRepositoryClass(WidgetRepository::class);
104
        $builder->addIdColumns('name', false);
105
        $builder->addField('type', Type::STRING);
106
        $builder->addField('width', Type::INTEGER);
107
        $builder->addField('height', Type::INTEGER);
108
        $builder->addNullableField('cacheTimeout', Type::INTEGER, 'cache_timeout');
109
        $builder->addNullableField('ordering', Type::INTEGER);
110
        $builder->addNullableField('params', Type::TARRAY);
111
    }
112
113
    public static function loadValidatorMetadata(ClassMetadata $metadata)
114
    {
115
        $metadata->addPropertyConstraint('type', new NotBlank([
116
            'message' => 'mautic.core.type.required',
117
        ]));
118
    }
119
120
    /**
121
     * Get id.
122
     *
123
     * @return int
124
     */
125
    public function getId()
126
    {
127
        return $this->id;
128
    }
129
130
    /**
131
     * Set name.
132
     *
133
     * @param string $name
134
     *
135
     * @return Widget
136
     */
137
    public function setName($name)
138
    {
139
        $this->name = InputHelper::string($name);
140
        $this->isChanged('name', $this->name);
141
142
        return $this;
143
    }
144
145
    /**
146
     * Get name.
147
     *
148
     * @return string
149
     */
150
    public function getName()
151
    {
152
        return $this->name;
153
    }
154
155
    /**
156
     * Set type.
157
     *
158
     * @param string $type
159
     *
160
     * @return Widget
161
     */
162
    public function setType($type)
163
    {
164
        $this->type = InputHelper::string($type);
165
        $this->isChanged('type', $this->type);
166
167
        return $this;
168
    }
169
170
    /**
171
     * Get type.
172
     *
173
     * @return string
174
     */
175
    public function getType()
176
    {
177
        return $this->type;
178
    }
179
180
    /**
181
     * Set width.
182
     *
183
     * @param int $width
184
     *
185
     * @return Widget
186
     */
187
    public function setWidth($width)
188
    {
189
        $this->width = (int) $width;
190
        $this->isChanged('width', $this->width);
191
192
        return $this;
193
    }
194
195
    /**
196
     * Get width.
197
     *
198
     * @return int
199
     */
200
    public function getWidth()
201
    {
202
        return $this->width;
203
    }
204
205
    /**
206
     * Set height.
207
     *
208
     * @param int $height
209
     *
210
     * @return Widget
211
     */
212
    public function setHeight($height)
213
    {
214
        $this->height = (int) $height;
215
        $this->isChanged('height', $this->height);
216
217
        return $this;
218
    }
219
220
    /**
221
     * Get cache timeout.
222
     *
223
     * @return int (minutes)
224
     */
225
    public function getCacheTimeout()
226
    {
227
        return $this->cacheTimeout;
228
    }
229
230
    /**
231
     * Set cache timeout.
232
     *
233
     * @param int $cacheTimeout (minutes)
234
     *
235
     * @return Widget
236
     */
237
    public function setCacheTimeout($cacheTimeout)
238
    {
239
        $this->isChanged('cacheTimeout', $cacheTimeout);
240
        $this->cacheTimeout = $cacheTimeout;
241
242
        return $this;
243
    }
244
245
    /**
246
     * Get height.
247
     *
248
     * @return int
249
     */
250
    public function getHeight()
251
    {
252
        return $this->height;
253
    }
254
255
    /**
256
     * Set ordering.
257
     *
258
     * @param int $ordering
259
     *
260
     * @return Widget
261
     */
262
    public function setOrdering($ordering)
263
    {
264
        $this->ordering = (int) $ordering;
265
        $this->isChanged('ordering', $this->ordering);
266
267
        return $this;
268
    }
269
270
    /**
271
     * Get ordering.
272
     *
273
     * @return int
274
     */
275
    public function getOrdering()
276
    {
277
        return $this->ordering;
278
    }
279
280
    /**
281
     * Get params.
282
     *
283
     * @return array $params
284
     */
285
    public function getParams()
286
    {
287
        return $this->params;
288
    }
289
290
    /**
291
     * Set params.
292
     *
293
     * @return Widget
294
     */
295
    public function setParams(array $params)
296
    {
297
        $this->isChanged('params', $params);
298
        $this->params = $params;
299
300
        return $this;
301
    }
302
303
    /**
304
     * Set template.
305
     *
306
     * @param string $template
307
     *
308
     * @return Widget
309
     */
310
    public function setTemplate($template)
311
    {
312
        $this->isChanged('template', $template);
313
        $this->template = $template;
314
315
        return $this;
316
    }
317
318
    /**
319
     * Get template.
320
     *
321
     * @return string
322
     */
323
    public function getTemplate()
324
    {
325
        return $this->template;
326
    }
327
328
    /**
329
     * Get template data.
330
     *
331
     * @return array $templateData
332
     */
333
    public function getTemplateData()
334
    {
335
        return $this->templateData;
336
    }
337
338
    /**
339
     * Set template data.
340
     *
341
     * @return Widget
342
     */
343
    public function setTemplateData(array $templateData)
344
    {
345
        $this->isChanged('templateData', $templateData);
346
        $this->templateData = $templateData;
347
348
        return $this;
349
    }
350
351
    /**
352
     * Set errorMessage.
353
     *
354
     * @param string $errorMessage
355
     *
356
     * @return Widget
357
     */
358
    public function setErrorMessage($errorMessage)
359
    {
360
        $this->errorMessage = $errorMessage;
361
362
        return $this;
363
    }
364
365
    /**
366
     * Get errorMessage.
367
     *
368
     * @return string
369
     */
370
    public function getErrorMessage()
371
    {
372
        return $this->errorMessage;
373
    }
374
375
    /**
376
     * Set cached flag.
377
     *
378
     * @param string $cached
379
     *
380
     * @return Widget
381
     */
382
    public function setCached($cached)
383
    {
384
        $this->cached = $cached;
0 ignored issues
show
Documentation Bug introduced by
The property $cached was declared of type boolean, but $cached is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
385
386
        return $this;
387
    }
388
389
    /**
390
     * Get cached.
391
     *
392
     * @return bool
393
     */
394
    public function isCached()
395
    {
396
        return $this->cached;
397
    }
398
399
    /**
400
     * Set loadTime.
401
     *
402
     * @param string $loadTime
403
     *
404
     * @return Widget
405
     */
406
    public function setLoadTime($loadTime)
407
    {
408
        $this->loadTime = $loadTime;
0 ignored issues
show
Documentation Bug introduced by
The property $loadTime was declared of type integer, but $loadTime is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
409
410
        return $this;
411
    }
412
413
    /**
414
     * Get loadTime.
415
     *
416
     * @return int
417
     */
418
    public function getLoadTime()
419
    {
420
        return $this->loadTime;
421
    }
422
423
    /**
424
     * @return array
425
     */
426
    public function toArray()
427
    {
428
        return [
429
            'name'     => $this->getName(),
430
            'width'    => $this->getWidth(),
431
            'height'   => $this->getHeight(),
432
            'ordering' => $this->getOrdering(),
433
            'type'     => $this->getType(),
434
            'params'   => $this->getParams(),
435
            'template' => $this->getTemplate(),
436
        ];
437
    }
438
}
439