EpgScheduler   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 87.5%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 63
ccs 35
cts 40
cp 0.875
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C schedule() 0 51 9
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\EpgManagerInterface;
15
use Chrisyue\Mala\Manager\VideoManagerInterface;
16
use Chrisyue\Mala\Model\ChannelInterface;
17
18
class EpgScheduler
19
{
20
    private $epgManager;
21
    private $videoManager;
22
23 4
    public function __construct(EpgManagerInterface $epgManager, VideoManagerInterface $videoManager)
24
    {
25 4
        $this->epgManager = $epgManager;
26 4
        $this->videoManager = $videoManager;
27 4
    }
28
29 4
    public function schedule(ChannelInterface $channel, \DateTime $startsAt, \DateTime $endsAt, $isForce = false)
30
    {
31 4
        if ($isForce) {
32 1
            $this->epgManager->clear($channel, $startsAt);
33 1
        }
34
35 4
        $lastProgram = $this->epgManager->findLastProgram($channel);
36
37 4
        if (null !== $lastProgram) {
38 2
            $shouldStartsAt = clone $lastProgram->getEndsAt();
39 2
            $shouldStartsAt->modify('+1 second');
40
41 2
            if ($shouldStartsAt > $endsAt) {
42
                // the ends time of the last program is after $endsAt
43
                throw new \Exception(sprintf(
44
                    'Cannot schedule in %s to %s because there is newer epg, you can try to do a force schedule',
45
                    $startsAt->format('c'),
46
                    $endsAt->format('c')
47
                ));
48
            }
49
50 2
            if ($startsAt < $shouldStartsAt) {
51 2
                $startsAt = $shouldStartsAt;
52 2
            }
53 2
        }
54
55 4
        $videos = $this->videoManager->findByChannel($channel);
56 4
        if (empty($videos)) {
57 2
            return;
58
        }
59
60 2
        $videoStartsAt = clone $startsAt;
61 2
        $sequence = null === $lastProgram ? 0 : $lastProgram->getSequence();
62
63 2
        $videos = new \InfiniteIterator(new \ArrayIterator($videos));
64 2
        foreach ($videos as $video) {
65 2
            $videoEndsAt = clone $videoStartsAt;
66 2
            $videoEndsAt->modify(sprintf('+%d seconds', $video->getDuration() - 1));
67
68 2
            $program = $this->epgManager->createProgram($channel, $video, ++$sequence, $videoStartsAt, $videoEndsAt);
69 2
            $this->epgManager->saveDeferred($program);
70
71 2
            $videoStartsAt = clone $videoEndsAt;
72 2
            $videoStartsAt->modify('+1 second');
73 2
            if ($videoStartsAt > $endsAt) {
74 2
                break;
75
            }
76 2
        }
77
78 2
        $this->epgManager->commit();
79 2
    }
80
}
81