Completed
Pull Request — master (#2)
by Jan-Petter
02:43
created

CommentParser   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 6
c 4
b 0
f 0
lcom 1
cbo 1
dl 0
loc 77
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A add() 0 5 1
A client() 0 7 2
A render() 0 8 2
1
<?php
2
namespace vipnytt\RobotsTxtParser\Parser\Directives;
3
4
use vipnytt\RobotsTxtParser\Client\Directives\CommentClient;
5
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
6
7
/**
8
 * Class CommentParser
9
 *
10
 * @package vipnytt\RobotsTxtParser\Parser\Directives
11
 */
12
class CommentParser implements ParserInterface, RobotsTxtInterface
13
{
14
    /**
15
     * User-agent
16
     * @var string
17
     */
18
    private $userAgent;
19
20
    /**
21
     * Base Uri
22
     * @var string
23
     */
24
    private $base;
25
26
    /**
27
     * Comment array
28
     * @var string[]
29
     */
30
    private $comments = [];
31
32
    /**
33
     * Client cache
34
     * @var CommentClient
35
     */
36
    private $client;
37
38
    /**
39
     * Comment constructor.
40
     *
41
     * @param string $base
42
     * @param string $userAgent
43
     */
44
    public function __construct($base, $userAgent)
45
    {
46
        $this->base = $base;
47
        $this->userAgent = $userAgent;
48
    }
49
50
    /**
51
     * Add
52
     *
53
     * @param string $line
54
     * @return bool
55
     */
56
    public function add($line)
57
    {
58
        $this->comments[] = $line;
59
        return true;
60
    }
61
62
    /**
63
     * Client
64
     *
65
     * @return CommentClient
66
     */
67
    public function client()
68
    {
69
        if (isset($this->client)) {
70
            return $this->client;
71
        }
72
        return $this->client = new CommentClient($this->base, $this->userAgent, $this->comments);
73
    }
74
75
    /**
76
     * Render
77
     *
78
     * @return string[]
79
     */
80
    public function render()
81
    {
82
        $result = [];
83
        foreach ($this->comments as $comment) {
84
            $result[] = self::DIRECTIVE_COMMENT . ':' . $comment;
85
        }
86
        return $result;
87
    }
88
}
89