File size: 3,747 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php

namespace Mautic\CampaignBundle\EventCollector\Builder;

use Mautic\CampaignBundle\Entity\Event;

class ConnectionBuilder
{
    private static array $eventTypes = [];

    /**
     * @var array
     */
    private static $connectionRestrictions = ['anchor' => []];

    /**
     * Used by JS/JsPlumb to restrict how events can be associated to each other in the UI.
     *
     * @return array
     */
    public static function buildRestrictionsArray(array $events)
    {
        // Reset restrictions
        self::$connectionRestrictions = ['anchor' => []];

        // Build the restrictions
        self::$eventTypes = array_fill_keys(array_keys($events), []);
        foreach ($events as $eventType => $typeEvents) {
            foreach ($typeEvents as $key => $event) {
                self::addTypeConnection($eventType, $key, $event);
            }
        }

        return self::$connectionRestrictions;
    }

    /**
     * @param string $eventType
     * @param string $key
     */
    private static function addTypeConnection($eventType, $key, array $event): void
    {
        if (!isset(self::$connectionRestrictions[$key])) {
            self::$connectionRestrictions[$key] = [
                'source' => self::$eventTypes,
                'target' => self::$eventTypes,
            ];
        }

        if (!isset(self::$connectionRestrictions[$key])) {
            self::$connectionRestrictions['anchor'][$key] = [];
        }

        if (isset($event['connectionRestrictions'])) {
            foreach ($event['connectionRestrictions'] as $restrictionType => $restrictions) {
                self::addRestriction($key, $restrictionType, $restrictions);
            }
        }

        self::addDeprecatedAnchorRestrictions($eventType, $key, $event);
    }

    /**
     * @param string $key
     * @param string $restrictionType
     */
    private static function addRestriction($key, $restrictionType, array $restrictions): void
    {
        switch ($restrictionType) {
            case 'source':
            case 'target':
                foreach ($restrictions as $groupType => $groupRestrictions) {
                    self::$connectionRestrictions[$key][$restrictionType][$groupType] += $groupRestrictions;
                }
                break;
            case 'anchor':
                foreach ($restrictions as $anchor) {
                    [$group, $anchor]                                               = explode('.', $anchor);
                    self::$connectionRestrictions[$restrictionType][$group][$key][] = $anchor;
                }

                break;
        }
    }

    /**
     * @deprecated 2.6.0 to be removed in 3.0; BC support
     *
     * @param string $eventType
     * @param string $key
     */
    private static function addDeprecatedAnchorRestrictions($eventType, $key, array $event): void
    {
        switch ($eventType) {
            case Event::TYPE_DECISION:
                if (isset($event['associatedActions'])) {
                    self::$connectionRestrictions[$key]['target']['action'] += $event['associatedActions'];
                }
                break;
            case Event::TYPE_ACTION:
                if (isset($event['associatedDecisions'])) {
                    self::$connectionRestrictions[$key]['source']['decision'] += $event['associatedDecisions'];
                }
                break;
        }

        if (isset($event['anchorRestrictions'])) {
            foreach ($event['anchorRestrictions'] as $restriction) {
                [$group, $anchor]                                       = explode('.', $restriction);
                self::$connectionRestrictions['anchor'][$key][$group][] = $anchor;
            }
        }
    }
}