StopTime::getHour()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Itmedia\ZippyBusBundle\Schedule;
6
7
class StopTime
8
{
9
10
    /**
11
     * Расписание представлено списком минут, например, 376, 399, 1511,
12
     * что соответствует времени 6:16, 6:39 и 25:11.
13
     * Из-за того, что транспорт курсирует в том числе и после полуночи,
14
     * количество часов в сутках в нашей системе может быть больше 24.
15
     * Это сделано для того, чтобы мы могли различать рейсы,
16
     * которые работают рано утром и после полуночи.
17
     * Таким образом, время 25:11 - это 1:11 утра (25-24=1).
18
     *
19
     * @var int
20
     */
21
    private $time;
22
23
    /**
24
     * Неполный маршрут
25
     * @var bool
26
     */
27
    private $short;
28
29
    /**
30
     * Заметка
31
     * @var string
32
     */
33
    private $note;
34
35
36
    public function __construct(int $time, bool $short = false, string $note = '')
37
    {
38
        $this->time = $time;
39
        $this->short = $short;
40
        $this->note = $note;
41
    }
42
43
44
    public static function createNow(): StopTime
45
    {
46
        $date = new \DateTime();
47
        $hour = (int)$date->format('H');
48
        $hour = $hour > 3 ? $hour : $hour + 24;
49
        $minute = (int)$date->format('i');
50
        return new self($hour * 60 + $minute);
51
    }
52
53
54
    public function sub(StopTime $time): StopTime
55
    {
56
        $newTime = $this->time - $time->time;
57
        return new self($newTime);
58
    }
59
60
61
    public function getHour(): int
62
    {
63
        $hour = (int)floor($this->time / 60);
64
        return ($hour >= 24) ? $hour - 24 : $hour;
65
    }
66
67
68
    public function getMinute(): int
69
    {
70
        return $this->time % 60;
71
    }
72
73
    public function isShort(): bool
74
    {
75
        return $this->short;
76
    }
77
78
79
    public function getTimeFormat(): string
80
    {
81
        return sprintf('%s:%s',
82
            $this->getHour(),
83
            $this->getMinute() < 10 ? '0' . $this->getMinute() : $this->getMinute()
84
        );
85
    }
86
87
    /**
88
     * Время в минутах
89
     * @return int
90
     */
91
    public function getTime(): int
92
    {
93
        return $this->time;
94
    }
95
96
    public function getNote(): string
97
    {
98
        return $this->note;
99
    }
100
}
101