EpgScheduler::schedule()   C
last analyzed

Complexity

Conditions 9
Paths 44

Size

Total Lines 51
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 9.217

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 51
ccs 31
cts 36
cp 0.8611
rs 6.2727
cc 9
eloc 30
nc 44
nop 4
crap 9.217

How to fix   Long Method   

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 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