Completed
Push — master ( b7fe7e...4bccf1 )
by Peter
01:34
created

OnceMonthRule   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 3
c 3
b 0
f 0
lcom 1
cbo 0
dl 0
loc 43
ccs 14
cts 14
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isMatched() 0 6 1
A seconds() 0 15 1
1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 */
8
9
namespace AnimeDb\SmartSleep\Rule;
10
11
class OnceMonthRule implements Rule
12
{
13
    /**
14
     * @var \DateTime
15
     */
16
    private $time;
17
18 3
    public function __construct()
19
    {
20 3
        $this->time = new \DateTime(); // default time
21 3
    }
22
23
    /**
24
     * @param \DateTime $time
25
     *
26
     * @return bool
27
     */
28 2
    public function isMatched(\DateTime $time)
29
    {
30 2
        $this->time = clone $time; // save current time
31
32 2
        return true;
33
    }
34
35
    /**
36
     * @return int
37
     */
38 3
    public function seconds()
39
    {
40
        // interval duration [next month, next month +1)
41 3
        $offset_time = clone $this->time;
42 3
        $offset_time->modify('first day of this month')->modify('+1 month')->setTime(0, 0, 0);
43
44 3
        $limit_time = clone $offset_time;
45 3
        $limit_time->modify('+1 month');
46 3
        $limit = $limit_time->getTimestamp() - $offset_time->getTimestamp();
47
48
        // offset to next month
49 3
        $offset = $offset_time->getTimestamp() - $this->time->getTimestamp();
50
51 3
        return $offset + rand(0, $limit);
52
    }
53
}
54