Completed
Push — master ( 705095...78cb27 )
by Jan-Petter
04:57
created

CrawlDelay   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 62
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A add() 0 12 4
A export() 0 4 2
1
<?php
2
namespace vipnytt\RobotsTxtParser\Parser\Directives;
3
4
use vipnytt\RobotsTxtParser\Exceptions\ParserException;
5
use vipnytt\RobotsTxtParser\Parser\Toolbox;
6
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
7
8
/**
9
 * Class CrawlDelay
10
 *
11
 * @package vipnytt\RobotsTxtParser\Parser\Directives
12
 */
13
class CrawlDelay implements DirectiveInterface, RobotsTxtInterface
14
{
15
    use Toolbox;
16
17
    /**
18
     * Directive alternatives
19
     */
20
    const DIRECTIVE = [
21
        self::DIRECTIVE_CACHE_DELAY,
22
        self::DIRECTIVE_CRAWL_DELAY,
23
    ];
24
25
    /**
26
     * Directive
27
     */
28
    protected $directive = self::DIRECTIVE_CRAWL_DELAY;
29
30
    /**
31
     * Delay
32
     * @var float|int
33
     */
34
    protected $value;
35
36
    /**
37
     * CrawlDelay constructor.
38
     * @param string $directive
39
     * @throws ParserException
40
     */
41
    public function __construct($directive = self::DIRECTIVE_CRAWL_DELAY)
42
    {
43
        $this->directive = $this->validateDirective($directive, self::DIRECTIVE);
44
    }
45
46
    /**
47
     * Add
48
     *
49
     * @param float|int|string $line
50
     * @return bool
51
     */
52
    public function add($line)
53
    {
54
        if (
55
            empty($float = floatval($line)) ||
56
            isset($this->value) &&
57
            $this->value > 0
58
        ) {
59
            return false;
60
        }
61
        $this->value = $float;
62
        return true;
63
    }
64
65
    /**
66
     * Export
67
     *
68
     * @return array
69
     */
70
    public function export()
71
    {
72
        return empty($this->value) ? [] : [$this->directive => $this->value];
73
    }
74
}
75