Issues (3627)

app/bundles/ReportBundle/Entity/Report.php (1 issue)

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\ReportBundle\Entity;
13
14
use Doctrine\DBAL\Types\Type;
15
use Doctrine\ORM\Mapping as ORM;
16
use Mautic\ApiBundle\Serializer\Driver\ApiMetadataDriver;
17
use Mautic\CoreBundle\Doctrine\Mapping\ClassMetadataBuilder;
18
use Mautic\CoreBundle\Entity\FormEntity;
19
use Mautic\EmailBundle\Validator as EmailAssert;
20
use Mautic\ReportBundle\Scheduler\Enum\SchedulerEnum;
21
use Mautic\ReportBundle\Scheduler\Exception\ScheduleNotValidException;
22
use Mautic\ReportBundle\Scheduler\SchedulerInterface;
23
use Mautic\ReportBundle\Scheduler\Validator as ReportAssert;
24
use Symfony\Component\Validator\Constraints\NotBlank;
25
use Symfony\Component\Validator\Mapping\ClassMetadata;
26
27
class Report extends FormEntity implements SchedulerInterface
28
{
29
    /**
30
     * @var int
31
     */
32
    private $id;
33
34
    /**
35
     * @var string
36
     */
37
    private $name;
38
39
    /**
40
     * @var string
41
     */
42
    private $description;
43
44
    /**
45
     * @var bool
46
     */
47
    private $system = false;
48
49
    /**
50
     * @var string
51
     */
52
    private $source;
53
54
    /**
55
     * @var array
56
     */
57
    private $columns = [];
58
59
    /**
60
     * @var array
61
     */
62
    private $filters = [];
63
64
    /**
65
     * @var array
66
     */
67
    private $tableOrder = [];
68
69
    /**
70
     * @var array
71
     */
72
    private $graphs = [];
73
74
    /**
75
     * @var array
76
     */
77
    private $groupBy = [];
78
79
    /**
80
     * @var array
81
     */
82
    private $aggregators = [];
83
84
    /**
85
     * @var array
86
     */
87
    private $settings = [];
88
89
    /**
90
     * @var bool
91
     */
92
    private $isScheduled = false;
93
94
    /**
95
     * @var string|null
96
     */
97
    private $toAddress;
98
99
    /**
100
     * @var string|null
101
     */
102
    private $scheduleUnit;
103
104
    /**
105
     * @var string|null
106
     */
107
    private $scheduleDay;
108
109
    /**
110
     * @var string|null
111
     */
112
    private $scheduleMonthFrequency;
113
114
    public function __clone()
115
    {
116
        $this->id = null;
117
118
        parent::__clone();
119
    }
120
121
    public static function loadMetadata(ORM\ClassMetadata $metadata)
122
    {
123
        $builder = new ClassMetadataBuilder($metadata);
124
125
        $builder->setTable('reports')
126
            ->setCustomRepositoryClass(ReportRepository::class);
127
128
        $builder->addIdColumns();
129
130
        $builder->addField('system', Type::BOOLEAN, ['columnName'=>'`system`']);
0 ignored issues
show
Deprecated Code introduced by
The constant Doctrine\DBAL\Types\Type::BOOLEAN has been deprecated: Use {@see Types::BOOLEAN} instead. ( Ignorable by Annotation )

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

130
        $builder->addField('system', /** @scrutinizer ignore-deprecated */ Type::BOOLEAN, ['columnName'=>'`system`']);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
131
132
        $builder->addField('source', Type::STRING);
133
134
        $builder->createField('columns', Type::TARRAY)
135
            ->nullable()
136
            ->build();
137
138
        $builder->createField('filters', Type::TARRAY)
139
            ->nullable()
140
            ->build();
141
142
        $builder->createField('tableOrder', Type::TARRAY)
143
            ->columnName('table_order')
144
            ->nullable()
145
            ->build();
146
147
        $builder->createField('graphs', Type::TARRAY)
148
            ->nullable()
149
            ->build();
150
151
        $builder->createField('groupBy', Type::TARRAY)
152
            ->columnName('group_by')
153
            ->nullable()
154
            ->build();
155
156
        $builder->createField('aggregators', Type::TARRAY)
157
            ->columnName('aggregators')
158
            ->nullable()
159
            ->build();
160
161
        $builder->createField('settings', Type::JSON_ARRAY)
162
            ->columnName('settings')
163
            ->nullable()
164
            ->build();
165
166
        $builder->createField('isScheduled', Type::BOOLEAN)
167
            ->columnName('is_scheduled')
168
            ->build();
169
170
        $builder->addNullableField('scheduleUnit', Type::STRING, 'schedule_unit');
171
        $builder->addNullableField('toAddress', Type::STRING, 'to_address');
172
        $builder->addNullableField('scheduleDay', Type::STRING, 'schedule_day');
173
        $builder->addNullableField('scheduleMonthFrequency', Type::STRING, 'schedule_month_frequency');
174
    }
175
176
    public static function loadValidatorMetadata(ClassMetadata $metadata)
177
    {
178
        $metadata->addPropertyConstraint('name', new NotBlank([
179
            'message' => 'mautic.core.name.required',
180
        ]));
181
182
        $metadata->addPropertyConstraint('toAddress', new EmailAssert\MultipleEmailsValid());
183
184
        $metadata->addConstraint(new ReportAssert\ScheduleIsValid());
185
    }
186
187
    /**
188
     * Prepares the metadata for API usage.
189
     *
190
     * @param $metadata
191
     */
192
    public static function loadApiMetadata(ApiMetadataDriver $metadata)
193
    {
194
        $metadata->setGroupPrefix('report')
195
            ->addListProperties(
196
                [
197
                    'id',
198
                    'name',
199
                    'description',
200
                    'system',
201
                    'isScheduled',
202
                ]
203
            )
204
            ->addProperties(
205
                [
206
                    'source',
207
                    'columns',
208
                    'filters',
209
                    'tableOrder',
210
                    'graphs',
211
                    'groupBy',
212
                    'settings',
213
                    'aggregators',
214
                    'scheduleUnit',
215
                    'toAddress',
216
                    'scheduleDay',
217
                    'scheduleMonthFrequency',
218
                ]
219
            )
220
            ->build();
221
    }
222
223
    /**
224
     * Get id.
225
     *
226
     * @return int
227
     */
228
    public function getId()
229
    {
230
        return $this->id;
231
    }
232
233
    public function setId(?int $id): void
234
    {
235
        $this->id = $id;
236
    }
237
238
    /**
239
     * Set name.
240
     *
241
     * @param string $name
242
     *
243
     * @return Report
244
     */
245
    public function setName($name)
246
    {
247
        $this->isChanged('name', $name);
248
        $this->name = $name;
249
250
        return $this;
251
    }
252
253
    /**
254
     * Get name.
255
     *
256
     * @return string
257
     */
258
    public function getName()
259
    {
260
        return $this->name;
261
    }
262
263
    /**
264
     * Set system.
265
     *
266
     * @param string $system
267
     *
268
     * @return Report
269
     */
270
    public function setSystem($system)
271
    {
272
        $this->isChanged('system', $system);
273
        $this->system = $system;
274
275
        return $this;
276
    }
277
278
    /**
279
     * Get system.
280
     *
281
     * @return int
282
     */
283
    public function getSystem()
284
    {
285
        return $this->system;
286
    }
287
288
    /**
289
     * Set source.
290
     *
291
     * @param string $source
292
     *
293
     * @return Report
294
     */
295
    public function setSource($source)
296
    {
297
        $this->isChanged('source', $source);
298
        $this->source = $source;
299
300
        return $this;
301
    }
302
303
    /**
304
     * Get source.
305
     *
306
     * @return string
307
     */
308
    public function getSource()
309
    {
310
        return $this->source;
311
    }
312
313
    /**
314
     * Set columns.
315
     *
316
     * @param string $columns
317
     *
318
     * @return Report
319
     */
320
    public function setColumns($columns)
321
    {
322
        $this->isChanged('columns', $columns);
323
        $this->columns = $columns;
324
325
        return $this;
326
    }
327
328
    /**
329
     * Get columns.
330
     *
331
     * @return array
332
     */
333
    public function getColumns()
334
    {
335
        return $this->columns;
336
    }
337
338
    /**
339
     * Set filters.
340
     *
341
     * @param string $filters
342
     *
343
     * @return Report
344
     */
345
    public function setFilters($filters)
346
    {
347
        $this->isChanged('filters', $filters);
348
        $this->filters = $filters;
349
350
        return $this;
351
    }
352
353
    /**
354
     * Get filters.
355
     *
356
     * @return array
357
     */
358
    public function getFilters()
359
    {
360
        return $this->filters;
361
    }
362
363
    /**
364
     * Get filter value from a specific filter.
365
     *
366
     * @param string $column
367
     *
368
     * @return mixed
369
     *
370
     * @throws \UnexpectedValueException
371
     */
372
    public function getFilterValue($column)
373
    {
374
        foreach ($this->getFilters() as $field) {
375
            if ($column === $field['column']) {
376
                return $field['value'];
377
            }
378
        }
379
380
        throw new \UnexpectedValueException("Column {$column} doesn't have any filter.");
381
    }
382
383
    /**
384
     * Get filter values from a specific filter.
385
     *
386
     * @param string $column
387
     *
388
     * @return array
389
     *
390
     * @throws \UnexpectedValueException
391
     */
392
    public function getFilterValues($column)
393
    {
394
        $values = [];
395
        foreach ($this->getFilters() as $field) {
396
            if ($column === $field['column']) {
397
                $values[] = $field['value'];
398
            }
399
        }
400
401
        if (empty($values)) {
402
            throw new \UnexpectedValueException("Column {$column} doesn't have any filter.");
403
        }
404
405
        return $values;
406
    }
407
408
    /**
409
     * @return mixed
410
     */
411
    public function getDescription()
412
    {
413
        return $this->description;
414
    }
415
416
    /**
417
     * @param mixed $description
418
     */
419
    public function setDescription($description)
420
    {
421
        $this->description = $description;
422
    }
423
424
    /**
425
     * @return mixed
426
     */
427
    public function getTableOrder()
428
    {
429
        return $this->tableOrder;
430
    }
431
432
    public function setTableOrder(array $tableOrder)
433
    {
434
        $this->isChanged('tableOrder', $tableOrder);
435
436
        $this->tableOrder = $tableOrder;
437
    }
438
439
    /**
440
     * @return mixed
441
     */
442
    public function getGraphs()
443
    {
444
        return $this->graphs;
445
    }
446
447
    public function setGraphs(array $graphs)
448
    {
449
        $this->isChanged('graphs', $graphs);
450
451
        $this->graphs = $graphs;
452
    }
453
454
    /**
455
     * @return mixed
456
     */
457
    public function getGroupBy()
458
    {
459
        return $this->groupBy;
460
    }
461
462
    public function setGroupBy(array $groupBy)
463
    {
464
        $this->isChanged('groupBy', $groupBy);
465
466
        $this->groupBy = $groupBy;
467
    }
468
469
    /**
470
     * @return mixed
471
     */
472
    public function getAggregators()
473
    {
474
        return $this->aggregators;
475
    }
476
477
    /**
478
     * @return array
479
     */
480
    public function getAggregatorColumns()
481
    {
482
        return array_map(function ($aggregator) {
483
            return $aggregator['column'];
484
        }, $this->getAggregators());
485
    }
486
487
    /**
488
     * @return array
489
     */
490
    public function getOrderColumns()
491
    {
492
        return array_map(function ($order) {
493
            return $order['column'];
494
        }, $this->getTableOrder());
495
    }
496
497
    /**
498
     * @return array
499
     */
500
    public function getSelectAndAggregatorAndOrderAndGroupByColumns()
501
    {
502
        return array_merge($this->getSelectAndAggregatorColumns(), $this->getOrderColumns(), $this->getGroupBy());
503
    }
504
505
    /**
506
     * @return array
507
     */
508
    public function getSelectAndAggregatorColumns()
509
    {
510
        return array_merge($this->getColumns(), $this->getAggregatorColumns());
511
    }
512
513
    public function setAggregators(array $aggregators)
514
    {
515
        $this->isChanged('aggregators', $aggregators);
516
517
        $this->aggregators = $aggregators;
518
    }
519
520
    public function setSettings(array $settings)
521
    {
522
        $this->isChanged('settings', $settings);
523
524
        $this->settings = $settings;
525
    }
526
527
    /**
528
     * @return array
529
     */
530
    public function getSettings()
531
    {
532
        return $this->settings;
533
    }
534
535
    /**
536
     * @return bool
537
     */
538
    public function isScheduled()
539
    {
540
        return $this->isScheduled;
541
    }
542
543
    /**
544
     * @param bool $isScheduled
545
     */
546
    public function setIsScheduled($isScheduled)
547
    {
548
        $this->isChanged('isScheduled', $isScheduled);
549
550
        $this->isScheduled = $isScheduled;
551
    }
552
553
    /**
554
     * @return string|null
555
     */
556
    public function getToAddress()
557
    {
558
        return $this->toAddress;
559
    }
560
561
    /**
562
     * @param string|null $toAddress
563
     */
564
    public function setToAddress($toAddress)
565
    {
566
        $this->isChanged('toAddress', $toAddress);
567
568
        $this->toAddress = $toAddress;
569
    }
570
571
    /**
572
     * @return string|null
573
     */
574
    public function getScheduleUnit()
575
    {
576
        return $this->scheduleUnit;
577
    }
578
579
    /**
580
     * @param string|null $scheduleUnit
581
     */
582
    public function setScheduleUnit($scheduleUnit)
583
    {
584
        $this->isChanged('scheduleUnit', $scheduleUnit);
585
586
        $this->scheduleUnit = $scheduleUnit;
587
    }
588
589
    /**
590
     * @return string|null
591
     */
592
    public function getScheduleDay()
593
    {
594
        return $this->scheduleDay;
595
    }
596
597
    /**
598
     * @param string|null $scheduleDay
599
     */
600
    public function setScheduleDay($scheduleDay)
601
    {
602
        $this->isChanged('scheduleDay', $scheduleDay);
603
604
        $this->scheduleDay = $scheduleDay;
605
    }
606
607
    /**
608
     * @return string|null
609
     */
610
    public function getScheduleMonthFrequency()
611
    {
612
        return $this->scheduleMonthFrequency;
613
    }
614
615
    /**
616
     * @param string|null $scheduleMonthFrequency
617
     */
618
    public function setScheduleMonthFrequency($scheduleMonthFrequency)
619
    {
620
        $this->scheduleMonthFrequency = $scheduleMonthFrequency;
621
    }
622
623
    public function setAsNotScheduled()
624
    {
625
        $this->setIsScheduled(false);
626
        $this->setToAddress(null);
627
        $this->setScheduleUnit(null);
628
        $this->setScheduleDay(null);
629
        $this->setScheduleMonthFrequency(null);
630
    }
631
632
    public function setAsScheduledNow(string $email): void
633
    {
634
        $this->setIsScheduled(true);
635
        $this->setToAddress($email);
636
        $this->setScheduleUnit(SchedulerEnum::UNIT_NOW);
637
    }
638
639
    public function ensureIsDailyScheduled()
640
    {
641
        $this->setIsScheduled(true);
642
        $this->setScheduleUnit(SchedulerEnum::UNIT_DAILY);
643
        $this->setScheduleDay(null);
644
        $this->setScheduleMonthFrequency(null);
645
    }
646
647
    /**
648
     * @throws ScheduleNotValidException
649
     */
650
    public function ensureIsMonthlyScheduled()
651
    {
652
        if (
653
            !in_array($this->getScheduleMonthFrequency(), SchedulerEnum::getMonthFrequencyForSelect()) ||
654
            !in_array($this->getScheduleDay(), SchedulerEnum::getDayEnumForSelect())
655
        ) {
656
            throw new ScheduleNotValidException();
657
        }
658
        $this->setIsScheduled(true);
659
        $this->setScheduleUnit(SchedulerEnum::UNIT_MONTHLY);
660
    }
661
662
    /**
663
     * @throws ScheduleNotValidException
664
     */
665
    public function ensureIsWeeklyScheduled()
666
    {
667
        if (!in_array($this->getScheduleDay(), SchedulerEnum::getDayEnumForSelect())) {
668
            throw new ScheduleNotValidException();
669
        }
670
        $this->setIsScheduled(true);
671
        $this->setScheduleUnit(SchedulerEnum::UNIT_WEEKLY);
672
        $this->setScheduleMonthFrequency(null);
673
    }
674
675
    public function isScheduledNow(): bool
676
    {
677
        return SchedulerEnum::UNIT_NOW === $this->getScheduleUnit();
678
    }
679
680
    /**
681
     * @return bool
682
     */
683
    public function isScheduledDaily()
684
    {
685
        return SchedulerEnum::UNIT_DAILY === $this->getScheduleUnit();
686
    }
687
688
    /**
689
     * @return bool
690
     */
691
    public function isScheduledWeekly()
692
    {
693
        return SchedulerEnum::UNIT_WEEKLY === $this->getScheduleUnit();
694
    }
695
696
    /**
697
     * @return bool
698
     */
699
    public function isScheduledMonthly()
700
    {
701
        return SchedulerEnum::UNIT_MONTHLY === $this->getScheduleUnit();
702
    }
703
704
    /**
705
     * @return bool
706
     */
707
    public function isScheduledWeekDays()
708
    {
709
        return SchedulerEnum::DAY_WEEK_DAYS === $this->getScheduleDay();
710
    }
711
}
712