Completed
Pull Request — develop (#2)
by Chris
02:11
created

M3u8Generator::generate()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 20
ccs 13
cts 13
cp 1
rs 9.4285
cc 3
eloc 12
nc 4
nop 2
crap 3
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)
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);
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
        return new M3u8($playlist, $this->options['version'], $targetDuration, $first->getProgram()->getSequence());
52
    }
53
}
54