Completed
Push — master ( 5595ec...d8eb26 )
by Jan-Petter
02:13
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\Exceptions\ClientException;
9
use vipnytt\RobotsTxtParser\Handler\EncodingHandler;
10
use vipnytt\RobotsTxtParser\Parser\RobotsTxtParser;
11
use vipnytt\RobotsTxtParser\Parser\UriParser;
12
13
/**
14
 * Class TxtClient
15
 *
16
 * @see https://github.com/VIPnytt/RobotsTxtParser/blob/master/docs/methods/TxtClient.md for documentation
17
 * @package vipnytt\RobotsTxtParser
18
 */
19
class TxtClient extends RobotsTxtParser
20
{
21
    use UriParser;
22
23
    /**
24
     * Status code
25
     * @var int|null
26
     */
27
    private $statusCode;
28
29
    /**
30
     * Robots.txt content
31
     * @var string
32
     */
33
    private $content;
34
35
    /**
36
     * Encoding
37
     * @var string
38
     */
39
    private $encoding;
40
41
    /**
42
     * TxtClient constructor.
43
     *
44
     * @param string $baseUri
45
     * @param int|null $statusCode
46
     * @param string $content
47
     * @param string $encoding
48
     * @param string|null $effectiveUri
49
     * @param int|null $byteLimit
50
     */
51
    public function __construct($baseUri, $statusCode, $content, $encoding = self::ENCODING, $effectiveUri = null, $byteLimit = self::BYTE_LIMIT)
52
    {
53
        $this->statusCode = $statusCode;
54
        $this->content = $content;
55
        $this->encoding = $encoding;
56
        $this->convertEncoding();
57
        $this->limitBytes($byteLimit);
58
        parent::__construct($baseUri, $this->content, $effectiveUri);
59
    }
60
61
    /**
62
     * Convert character encoding
63
     *
64
     * @return string
65
     */
66
    private function convertEncoding()
67
    {
68
        $convert = new EncodingHandler($this->content, $this->encoding);
69
        if (($result = $convert->auto()) !== false) {
70
            $this->encoding = self::ENCODING;
71
            mb_internal_encoding(self::ENCODING);
72
            return $this->content = $result;
73
        }
74
        mb_internal_encoding(self::ENCODING);
75
        return $this->content;
76
    }
77
78
    /**
79
     * Byte limit
80
     *
81
     * @param int|null $bytes
82
     * @return string
83
     * @throws ClientException
84
     */
85
    private function limitBytes($bytes)
86
    {
87
        if ($bytes === null) {
88
            return $this->content;
89
        } elseif ($bytes < (self::BYTE_LIMIT * 0.25)) {
90
            throw new ClientException('Byte limit is set dangerously low! Default value=' . self::BYTE_LIMIT);
91
        }
92
        return $this->content = mb_strcut($this->content, 0, intval($bytes));
93
    }
94
95
    /**
96
     * Get User-agent list
97
     *
98
     * @return string[]
99
     */
100
    public function getUserAgents()
101
    {
102
        return $this->handler->userAgent()->getUserAgents();
103
    }
104
105
    /**
106
     * Clean-param
107
     *
108
     * @return CleanParamClient
109
     */
110
    public function cleanParam()
111
    {
112
        return $this->handler->cleanParam()->client();
113
    }
114
115
    /**
116
     * Host
117
     *
118
     * @return HostClient
119
     */
120
    public function host()
121
    {
122
        return $this->handler->host()->client();
123
    }
124
125
    /**
126
     * Sitemaps
127
     *
128
     * @return SitemapClient
129
     */
130
    public function sitemap()
131
    {
132
        return $this->handler->sitemap()->client();
133
    }
134
135
    /**
136
     * User-agent specific rules
137
     *
138
     * @param string $string
139
     * @return UserAgentClient
140
     */
141
    public function userAgent($string = self::USER_AGENT)
142
    {
143
        return $this->handler->userAgent()->client($string, $this->statusCode);
144
    }
145
}
146