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

OnceMonthRule::getSeconds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
crap 1
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\Rule;
9
10
class OnceMonthRule extends RuleBase
11
{
12
    /**
13
     * @var \DateTime
14
     */
15
    protected $time;
16
17 6
    public function __construct()
18
    {
19 6
        $this->time = new \DateTime(); // default time
20 6
    }
21
22
    /**
23
     * @param \DateTime $time
24
     *
25
     * @return bool
26
     */
27 2
    public function isMatched(\DateTime $time)
28
    {
29 2
        $this->time = clone $time; // save current time
30
31 2
        return true;
32
    }
33
34
    /**
35
     * @return int
36
     */
37 3
    public function getSeconds()
38
    {
39
        // interval duration [next month, next month +1)
40 3
        $offset_time = clone $this->time;
41 3
        $offset_time->modify('+1 month 00:00:00')->modify('first day of this month');
42
43 3
        $limit_time = clone $this->time;
44 3
        $limit_time->modify('+2 month 00:00:00')->modify('first day of this month');
45 3
        $limit = $limit_time->getTimestamp() - $offset_time->getTimestamp();
46
47
        // offset to next month
48 3
        $offset_time = clone $this->time;
49 3
        $offset_time->modify('+1 month 00:00:00')->modify('first day of this month');
50 3
        $offset = $offset_time->getTimestamp() - $this->time->getTimestamp();
51
52 3
        return $offset + rand(0, $limit);
53
    }
54
}
55