Completed
Pull Request — master (#29)
by
unknown
26:31 queued 23:57
created

ChannelTrait::setChannelRelations()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
cc 5
eloc 11
nc 6
nop 1
1
<?php
2
3
namespace MovingImage\Client\VMPro\Util;
4
5
use MovingImage\Client\VMPro\Entity\Channel;
6
7
/**
8
 * Helper methods for manipulating Channels.
9
 */
10
trait ChannelTrait
11
{
12
    /**
13
     * Configures the parent/child relationships between Channels.
14
     *
15
     * @param Channel[] $channels - any iterable collection of Channel
16
     *
17
     * @return array
18
     */
19
    private function setChannelRelations($channels)
20
    {
21
        $indexedChannels = [];
22
23
        /** @var Channel $channel */
24
        foreach ($channels as $channel) {
25
            $indexedChannels[$channel->getId()] = $channel;
26
        }
27
28
        foreach ($indexedChannels as $channel) {
29
            $parentId = $channel->getParentId();
30
            if ($parentId && array_key_exists($parentId, $indexedChannels)) {
31
                /** @var Channel $parent */
32
                $parent = $indexedChannels[$parentId];
33
                $channel->setParent($parent);
34
                $parent->addChild($channel);
35
            }
36
        }
37
38
        return array_values($indexedChannels);
39
    }
40
}
41