Completed
Pull Request — develop (#27)
by Chris
02:45
created

Dumper   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A dumpToLines() 0 26 4
A iterateProperties() 0 24 5
A dumpValue() 0 15 3
A dumpAndAddToLines() 0 8 2
1
<?php
2
3
/*
4
 * This file is part of the PhpM3u8 package.
5
 *
6
 * (c) Chrisyue <https://chrisyue.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Chrisyue\PhpM3u8\Dumper;
13
14
use Chrisyue\PhpM3u8\Definition\TagDefinitions;
15
use Chrisyue\PhpM3u8\Config;
16
use Chrisyue\PhpM3u8\Line\Lines;
17
use Chrisyue\PhpM3u8\Line\Line;
18
use Chrisyue\PhpM3u8\Definition\TagDefinition;
19
20
class Dumper
21
{
22
    private $tagDefinitions;
23
24
    private $valueDumpers;
25
26
    private $currentUriAware;
27
28
    private $isMasterPlaylist;
29
30
    public function __construct(TagDefinitions $tagDefinitions, Config $valueDumpers)
31
    {
32
        $this->tagDefinitions = $tagDefinitions;
33
        $this->valueDumpers = $valueDumpers;
34
    }
35
36
    public function dumpToLines(\ArrayObject $data, Lines $lines)
37
    {
38
        $lines->add(new Line('EXTM3U', true));
39
        $this->iterateProperties(
40
            $this->tagDefinitions->getHeadProperties(),
41
            $data,
42
            $lines,
43
            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...
44
                if ('master-playlist' === $tagDefinition->getCategory()) {
45
                    $this->isMasterPlaylist = true;
46
                }
47
            }
48
        );
49
50
        if (!isset($data['mediaSegments'])) {
51
            return;
52
        }
53
54
        foreach ($data['mediaSegments'] as $mediaSegment) {
55
            $this->iterateProperties($this->tagDefinitions->getMediaSegmentProperties(), $mediaSegment, $lines);
56
57
            $lines->add(new Line(null, $mediaSegment['uri']));
58
        }
59
60
        $this->iterateProperties($this->tagDefinitions->getFootProperties(), $data, $lines);
61
    }
62
63
    private function iterateProperties(
64
        array $properties,
65
        \ArrayObject $data,
66
        Lines $lines
67
    ) {
68
        foreach ($properties as $property) {
69
            if (!isset($data[$property])) {
70
                continue;
71
            }
72
73
            $definition = $this->tagDefinitions->get($property);
74
            $value = $data[$property];
75
76
            if (!$definition->isMultiple()) {
77
                $this->dumpAndAddToLines($definition, $value, $lines);
78
79
                continue;
80
            }
81
82
            foreach ($value as $element) {
83
                $this->dumpAndAddToLines($definition, $element, $lines);
84
            }
85
        }
86
    }
87
88
    private function dumpValue(TagDefinition $definition, $value)
89
    {
90
        $valueType = $definition->getValueType();
91
        $dump = $this->valueDumpers->get($valueType);
92
93
        if (!is_callable($dump)) {
94
            return $value;
95
        }
96
97
        if ('attribute-list' === $valueType) {
98
            return $dump($value, $definition->getAttributeTypes());
99
        }
100
101
        return $dump($value);
102
    }
103
104
    private function dumpAndAddToLines(TagDefinition $definition, $value, Lines $lines)
105
    {
106
        $lines->add(new Line($definition->getTagName(), $this->dumpValue($definition, $value)));
107
108
        if ($definition->isUriAware()) {
109
            $lines->add(new Line(null, $value['uri']));
110
        }
111
    }
112
}
113