File size: 1,934 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php

namespace Mautic\ChannelBundle\PreferenceBuilder;

use Doctrine\Common\Collections\ArrayCollection;
use Mautic\CampaignBundle\Entity\Event;
use Mautic\CampaignBundle\Entity\LeadEventLog;

class ChannelPreferences
{
    /**
     * @var ArrayCollection[]
     */
    private array $organizedByPriority = [];

    public function __construct(
        private Event $event
    ) {
    }

    /**
     * @param int $priority
     *
     * @return $this
     */
    public function addPriority($priority)
    {
        $priority = (int) $priority;

        if (!isset($this->organizedByPriority[$priority])) {
            $this->organizedByPriority[$priority] = new ArrayCollection();
        }

        return $this;
    }

    /**
     * @param int $priority
     *
     * @return $this
     */
    public function addLog(LeadEventLog $log, $priority)
    {
        $priority = (int) $priority;

        $this->addPriority($priority);

        // We have to clone the log to not affect the original assocaited with the MM event itself

        // Clone to remove from Doctrine's ORM memory since we're having to apply a pseudo event
        $log = clone $log;
        $log->setEvent($this->event);

        $this->organizedByPriority[$priority]->set($log->getId(), $log);

        return $this;
    }

    /**
     * Removes a log from all prioritized groups.
     *
     * @return $this
     */
    public function removeLog(LeadEventLog $log)
    {
        foreach ($this->organizedByPriority as $logs) {
            /** @var ArrayCollection<int, LeadEventLog> $logs */
            $logs->remove($log->getId());
        }

        return $this;
    }

    /**
     * @param int $priority
     *
     * @return ArrayCollection|LeadEventLog[]
     */
    public function getLogsByPriority($priority)
    {
        $priority = (int) $priority;

        return $this->organizedByPriority[$priority] ?? new ArrayCollection();
    }
}