DateHandlerMonth::fromSlot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\statistics;
6
7
use SimpleSAML\Assert\Assert;
8
9
/**
10
 * @package SimpleSAMLphp
11
 */
12
class DateHandlerMonth extends DateHandler
13
{
14
    /**
15
     * Constructor
16
     *
17
     * @param integer $offset Date offset
18
     */
19
    public function __construct(int $offset)
20
    {
21
        $this->offset = $offset;
22
    }
23
24
25
    /**
26
     * @param int $epoch
27
     * @param int $slotsize
28
     * @return int
29
     */
30
    public function toSlot(int $epoch, int $slotsize): int
31
    {
32
        $dsttime = $this->getDST($epoch) + $epoch;
33
        $parsed = getdate($dsttime);
34
        $slot = (($parsed['year'] - 2000) * 12) + $parsed['mon'] - 1;
35
        return $slot;
36
    }
37
38
39
    /**
40
     * @param int $slot
41
     * @param int $slotsize
42
     * @return int
43
     */
44
    public function fromSlot(int $slot, ?int $slotsize): int
45
    {
46
        Assert::null($slotsize); // Only the upstream DateHandler uses this
47
48
        $month = ($slot % 12);
49
        $year = 2000 + intval(floor($slot / 12));
50
        return mktime(0, 0, 0, $month + 1, 1, $year);
51
    }
52
53
54
    /**
55
     * @param int $from
56
     * @param int $to
57
     * @param int $slotsize
58
     * @param string $dateformat
59
     * @return string
60
     */
61
    public function prettyHeader(int $from, int $to, int $slotsize, string $dateformat): string
62
    {
63
        $month = ($from % 12) + 1;
64
        $year = 2000 + intval(floor($from / 12));
65
        return $year . '-' . $month;
66
    }
67
}
68