Issues (3627)

app/bundles/EmailBundle/Entity/Stat.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\EmailBundle\Entity;
13
14
use Doctrine\Common\Collections\ArrayCollection;
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\IpAddress;
19
use Mautic\LeadBundle\Entity\Lead;
20
use Mautic\LeadBundle\Entity\LeadList;
21
22
class Stat
23
{
24
    /** @var int Limit number of stored 'openDetails' */
25
    const MAX_OPEN_DETAILS = 1000;
26
27
    /**
28
     * @var int|null
29
     */
30
    private $id;
31
32
    /**
33
     * @var Email|null
34
     */
35
    private $email;
36
37
    /**
38
     * @var Lead|null
39
     */
40
    private $lead;
41
42
    /**
43
     * @var string|null
44
     */
45
    private $emailAddress;
46
47
    /**
48
     * @var LeadList|null
49
     */
50
    private $list;
51
52
    /**
53
     * @var IpAddress|null
54
     */
55
    private $ipAddress;
56
57
    /**
58
     * @var \DateTime|null
59
     */
60
    private $dateSent;
61
62
    /**
63
     * @var bool
64
     */
65
    private $isRead = false;
66
67
    /**
68
     * @var bool
69
     */
70
    private $isFailed = false;
71
72
    /**
73
     * @var bool
74
     */
75
    private $viewedInBrowser = false;
76
77
    /**
78
     * @var \DateTime|null
79
     */
80
    private $dateRead;
81
82
    /**
83
     * @var string|null
84
     */
85
    private $trackingHash;
86
87
    /**
88
     * @var int
89
     */
90
    private $retryCount = 0;
91
92
    /**
93
     * @var string|null
94
     */
95
    private $source;
96
97
    /**
98
     * @var int|null
99
     */
100
    private $sourceId;
101
102
    /**
103
     * @var array
104
     */
105
    private $tokens = [];
106
107
    /**
108
     * @var Copy|null
109
     */
110
    private $storedCopy;
111
112
    /**
113
     * @var int
114
     */
115
    private $openCount = 0;
116
117
    /**
118
     * @var \DateTime|null
119
     */
120
    private $lastOpened;
121
122
    /**
123
     * @var array
124
     */
125
    private $openDetails = [];
126
127
    /**
128
     * @var ArrayCollection|EmailReply[]
129
     */
130
    private $replies;
131
132
    public function __construct()
133
    {
134
        $this->replies = new ArrayCollection();
135
    }
136
137
    public static function loadMetadata(ORM\ClassMetadata $metadata)
138
    {
139
        $builder = new ClassMetadataBuilder($metadata);
140
141
        $builder->setTable('email_stats')
142
            ->setCustomRepositoryClass('Mautic\EmailBundle\Entity\StatRepository')
143
            ->addIndex(['email_id', 'lead_id'], 'stat_email_search')
144
            ->addIndex(['lead_id', 'email_id'], 'stat_email_search2')
145
            ->addIndex(['is_failed'], 'stat_email_failed_search')
146
            ->addIndex(['is_read', 'date_sent'], 'is_read_date_sent')
147
            ->addIndex(['tracking_hash'], 'stat_email_hash_search')
148
            ->addIndex(['source', 'source_id'], 'stat_email_source_search')
149
            ->addIndex(['date_sent'], 'email_date_sent')
150
            ->addIndex(['date_read', 'lead_id'], 'email_date_read_lead');
151
152
        $builder->addBigIntIdField();
153
154
        $builder->createManyToOne('email', 'Email')
155
            ->inversedBy('stats')
156
            ->addJoinColumn('email_id', 'id', true, false, 'SET NULL')
157
            ->build();
158
159
        $builder->addLead(true, 'SET NULL');
160
161
        $builder->createField('emailAddress', 'string')
162
            ->columnName('email_address')
163
            ->build();
164
165
        $builder->createManyToOne('list', 'Mautic\LeadBundle\Entity\LeadList')
166
            ->addJoinColumn('list_id', 'id', true, false, 'SET NULL')
167
            ->build();
168
169
        $builder->addIpAddress(true);
170
171
        $builder->createField('dateSent', 'datetime')
172
            ->columnName('date_sent')
173
            ->build();
174
175
        $builder->createField('isRead', 'boolean')
176
            ->columnName('is_read')
177
            ->build();
178
179
        $builder->createField('isFailed', 'boolean')
180
            ->columnName('is_failed')
181
            ->build();
182
183
        $builder->createField('viewedInBrowser', 'boolean')
184
            ->columnName('viewed_in_browser')
185
            ->build();
186
187
        $builder->createField('dateRead', 'datetime')
188
            ->columnName('date_read')
189
            ->nullable()
190
            ->build();
191
192
        $builder->createField('trackingHash', 'string')
193
            ->columnName('tracking_hash')
194
            ->nullable()
195
            ->build();
196
197
        $builder->createField('retryCount', 'integer')
198
            ->columnName('retry_count')
199
            ->nullable()
200
            ->build();
201
202
        $builder->createField('source', 'string')
203
            ->nullable()
204
            ->build();
205
206
        $builder->createField('sourceId', 'integer')
207
            ->columnName('source_id')
208
            ->nullable()
209
            ->build();
210
211
        $builder->createField('tokens', 'array')
212
            ->nullable()
213
            ->build();
214
215
        $builder->createManyToOne('storedCopy', 'Mautic\EmailBundle\Entity\Copy')
216
            ->addJoinColumn('copy_id', 'id', true, false, 'SET NULL')
217
            ->build();
218
219
        $builder->addNullableField('openCount', 'integer', 'open_count');
220
221
        $builder->addNullableField('lastOpened', 'datetime', 'last_opened');
222
223
        $builder->addNullableField('openDetails', 'array', 'open_details');
224
225
        $builder->createOneToMany('replies', EmailReply::class)
226
            ->mappedBy('stat')
227
            ->fetchExtraLazy()
228
            ->cascadeAll()
229
            ->build();
230
    }
231
232
    /**
233
     * Prepares the metadata for API usage.
234
     */
235
    public static function loadApiMetadata(ApiMetadataDriver $metadata)
236
    {
237
        $metadata->setGroupPrefix('stat')
238
            ->addProperties(
239
                [
240
                    'id',
241
                    'emailAddress',
242
                    'ipAddress',
243
                    'dateSent',
244
                    'isRead',
245
                    'isFailed',
246
                    'dateRead',
247
                    'retryCount',
248
                    'source',
249
                    'openCount',
250
                    'lastOpened',
251
                    'sourceId',
252
                    'trackingHash',
253
                    'viewedInBrowser',
254
                    'lead',
255
                    'email',
256
                ]
257
            )
258
            ->build();
259
    }
260
261
    /**
262
     * @return \DateTime|null
263
     */
264
    public function getDateRead()
265
    {
266
        return $this->dateRead;
267
    }
268
269
    /**
270
     * @param \DateTime|null $dateRead
271
     */
272
    public function setDateRead($dateRead)
273
    {
274
        $this->dateRead = $dateRead;
275
    }
276
277
    /**
278
     * @return \DateTime|null
279
     */
280
    public function getDateSent()
281
    {
282
        return $this->dateSent;
283
    }
284
285
    /**
286
     * @param \DateTime|null $dateSent
287
     */
288
    public function setDateSent($dateSent)
289
    {
290
        $this->dateSent = $dateSent;
291
    }
292
293
    /**
294
     * @return Email|null
295
     */
296
    public function getEmail()
297
    {
298
        return $this->email;
299
    }
300
301
    public function setEmail(Email $email = null)
302
    {
303
        $this->email = $email;
304
    }
305
306
    /**
307
     * @return id|null
308
     */
309
    public function getId()
310
    {
311
        return $this->id;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->id also could return the type integer which is incompatible with the documented return type Mautic\EmailBundle\Entity\id|null.
Loading history...
312
    }
313
314
    /**
315
     * @return IpAddress|null
316
     */
317
    public function getIpAddress()
318
    {
319
        return $this->ipAddress;
320
    }
321
322
    /**
323
     * @param IpAddress|null $ip
324
     */
325
    public function setIpAddress(IpAddress $ip)
326
    {
327
        $this->ipAddress = $ip;
328
    }
329
330
    /**
331
     * @return bool
332
     */
333
    public function getIsRead()
334
    {
335
        return $this->isRead;
336
    }
337
338
    /**
339
     * @return bool
340
     */
341
    public function isRead()
342
    {
343
        return $this->getIsRead();
344
    }
345
346
    /**
347
     * @param bool $isRead
348
     */
349
    public function setIsRead($isRead)
350
    {
351
        $this->isRead = $isRead;
352
    }
353
354
    /**
355
     * @return Lead|null
356
     */
357
    public function getLead()
358
    {
359
        return $this->lead;
360
    }
361
362
    public function setLead(Lead $lead = null)
363
    {
364
        $this->lead = $lead;
365
    }
366
367
    /**
368
     * @return string|null
369
     */
370
    public function getTrackingHash()
371
    {
372
        return $this->trackingHash;
373
    }
374
375
    /**
376
     * @param string|null $trackingHash
377
     */
378
    public function setTrackingHash($trackingHash)
379
    {
380
        $this->trackingHash = $trackingHash;
381
    }
382
383
    /**
384
     * @return LeadList|null
385
     */
386
    public function getList()
387
    {
388
        return $this->list;
389
    }
390
391
    /**
392
     * @param LeadList|null $list
393
     */
394
    public function setList($list)
395
    {
396
        $this->list = $list;
397
    }
398
399
    /**
400
     * @return int
401
     */
402
    public function getRetryCount()
403
    {
404
        return $this->retryCount;
405
    }
406
407
    /**
408
     * @param int $retryCount
409
     */
410
    public function setRetryCount($retryCount)
411
    {
412
        $this->retryCount = $retryCount;
413
    }
414
415
    /**
416
     * Increase the retry count.
417
     */
418
    public function upRetryCount()
419
    {
420
        ++$this->retryCount;
421
    }
422
423
    /**
424
     * @return bool
425
     */
426
    public function getIsFailed()
427
    {
428
        return $this->isFailed;
429
    }
430
431
    /**
432
     * @param bool $isFailed
433
     */
434
    public function setIsFailed($isFailed)
435
    {
436
        $this->isFailed = $isFailed;
437
    }
438
439
    /**
440
     * @return bool
441
     */
442
    public function isFailed()
443
    {
444
        return $this->getIsFailed();
445
    }
446
447
    /**
448
     * @return string|null
449
     */
450
    public function getEmailAddress()
451
    {
452
        return $this->emailAddress;
453
    }
454
455
    /**
456
     * @param string|null $emailAddress
457
     */
458
    public function setEmailAddress($emailAddress)
459
    {
460
        $this->emailAddress = $emailAddress;
461
    }
462
463
    /**
464
     * @return bool
465
     */
466
    public function getViewedInBrowser()
467
    {
468
        return $this->viewedInBrowser;
469
    }
470
471
    /**
472
     * @param bool $viewedInBrowser
473
     */
474
    public function setViewedInBrowser($viewedInBrowser)
475
    {
476
        $this->viewedInBrowser = $viewedInBrowser;
477
    }
478
479
    /**
480
     * @return string|null
481
     */
482
    public function getSource()
483
    {
484
        return $this->source;
485
    }
486
487
    /**
488
     * @param string|null $source
489
     */
490
    public function setSource($source)
491
    {
492
        $this->source = $source;
493
    }
494
495
    /**
496
     * @return int|null
497
     */
498
    public function getSourceId()
499
    {
500
        return $this->sourceId;
501
    }
502
503
    /**
504
     * @param int|null $sourceId
505
     */
506
    public function setSourceId($sourceId)
507
    {
508
        $this->sourceId = (int) $sourceId;
509
    }
510
511
    /**
512
     * @return array
513
     */
514
    public function getTokens()
515
    {
516
        return $this->tokens;
517
    }
518
519
    public function setTokens(array $tokens)
520
    {
521
        $this->tokens = $tokens;
522
    }
523
524
    /**
525
     * @return int
526
     */
527
    public function getOpenCount()
528
    {
529
        return $this->openCount;
530
    }
531
532
    /**
533
     * @param int $openCount
534
     *
535
     * @return Stat
536
     */
537
    public function setOpenCount($openCount)
538
    {
539
        $this->openCount = $openCount;
540
541
        return $this;
542
    }
543
544
    /**
545
     * @param string $details
546
     */
547
    public function addOpenDetails($details)
548
    {
549
        if (self::MAX_OPEN_DETAILS > $this->getOpenCount()) {
550
            $this->openDetails[] = $details;
551
        }
552
553
        ++$this->openCount;
554
    }
555
556
    /**
557
     * Up the sent count.
558
     *
559
     * @return Stat
560
     */
561
    public function upOpenCount()
562
    {
563
        $count           = (int) $this->openCount + 1;
564
        $this->openCount = $count;
565
566
        return $this;
567
    }
568
569
    /**
570
     * @return \DateTime|null
571
     */
572
    public function getLastOpened()
573
    {
574
        return $this->lastOpened;
575
    }
576
577
    /**
578
     * @param \DateTime|null $lastOpened
579
     *
580
     * @return Stat
581
     */
582
    public function setLastOpened($lastOpened)
583
    {
584
        $this->lastOpened = $lastOpened;
585
586
        return $this;
587
    }
588
589
    /**
590
     * @return array
591
     */
592
    public function getOpenDetails()
593
    {
594
        return $this->openDetails;
595
    }
596
597
    /**
598
     * @return Stat
599
     */
600
    public function setOpenDetails(array $openDetails)
601
    {
602
        $this->openDetails = $openDetails;
603
604
        return $this;
605
    }
606
607
    /**
608
     * @return Copy|null
609
     */
610
    public function getStoredCopy()
611
    {
612
        return $this->storedCopy;
613
    }
614
615
    /**
616
     * @return Stat
617
     */
618
    public function setStoredCopy(Copy $storedCopy)
619
    {
620
        $this->storedCopy = $storedCopy;
621
622
        return $this;
623
    }
624
625
    /**
626
     * @return ArrayCollection|EmailReply[]
627
     */
628
    public function getReplies()
629
    {
630
        return $this->replies;
631
    }
632
633
    public function addReply(EmailReply $reply)
634
    {
635
        $this->replies[] = $reply;
636
    }
637
}
638