ChannelManager::getChannelEventCollection()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace MallardDuck\DynamicEcho;
4
5
use MallardDuck\DynamicEcho\Loader\LoadedEventDTO;
6
use MallardDuck\DynamicEcho\Collections\{
7
    ChannelEventCollection,
8
    ChannelAwareEventCollection
9
};
10
11
class ChannelManager
12
{
13
    /**
14
     * @var ChannelAwareEventCollection
15
     */
16
    private ChannelAwareEventCollection $channelCollection;
17
18 28
    public function __construct()
19
    {
20 28
        $this->channelCollection = new ChannelAwareEventCollection();
21 28
    }
22
23 28
    public function getChannelEventCollection(): ChannelAwareEventCollection
24
    {
25 28
        return $this->channelCollection;
26
    }
27
28
    /**
29
     * @param LoadedEventDTO $newItem
30
     *
31
     * @return $this
32
     */
33
    public function pushEventDto(LoadedEventDTO $newItem): self
34
    {
35
        $this->channelCollection = $this->channelCollection->push($newItem);
36
37
        return $this;
38
    }
39
40
    /**
41
     * Push multiple EventDTOs into the channel collection.
42
     *
43
     * @param LoadedEventDTO|LoadedEventDTO[] $newItems,...
44
     *
45
     * @return $this
46
     */
47
    public function pushEventDtos(...$newItems): self
48
    {
49
        $this->channelCollection = $this->channelCollection->push(...$newItems);
50
51
        return $this;
52
    }
53
54
    public function registeredChannelNames()
55
    {
56
        return $this->channelCollection->keys();
57
    }
58
59
    public function registeredChannelInfo()
60
    {
61
        /**
62
         * @var ChannelEventCollection $channelGroup
63
         */
64
        return $this->channelCollection->map(static function ($channelGroup, $key) {
65
            $first = true;
66
            $channelInfo = [
67
                'authName' => $key,
68
                'type' => null,
69
                'authCallback' => null,
70
            ];
71
            /**
72
             * @var LoadedEventDTO $val
73
             */
74
            $events = $channelGroup->map(static function ($val) use (&$first, &$channelInfo) {
75
                if ($first) {
76
                    $first = false;
77
                    $channelInfo['type'] = $val->getParameter('channelType');
78
                    $channelInfo['authCallback'] = $val->getParameter('channelAuthCallback');
79
                }
80
                return $val->fullEventName;
81
            })->toArray();
82
            $channelInfo['events'] = $events;
83
84
            return $channelInfo;
85
        });
86
    }
87
}
88