Completed
Push — master ( 0591fe...b215f8 )
by Jan-Petter
03:21
created

src/Parser/Directives/DelayParser.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Parser\Directives;
10
11
use vipnytt\RobotsTxtParser\Client\Directives\DelayClient;
12
use vipnytt\RobotsTxtParser\Handler\RenderHandler;
13
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
14
15
/**
16
 * Class DelayParser
17
 *
18
 * @package vipnytt\RobotsTxtParser\Parser\Directives
19
 */
20
class DelayParser implements ParserInterface, RobotsTxtInterface
21
{
22
    /**
23
     * Directive
24
     * @var string
25
     */
26
    private $directive;
27
28
    /**
29
     * Base uri
30
     * @var string
31
     */
32
    private $base;
33
34
    /**
35
     * Delay
36
     * @var float|int
37
     */
38
    private $delay;
39
40
    /**
41
     * Client cache
42
     * @var DelayClient
43
     */
44
    private $client;
45
46
    /**
47
     * DelayParser constructor.
48
     *
49
     * @param string $base
50
     * @param string $directive
51
     */
52
    public function __construct($base, $directive)
53
    {
54
        $this->base = $base;
55
        $this->directive = $directive;
56
    }
57
58
    /**
59
     * Add
60
     *
61
     * @param float|int|string $line
62
     * @return bool
63
     */
64
    public function add($line)
65
    {
66
        if (!is_numeric($line) ||
67
            (
68
                isset($this->delay) &&
69
                $this->delay > 0
70
            )
71
        ) {
72
            return false;
73
        }
74
        // PHP hack to convert numeric string to float or int
75
        // http://stackoverflow.com/questions/16606364/php-cast-string-to-either-int-or-float
76
        $this->delay = $line + 0;
77
        return true;
78
    }
79
80
    /**
81
     * Client
82
     *
83
     * @param string $userAgent
84
     * @param float|int $fallbackValue
85
     * @return DelayClient
86
     */
87 View Code Duplication
    public function client($userAgent = self::USER_AGENT, $fallbackValue = 0)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
    {
89
        if (isset($this->client)) {
90
            return $this->client;
91
        }
92
        return $this->client = new DelayClient($this->base, $userAgent, $this->delay, $fallbackValue);
93
    }
94
95
    /**
96
     * Render
97
     *
98
     * @param RenderHandler $handler
99
     * @return bool
100
     */
101
    public function render(RenderHandler $handler)
102
    {
103
        if (!empty($this->delay)) {
104
            $handler->add($this->directive, $this->delay);
105
        }
106
        return true;
107
    }
108
}
109