Completed
Pull Request — develop (#27)
by Chris
13:09
created

PlaylistBuilder::addUri()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 5
nop 1
1
<?php
2
3
namespace Chrisyue\PhpM3u8\Parser;
4
5
use Chrisyue\PhpM3u8\Model\Mapping\TagMetadata;
6
use Chrisyue\PhpM3u8\Model\MasterPlaylist;
7
use Chrisyue\PhpM3u8\Model\MediaPlaylist;
8
9
class PlaylistBuilder
10
{
11
    private $factory;
12
13
    public function __construct(PlaylistComponentFactory $factory)
14
    {
15
        $this->factory = $factory;
16
    }
17
18
    public function initPlaylist()
19
    {
20
        $this->playlist = $this->factory->createPlaylistBuffer();
0 ignored issues
show
Bug introduced by
The property playlist does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
21
    }
22
23
    public function addPlaylistTag(TagMetadata $metadata, $value)
24
    {
25
        $this->ensurePlaylistType($metadata->getProperty()->getDeclaringClass()->getName());
26
        $this->callComponentSetter($this->playlist, $metadata, $value);
27
    }
28
29
    public function addMediaSegmentTag(TagMetadata $metadata, $value)
30
    {
31
        $this->ensurePlaylistType(MediaPlaylist::class);
32
33
        $segment = end($this->playlist->segments);
34
        if (!$segment || null !== $segment->uri) {
35
            $segment = $this->factory->createSegment();
36
            $this->playlist->segments->append($segment);
0 ignored issues
show
Bug introduced by
The method append cannot be called on $this->playlist->segments (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
37
        }
38
39
        $this->callComponentSetter($segment, $metadata, $value);
40
    }
41
42
    public function addUri($uri)
43
    {
44
        if ($this->playlist instanceof PlaylistCopyableInterface) {
45
            throw new \RuntimeException('Adding URI to unknown playlist type');
46
        }
47
48
        $property = $this->playlist instanceof MasterPlaylist ? 'streamInfs' : 'segments';
49
        $component = end($this->playlist->$property);
50
        if (!$component || null !== $component->uri) {
51
            throw new \RuntimeException();
52
        }
53
54
        $component->uri = $uri;
55
    }
56
57
    public function getResult()
58
    {
59
        return $this->playlist;
60
    }
61
62
    private function callComponentSetter($component, TagMetadata $metadata, $value)
63
    {
64
        $property = $metadata->getProperty();
65
        $property->setAccessible(true);
66
        if ($metadata->isMultiple()) {
67
            return $property->getValue($component)->append($value);
68
        }
69
70
        $property->setValue($component, $value);
71
    }
72
73
    private function ensurePlaylistType($type)
74
    {
75
        if ($this->playlist instanceof $type) {
76
            return;
77
        }
78
79
        if (!$this->playlist instanceof PlaylistCopyableInterface) {
80
            throw new \RuntimeException('Different kinds of playlist tag in a same m3u8 content');
81
        }
82
83
        $playlist = $type === MediaPlaylist::class ? $this->factory->createMediaPlaylist() : $this->factory->createMasterPlaylist();
84
        $this->playlist->copyToPlaylist($playlist);
85
        $this->playlist = $playlist;
86
    }
87
}
88