Completed
Pull Request — develop (#27)
by Chris
05:29
created

Dumper::iterateProperties()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.2248
c 0
b 0
f 0
cc 5
nc 4
nop 3
1
<?php
2
3
namespace Chrisyue\PhpM3u8\Dumper;
4
5
use Chrisyue\PhpM3u8\Definition\TagDefinitions;
6
use Chrisyue\PhpM3u8\Dumper\DataIterator\DataIteratorInterface;
7
use Chrisyue\PhpM3u8\Config;
8
use Chrisyue\PhpM3u8\Line\Lines;
9
use Chrisyue\PhpM3u8\Line\Line;
10
use Chrisyue\PhpM3u8\Definition\TagDefinition;
11
12
class Dumper
13
{
14
    private $tagDefinitions;
15
16
    private $valueDumpers;
17
18
    private $currentUriAware;
19
20
    private $isMasterPlaylist;
21
22
    public function __construct(TagDefinitions $tagDefinitions, Config $valueDumpers)
23
    {
24
        $this->tagDefinitions = $tagDefinitions;
25
        $this->valueDumpers = $valueDumpers;
26
    }
27
28
    public function dumpToLines(\ArrayObject $data, Lines $lines)
29
    {
30
        $lines->add(new Line('EXTM3U', true));
31
        $this->iterateProperties(
32
            $this->tagDefinitions->getHeadProperties(),
33
            $data,
34
            $lines,
35
            function (TagDefinition $tagDefinition) {
0 ignored issues
show
Unused Code introduced by
The call to Dumper::iterateProperties() has too many arguments starting with function (\Chrisyue\PhpM...laylist = true; } }.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
36
                if ('master-playlist' === $tagDefinition->getCategory()) {
37
                    $this->isMasterPlaylist = true;
38
                }
39
            }
40
        );
41
42
        if (!isset($data['mediaSegments'])) {
43
            return;
44
        }
45
46
        foreach ($data['mediaSegments'] as $mediaSegment) {
47
            $this->iterateProperties($this->tagDefinitions->getMediaSegmentProperties(), $mediaSegment, $lines);
48
49
            $lines->add(new Line(null, $mediaSegment['uri']));
50
        }
51
52
        $this->iterateProperties($this->tagDefinitions->getFootProperties(), $data, $lines);
53
    }
54
55
    private function iterateProperties(
56
        array $properties,
57
        \ArrayObject $data,
58
        Lines $lines
59
    ) {
60
        foreach ($properties as $property) {
61
            if (!isset($data[$property])) {
62
                continue;
63
            }
64
65
            $definition = $this->tagDefinitions->get($property);
66
            $value = $data[$property];
67
68
            if (!$definition->isMultiple()) {
69
                $this->dumpAndAddToLines($definition, $value, $lines);
70
71
                continue;
72
            }
73
74
            foreach ($value as $element) {
75
                $this->dumpAndAddToLines($definition, $element, $lines);
76
            }
77
        }
78
    }
79
80
    private function dumpValue(TagDefinition $definition, $value)
81
    {
82
        $valueType = $definition->getValueType();
83
        $dump = $this->valueDumpers->get($valueType);
84
85
        if (!is_callable($dump)) {
86
            return $value;
87
        }
88
89
        if ('attribute-list' === $valueType) {
90
            return $dump($value, $definition->getAttributeTypes());
91
        }
92
93
94
        return $dump($value);
95
    }
96
97
    private function dumpAndAddToLines(TagDefinition $definition, $value, Lines $lines)
98
    {
99
        $lines->add(new Line($definition->getTagName(), $this->dumpValue($definition, $value)));
100
101
        if ($definition->isUriAware()) {
102
            $lines->add(new Line(null, $value['uri']));
103
        }
104
    }
105
}
106