DirectiveClientTrait   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 2
dl 0
loc 87
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getPathFromUri() 0 17 5
A isBetween() 0 18 5
A checkPaths() 0 24 3
1
<?php
2
/**
3
 * vipnytt/RobotsTxtParser
4
 *
5
 * @link https://github.com/VIPnytt/RobotsTxtParser
6
 * @license https://github.com/VIPnytt/RobotsTxtParser/blob/master/LICENSE The MIT License (MIT)
7
 */
8
9
namespace vipnytt\RobotsTxtParser\Client\Directives;
10
11
use DateTime;
12
use DateTimeZone;
13
use vipnytt\RobotsTxtParser\Handler\ErrorHandler;
14
use vipnytt\RobotsTxtParser\Parser\UriParser;
15
16
/**
17
 * Trait DirectiveClientTrait
18
 *
19
 * @package vipnytt\RobotsTxtParser\Client\Directives
20
 */
21
trait DirectiveClientTrait
22
{
23
    /**
24
     * Get path and query
25
     *
26
     * @param string $uri
27
     * @return string
28
     * @throws \InvalidArgumentException
29
     */
30
    private function getPathFromUri($uri)
31
    {
32
        $uriParser = new UriParser($uri);
33
        // Prepare uri
34
        $uriParser->encode();
35
        $uri = $uriParser->stripFragment();
36
        if (strpos($uri, '/') === 0) {
37
            // URI is already an path
38
            return $uri;
39
        }
40
        if (!$uriParser->validate()) {
41
            throw new \InvalidArgumentException('Invalid URI');
42
        }
43
        $path = (($path = parse_url($uri, PHP_URL_PATH)) === null) ? '/' : $path;
44
        $query = (($query = parse_url($uri, PHP_URL_QUERY)) === null) ? '' : '?' . $query;
45
        return $path . $query;
46
    }
47
48
    /**
49
     * Is time between
50
     *
51
     * @param int $timestamp
52
     * @param string $fromTime
53
     * @param string $toTime
54
     * @param string $format
55
     * @return bool
56
     */
57
    private function isBetween($timestamp, $fromTime, $toTime, $format = 'Hi')
58
    {
59
        $dateTime = new DateTime();
60
        $timezone = new DateTimeZone('UTC');
61
        $dtRef = $dateTime->createFromFormat('U', $timestamp, $timezone);
62
        $dtFrom = $dateTime->createFromFormat($format, $fromTime, $timezone);
63
        $dtTo = $dateTime->createFromFormat($format, $toTime, $timezone);
64
        if ($dtFrom > $dtTo) {
65
            $dtTo->modify('+1 day');
66
        }
67
        return (
68
                $dtFrom <= $dtRef &&
69
                $dtRef <= $dtTo
70
            ) || (
71
                $dtFrom <= $dtRef->modify('+1 day') &&
72
                $dtRef <= $dtTo
73
            );
74
    }
75
76
    /**
77
     * Check path rule
78
     *
79
     * @param string $path
80
     * @param string[] $paths
81
     * @return string|false
82
     */
83
    private function checkPaths($path, array $paths)
84
    {
85
        $reserved = [
86
            '?' => '\?',
87
            '.' => '\.',
88
            '*' => '.*',
89
            '+' => '\+',
90
            '(' => '\(',
91
            ')' => '\)',
92
            '[' => '\[',
93
            ']' => '\]',
94
        ];
95
        $errorHandler = new ErrorHandler();
96
        set_error_handler([$errorHandler, 'callback'], E_NOTICE | E_WARNING);
97
        foreach ($paths as $rule) {
98
            $escaped = str_replace(array_keys($reserved), array_values($reserved), $rule);
99
            if (preg_match('#^' . $escaped . '#', $path) === 1) {
100
                restore_error_handler();
101
                return $rule;
102
            }
103
        }
104
        restore_error_handler();
105
        return false;
106
    }
107
}
108