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

ChannelTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 31
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B setChannelRelations() 0 21 5
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