Completed
Branch 2.0-dev (4f313a)
by Jan-Petter
02:57
created

CommentClient::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
namespace vipnytt\RobotsTxtParser\Client\Directives;
3
4
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
5
6
/**
7
 * Class CommentClient
8
 *
9
 * @package vipnytt\RobotsTxtParser\Client\Directives
10
 */
11
class CommentClient implements RobotsTxtInterface
12
{
13
    /**
14
     * Base Uri
15
     * @var string
16
     */
17
    private $base;
18
19
    /**
20
     * User-agent
21
     * @var string
22
     */
23
    private $userAgent;
24
25
    /**
26
     * Comments
27
     * @var string[]
28
     */
29
    private $comments = [];
30
31
    /**
32
     * Fetched status
33
     * @var bool
34
     */
35
    private $fetched = false;
36
37
    /**
38
     * CommentClient constructor.
39
     *
40
     * @param string $base
41
     * @param string $userAgent
42
     * @param array $comments
43
     */
44
    public function __construct($base, $userAgent, array $comments)
45
    {
46
        $this->base = $base;
47
        $this->userAgent = $userAgent;
48
        $this->comments = $comments;
49
    }
50
51
    /**
52
     * CommentClient destructor.
53
     */
54
    public function __destruct()
55
    {
56
        if ($this->fetched !== true) {
57
            // Comment exists, but has not been fetched.
58
            foreach ($this->get() as $message) {
59
                trigger_error('`' . $this->userAgent . '` at `' . $this->base . self::PATH . '` -> ' . $message, E_USER_NOTICE);
60
            }
61
        }
62
    }
63
64
    /**
65
     * Get
66
     *
67
     * @return string[]
68
     */
69
    public function get()
70
    {
71
        $this->fetched = true;
72
        return $this->userAgent == self::USER_AGENT ? [] : $this->export();
73
    }
74
75
    /**
76
     * Export
77
     *
78
     * @return string[]
79
     */
80
    public function export()
81
    {
82
        $this->fetched = true;
83
        return $this->comments;
84
    }
85
}
86