Completed
Pull Request — master (#2)
by Jan-Petter
02:43
created

DirectiveClientCommons::isBetween()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 18
rs 8.8571
cc 5
eloc 14
nc 10
nop 4
1
<?php
2
namespace vipnytt\RobotsTxtParser\Client\Directives;
3
4
use DateTime;
5
use DateTimeZone;
6
7
/**
8
 * Class DirectiveClientCommons
9
 *
10
 * @package vipnytt\RobotsTxtParser\Client\Directives
11
 */
12
trait DirectiveClientCommons
13
{
14
    /**
15
     * Is time between
16
     *
17
     * @param int $timestamp
18
     * @param string $fromTime
19
     * @param string $toTime
20
     * @param string $format
21
     * @return bool
22
     */
23
    private function isBetween($timestamp, $fromTime, $toTime, $format = 'Hi')
24
    {
25
        $dateTime = new DateTime();
26
        $timezone = new DateTimeZone('UTC');
27
        $dtRef = $dateTime->createFromFormat('U', $timestamp, $timezone);
28
        $dtFrom = $dateTime->createFromFormat($format, $fromTime, $timezone);
29
        $dtTo = $dateTime->createFromFormat($format, $toTime, $timezone);
30
        if ($dtFrom > $dtTo) {
31
            $dtTo->modify('+1 day');
32
        }
33
        return (
34
            $dtFrom <= $dtRef &&
35
            $dtRef <= $dtTo
36
        ) || (
37
            $dtFrom <= $dtRef->modify('+1 day') &&
38
            $dtRef <= $dtTo
39
        );
40
    }
41
}
42