File size: 1,779 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
<?php

declare(strict_types=1);

namespace Mautic\CoreBundle\Service;

use Mautic\CoreBundle\Model\NotificationModel;
use Mautic\UserBundle\Entity\User;

final class BulkNotification implements BulkNotificationInterface
{
    /**
     * @var mixed[]
     */
    private array $notifications = [];

    public function __construct(
        private NotificationModel $notificationModel
    ) {
    }

    public function addNotification(
        string $deduplicateValue,
        string $message,
        string $type = null,
        string $header = null,
        string $iconClass = null,
        \DateTime $datetime = null,
        User $user = null
    ): void {
        if (isset($this->notifications[$deduplicateValue])) {
            return;
        }

        $this->notifications[$deduplicateValue] = [
            'message'   => $message,
            'type'      => $type,
            'header'    => $header,
            'iconClass' => $iconClass,
            'datetime'  => $datetime,
            'user'      => $user,
        ];
    }

    /**
     * @param \DateTime|null $deduplicateDateTimeFrom If last 24 hours for deduplication does not fit, change it here
     */
    public function flush(\DateTime $deduplicateDateTimeFrom = null): void
    {
        foreach ($this->notifications as $deduplicateValue => $data) {
            $this->notificationModel->addNotification(
                $data['message'],
                $data['type'],
                false,
                $data['header'],
                $data['iconClass'],
                $data['datetime'],
                $data['user'],
                $deduplicateValue,
                $deduplicateDateTimeFrom
            );
            unset($this->notifications[$deduplicateValue]);
        }
    }
}