Completed
Push — master ( f53952...657bbc )
by Jan-Petter
02:28
created

Download::connectionIssue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
namespace vipnytt\RobotsTxtParser;
3
4
use GuzzleHttp;
5
use vipnytt\RobotsTxtParser\Client;
6
use vipnytt\RobotsTxtParser\Parser\RobotsTxtInterface;
7
8
/**
9
 * Class Download
10
 *
11
 * @package vipnytt\RobotsTxtParser
12
 */
13
class Download implements RobotsTxtInterface
14
{
15
    /**
16
     * Base uri
17
     * @var string
18
     */
19
    protected $baseUri;
20
21
    /**
22
     * HTTP Status code
23
     * @var int
24
     */
25
    protected $statusCode;
26
27
    /**
28
     * Robots.txt contents
29
     * @var string
30
     */
31
    protected $contents;
32
33
    /**
34
     * Robots.txt character encoding
35
     * @var string
36
     */
37
    protected $encoding;
38
39
    /**
40
     * Download constructor.
41
     *
42
     * @param string $baseUri
43
     * @param array $guzzleConfig
44
     */
45
    public function __construct($baseUri, $guzzleConfig = [])
46
    {
47
        $this->baseUri = $baseUri;
48
        try {
49
            $client = new GuzzleHttp\Client(
50
                array_merge_recursive(
51
                    [
52
                        'allow_redirects' => [
53
                            'max' => self::MAX_REDIRECTS,
54
                            'referer' => true,
55
                            'strict' => true,
56
                            'track_redirects' => true,
57
                        ],
58
                        'base_uri' => $baseUri,
59
                        'headers' => [
60
                            'Accept' => 'text/plain;q=1.0, text/*;q=0.8, */*;q=0.1',
61
                            'Accept-Charset' => 'utf-8;q=1.0, *;q=0.1',
62
                            'Accept-Encoding' => 'identity;q=1.0, *;q=0.1',
63
                            'User-Agent' => 'RobotsTxtParser-VIPnytt/1.0 (+https://github.com/VIPnytt/RobotsTxtParser/blob/master/README.md)',
64
                        ],
65
                        'http_errors' => false,
66
                        'timeout' => 60,
67
                        'verify' => true,
68
                    ],
69
                    $guzzleConfig
70
                )
71
            );
72
            $response = $client->request('GET', '/robots.txt');
73
            $this->statusCode = $response->getStatusCode();
74
            $this->contents = $response->getBody()->getContents();
75
            $this->encoding = $this->headerEncoding($response->getHeader('content-type')[0]);
76
        } catch (GuzzleHttp\Exception\TransferException $e) {
77
            $this->statusCode = 523;
78
            $this->contents = '';
79
            $this->encoding = self::ENCODING;
80
        }
81
    }
82
83
    /**
84
     * HTTP header encoding
85
     *
86
     * @param $header
87
     * @return string
88
     */
89
    protected function headerEncoding($header)
90
    {
91
        $split = array_map('trim', mb_split(';', $header));
92
        foreach ($split as $string) {
93
            if (mb_stripos($string, 'charset=') === 0) {
94
                $encoding = mb_split('=', $string, 2)[1];
95
                if (in_array(mb_strtolower($encoding), array_map('mb_strtolower', mb_list_encodings()))) {
96
                    return $encoding;
97
                }
98
            }
99
        }
100
        return $this->detectEncoding();
101
    }
102
103
    /**
104
     * Manually detect encoding
105
     *
106
     * @return string
107
     */
108
    protected function detectEncoding()
109
    {
110
        if (($encoding = mb_detect_encoding($this->getContents())) !== false) {
111
            return $encoding;
112
        }
113
        return self::ENCODING;
114
    }
115
116
    /**
117
     * URL content
118
     *
119
     * @return string
120
     */
121
    public function getContents()
122
    {
123
        return $this->contents;
124
    }
125
126
    /**
127
     * Parser client
128
     *
129
     * @param int|null $byteLimit
130
     * @return Client
131
     */
132
    public function parserClient($byteLimit = self::BYTE_LIMIT)
133
    {
134
        return new Client($this->baseUri, $this->getStatusCode(), $this->getContents(), $this->getEncoding(), $byteLimit);
135
    }
136
137
    /**
138
     * Status code
139
     *
140
     * @return int
141
     */
142
    public function getStatusCode()
143
    {
144
        return $this->statusCode;
145
    }
146
147
    /**
148
     * Encoding
149
     *
150
     * @return string
151
     */
152
    public function getEncoding()
153
    {
154
        return $this->encoding;
155
    }
156
}
157