Completed
Push — feature/bug-fix ( c7667a )
by Chris
02:14
created

M3u8Generator::generate()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 25
ccs 17
cts 17
cp 1
rs 8.5806
cc 4
eloc 15
nc 6
nop 2
crap 4
1
<?php
2
3
/*
4
 * This file is part of the Mala 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\Mala;
13
14
use Chrisyue\Mala\Manager\MediaSegmentManagerInterface;
15
use Chrisyue\Mala\Model\ChannelInterface;
16
use Chrisyue\PhpM3u8\M3u8\M3u8;
17
use Chrisyue\PhpM3u8\M3u8\Playlist;
18
19
class M3u8Generator
20
{
21
    private $manager;
22
    private $options;
23
24 2
    public function __construct(MediaSegmentManagerInterface $manager, array $options = array())
25
    {
26 2
        $this->manager = $manager;
27 2
        $this->options = $options + array(
28 2
            'version' => 3,
29 2
            'target_duration' => 10,
30 2
        );
31 2
    }
32
33 2
    public function generate(ChannelInterface $channel, \DateTime $startsAt = null)
34
    {
35 2
        $targetDuration = $this->options['target_duration'];
36
37 2
        if (null === $startsAt) {
38 1
            $startsAt = new \DateTime();
39 1
        }
40 2
        $segments = $this->manager->findPlaying($channel, $startsAt, $targetDuration * 3);
41 2
        $playlist = new Playlist($segments);
42
43 2
        $first = $playlist->getFirst();
44 2
        if (null === $first) {
45 1
            return;
46
        }
47
48 1
        $age = $first->getEndsAt()->getTimestamp() - $startsAt->getTimestamp() + 1;
49 1
        $playlist->setAge($age);
50
51 1
        $discontinuitySequence = $first->getProgram()->getSequence();
52 1
        if ($first->isDiscontinuity()) {
53 1
            --$discontinuitySequence;
54 1
        }
55
56 1
        return new M3u8($playlist, $this->options['version'], $targetDuration, $discontinuitySequence);
57
    }
58
}
59