Completed
Push — master ( a37b73...8ce218 )
by Chris
02:15
created

Parser::lex()   C

Complexity

Conditions 13
Paths 13

Size

Total Lines 73
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 44
CRAP Score 13

Importance

Changes 0
Metric Value
cc 13
eloc 41
nc 13
nop 1
dl 0
loc 73
rs 5.4475
c 0
b 0
f 0
ccs 44
cts 44
cp 1
crap 13

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the PhpM3u8 package.
5
 *
6
 * (c) Chrisyue <http://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;
13
14
use Chrisyue\PhpM3u8\Loader\LoaderInterface;
15
use Chrisyue\PhpM3u8\M3u8\M3u8;
16
use Chrisyue\PhpM3u8\M3u8\MediaSegment;
17
use Chrisyue\PhpM3u8\M3u8\Playlist;
18
19
class Parser
20
{
21
    private $loader;
22
23 1
    public function setLoader(LoaderInterface $loader)
24
    {
25 1
        $this->loader = $loader;
26
27 1
        return $this;
28
    }
29
30 2
    public function parseFromUri($uri)
31
    {
32 2
        if (null === $this->loader) {
33 1
            throw new \BadMethodCallException('You should set an m3u8 loader first');
34
        }
35
36 1
        return $this->parse($this->loader->load($uri));
37
    }
38
39 2
    public function parse($content)
40
    {
41 2
        $tokens = $this->lex($content);
42
43 2
        if (!isset($tokens['version'])) {
44
            $tokens['version'] = 3;
45
        }
46
47 2
        if (!isset($tokens['mediaSequence'])) {
48
            $tokens['mediaSequence'] = 0;
49
        }
50
51 2
        if (!isset($tokens['discontinuitySequence'])) {
52
            $tokens['discontinuitySequence'] = null;
53
        }
54
55 2
        if (!isset($tokens['isEndless'])) {
56
            $tokens['isEndless'] = true;
57
        }
58
59 2
        $mediaSegments = array();
60 2
        foreach ($tokens['playlist'] as $index => $row) {
61 2
            $mediaSegment = new MediaSegment(
62 2
                $row['uri'],
63 2
                $row['duration'],
64 2
                $tokens['mediaSequence'] + $index,
65 2
                !empty($row['isDiscontinuity']),
66 2
                empty($row['title']) ? null : $row['title'],
67 2
                empty($row['byteRange']) ? null : $row['byteRange']
68 2
            );
69 2
            $mediaSegments[] = $mediaSegment;
70 2
        }
71 2
        $playlist = new Playlist($mediaSegments, $tokens['isEndless']);
72
73 2
        return new M3u8($playlist, $tokens['version'], $tokens['targetDuration'], $tokens['discontinuitySequence']);
74
    }
75
76 2
    private function lex($content)
77
    {
78 2
        $tokens = array();
79
80 2
        $mediaSequence = 0;
81
82 2
        $lines = explode("\n", $content);
83 2
        foreach ($lines as $line) {
84 2
            $line = trim($line);
85
86 2
            if (preg_match('/^#EXT-X-VERSION:(\d+)/', $line, $matches)) {
87 2
                $tokens['version'] = $matches[1];
88 2
                continue;
89
            }
90
91 2
            if (preg_match('/^#EXT-X-TARGETDURATION:(\d+)/', $line, $matches)) {
92 2
                $tokens['targetDuration'] = (int) $matches[1];
93 2
                continue;
94
            }
95
96 2
            if (preg_match('/^#EXT-X-MEDIA-SEQUENCE:(\d+)/', $line, $matches)) {
97 2
                $tokens['mediaSequence'] = (int) $matches[1];
98 2
                continue;
99
            }
100
101 2
            if (preg_match('/^#EXT-X-DISCONTINUITY-SEQUENCE:(\d+)/', $line, $matches)) {
102 2
                $tokens['discontinuitySequence'] = (int) $matches[1];
103 2
                continue;
104
            }
105
106 2
            if (preg_match('/^#EXT-X-DISCONTINUITY/', $line)) {
107 2
                $tokens['playlist'][$mediaSequence]['isDiscontinuity'] = true;
108 2
                continue;
109
            }
110
111 2
            if (preg_match('/^#EXTINF:(.+),(.*)$/', $line, $matches)) {
112 2
                $tokens['playlist'][$mediaSequence]['duration'] = $matches[1];
113
114 2
                if (isset($matches[2])) {
115 2
                    $tokens['playlist'][$mediaSequence]['title'] = $matches[2];
116 2
                }
117
118 2
                continue;
119
            }
120
121 2
            if (preg_match('/^#EXT-X-BYTERANGE:(\d+)(@(\d+))?$/', $line, $matches)) {
122 2
                $byteRange = new \SplFixedArray(2);
123 2
                $byteRange[0] = (int) $matches[1];
124
125 2
                if (!empty($matches[3])) {
126 2
                    $byteRange[1] = (int) $matches[3];
127 2
                }
128
129 2
                $tokens['playlist'][$mediaSequence]['byteRange'] = $byteRange;
130
131 2
                continue;
132
            }
133
134 2
            if (preg_match('/^[^#]+/', $line, $matches)) {
135 2
                $tokens['playlist'][$mediaSequence]['uri'] = $matches[0];
136 2
                ++$mediaSequence;
137
138 2
                continue;
139
            }
140
141 2
            if ('#EXT-X-ENDLIST' === $line) {
142 2
                $tokens['isEndless'] = false;
143 2
                break;
144
            }
145 2
        }
146
147 2
        return $tokens;
148
    }
149
}
150