Completed
Push — master ( 0ebeda...45d090 )
by Peter
02:31
created

ScheduleBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 6
c 4
b 0
f 1
lcom 1
cbo 2
dl 0
loc 65
ccs 20
cts 20
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A buildRule() 0 13 2
A buildSchedule() 0 13 3
1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 */
8
namespace AnimeDb\SmartSleep;
9
10
use AnimeDb\SmartSleep\Rule\RuleInterface;
11
12
class ScheduleBuilder
13
{
14
    /**
15
     * @var RuleCollection
16
     */
17
    protected $collection;
18
19
    /**
20
     * @param RuleCollection $collection
21
     */
22 3
    public function __construct(RuleCollection $collection)
23
    {
24 3
        $this->collection = $collection;
25 3
    }
26
27
    /**
28
     * @param string $name
29
     * @param int $start
30
     * @param int $end
31
     * @param int $seconds
32
     *
33
     * @return RuleInterface|null
34
     */
35 3
    public function buildRule($name, $start, $end, $seconds)
36
    {
37 3
        if (!$this->collection->has($name)) {
38 2
            return null;
39
        }
40
41 2
        return $this
42
            ->collection
43 2
            ->get($name)
44 2
            ->setEnd($end)
45 2
            ->setStart($start)
46 2
            ->setSeconds($seconds);
47
    }
48
49
    /**
50
     * Build schedule.
51
     *
52
     * <code>
53
     * [
54
     *   {rule: <rule_name>, start: <start_hour>, end: <end_hour>, seconds: <sleep_seconds>},
55
     *   ...
56
     * ]
57
     * </code>
58
     *
59
     * @param array $schedule
60
     *
61
     * @return Schedule
62
     */
63 2
    public function buildSchedule(array $schedule)
64
    {
65 1
        $result = new Schedule();
66 1
        foreach ($schedule as $options) {
67 1
            $rule = $this->buildRule($options['rule'], $options['start'], $options['end'], $options['seconds']);
68
69 2
            if ($rule instanceof RuleInterface) {
70 1
                $result->add($rule);
71 1
            }
72 1
        }
73
74 1
        return $result;
75
    }
76
}
77