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

namespace Mautic\CampaignBundle\Helper;

use Mautic\CampaignBundle\Entity\ChannelInterface;
use Mautic\CampaignBundle\Entity\Event;
use Mautic\CampaignBundle\EventCollector\Accessor\Event\AbstractEventAccessor;

class ChannelExtractor
{
    public static function setChannel(ChannelInterface $entity, Event $event, AbstractEventAccessor $eventConfig): void
    {
        // Allow event to update itself
        $isSelf = $entity === $event;

        if (!$isSelf && $entity->getChannel()) {
            return;
        }

        if (!$channel = $eventConfig->getChannel()) {
            return;
        }

        $entity->setChannel($channel);

        if (!$channelIdField = $eventConfig->getChannelIdField()) {
            return;
        }

        if (!$event->getProperties()) {
            return;
        }

        $entity->setChannelId(
            self::getChannelId($event->getProperties(), $channelIdField)
        );
    }

    /**
     * @param string $channelIdField
     */
    private static function getChannelId(array $properties, $channelIdField): ?int
    {
        if (empty($properties[$channelIdField])) {
            return null;
        }

        $channelId = $properties[$channelIdField];
        if (is_array($channelId) && (1 === count($channelId))) {
            // Only store channel ID if a single item was selected
            $channelId = reset($channelId);
        }

        if (!is_numeric($channelId)) {
            return null;
        }

        return (int) $channelId;
    }
}