Completed
Branch 2.0-dev (d250b8)
by Jan-Petter
03:02
created

DirectiveClientCommons::isBetween()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 8.8571
cc 5
eloc 13
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) $dtTo->modify('+1 day');
31
        return (
32
            $dtFrom <= $dtRef &&
33
            $dtRef <= $dtTo
34
        ) || (
35
            $dtFrom <= $dtRef->modify('+1 day') &&
36
            $dtRef <= $dtTo
37
        );
38
    }
39
}