Completed
Push — master ( 065a8d...52e320 )
by Jan-Petter
02:19
created

TxtClient::getStatusCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace vipnytt\RobotsTxtParser;
3
4
use vipnytt\RobotsTxtParser\Client\Directives\CleanParamClient;
5
use vipnytt\RobotsTxtParser\Client\Directives\HostClient;
6
use vipnytt\RobotsTxtParser\Client\Directives\SitemapClient;
7
use vipnytt\RobotsTxtParser\Client\Directives\UserAgentClient;
8
use vipnytt\RobotsTxtParser\Client\Encoding\EncodingConverter;
9
use vipnytt\RobotsTxtParser\Exceptions\ClientException;
10
use vipnytt\RobotsTxtParser\Parser\RobotsTxtParser;
11
use vipnytt\RobotsTxtParser\Parser\UriParser;
12
13
/**
14
 * Class TxtClient
15
 *
16
 * @package vipnytt\RobotsTxtParser
17
 */
18
class TxtClient extends RobotsTxtParser
19
{
20
    use UriParser;
21
22
    /**
23
     * Status code
24
     * @var int|null
25
     */
26
    private $statusCode;
27
28
    /**
29
     * Robots.txt content
30
     * @var string
31
     */
32
    private $content;
33
34
    /**
35
     * Encoding
36
     * @var string
37
     */
38
    private $encoding;
39
40
    /**
41
     * TxtClient constructor.
42
     *
43
     * @param string $baseUri
44
     * @param int $statusCode
45
     * @param string $content
46
     * @param string $encoding
47
     * @param string|null $effectiveUri
48
     * @param int|null $byteLimit
49
     */
50
    public function __construct($baseUri, $statusCode, $content, $encoding = self::ENCODING, $effectiveUri = null, $byteLimit = self::BYTE_LIMIT)
51
    {
52
        $this->statusCode = $statusCode;
53
        $this->content = $content;
54
        $this->encoding = $encoding;
55
        $this->convertEncoding();
56
        $this->limitBytes($byteLimit);
57
        parent::__construct($baseUri, $this->content, $effectiveUri);
58
    }
59
60
    /**
61
     * Convert character encoding
62
     *
63
     * @return string
64
     */
65
    private function convertEncoding()
66
    {
67
        mb_internal_encoding(self::ENCODING);
68
        $convert = new EncodingConverter($this->content, $this->encoding);
69
        if (($result = $convert->auto()) !== false) {
70
            $this->encoding = self::ENCODING;
71
            return $this->content = $result;
72
        }
73
        return $this->content;
74
    }
75
76
    /**
77
     * Byte limit
78
     *
79
     * @param int|null $bytes
80
     * @return string
81
     * @throws ClientException
82
     */
83
    private function limitBytes($bytes)
84
    {
85
        if ($bytes === null) {
86
            return $this->content;
87
        } elseif ($bytes < (self::BYTE_LIMIT * 0.25)) {
88
            throw new ClientException('Byte limit is set dangerously low! Default value=' . self::BYTE_LIMIT);
89
        }
90
        return $this->content = mb_strcut($this->content, 0, intval($bytes));
91
    }
92
93
    /**
94
     * Get Status code
95
     *
96
     * @return int|null
97
     */
98
    public function getStatusCode()
99
    {
100
        return $this->statusCode;
101
    }
102
103
    /**
104
     * Get User-agent list
105
     *
106
     * @return string[]
107
     */
108
    public function getUserAgents()
109
    {
110
        return $this->handler->userAgent()->getUserAgents();
111
    }
112
113
    /**
114
     * Clean-param
115
     *
116
     * @return CleanParamClient
117
     */
118
    public function cleanParam()
119
    {
120
        return $this->handler->cleanParam()->client();
121
    }
122
123
    /**
124
     * Host
125
     *
126
     * @return HostClient
127
     */
128
    public function host()
129
    {
130
        return $this->handler->host()->client();
131
    }
132
133
    /**
134
     * Sitemaps
135
     *
136
     * @return SitemapClient
137
     */
138
    public function sitemap()
139
    {
140
        return $this->handler->sitemap()->client();
141
    }
142
143
    /**
144
     * User-agent specific rules
145
     *
146
     * @param string $string
147
     * @return UserAgentClient
148
     */
149
    public function userAgent($string = self::USER_AGENT)
150
    {
151
        return $this->handler->userAgent()->client($string, $this->statusCode);
152
    }
153
}
154