Completed
Push — master ( 2c514d...b5f31e )
by Jan-Petter
05:00
created

Url   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 4
c 2
b 1
f 0
lcom 0
cbo 3
dl 0
loc 31
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 4
1
<?php
2
namespace vipnytt\XRobotsTagParser\Adapters;
3
4
use GuzzleHttp\Client;
5
use GuzzleHttp\Exception\TransferException;
6
use vipnytt\XRobotsTagParser;
7
8
/**
9
 * Class url
10
 *
11
 * Request the HTTP headers from an URL
12
 *
13
 * @package vipnytt\XRobotsTagParser\Adapters
14
 */
15
class Url extends XRobotsTagParser\Adapters\GuzzleHttp
16
{
17
    /**
18
     * Constructor
19
     *
20
     * @param string $url
21
     * @param string $userAgent
22
     * @throws XRobotsTagParser\Exceptions\XrobotsTagParserException
23
     */
24
    public function __construct($url, $userAgent = '')
25
    {
26
        if (!filter_var($url, FILTER_VALIDATE_URL)) {
27
            throw new XRobotsTagParser\Exceptions\XrobotsTagParserException('Invalid URL provided');
28
        }
29
        $config = [];
30
        if (!empty($userAgent)) {
31
            $config = [
32
                'headers' => [
33
                    'User-Agent' => $userAgent
34
                ]
35
            ];
36
        }
37
        try {
38
            $client = new Client($config);
39
            $request = $client->Request('GET', $url);
40
            parent::__construct($request, $userAgent);
41
        } catch (TransferException $e) {
42
            throw new XRobotsTagParser\Exceptions\XrobotsTagParserException($e->getMessage());
43
        }
44
    }
45
}
46