Dom   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 71
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createViaHttpClient() 0 15 4
A createFromString() 0 6 1
A createViaStreamWrapper() 0 20 3
1
<?php
2
3
/**
4
 * dom-factory
5
 *
6
 * @category   Jkphl
7
 * @package    Jkphl\Domfactory
8
 * @subpackage Jkphl\Domfactory\Infrastructure
9
 * @author     Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright  Copyright © 2020 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license    http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2020 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Jkphl\Domfactory\Infrastructure;
38
39
use DOMDocument;
40
use GuzzleHttp\Client;
41
use GuzzleHttp\Exception\RequestException;
42
use Jkphl\Domfactory\Domain\Dom as DomainDom;
43
use Jkphl\Domfactory\Ports\RuntimeException;
44
45
/**
46
 * DOM factory (internal)
47
 *
48
 * @package    Jkphl\Domfactory
49
 * @subpackage Jkphl\Domfactory\Infrastructure
50
 */
51
class Dom
52
{
53
    /**
54
     * Create a DOM document using a HTTP client implementation
55
     *
56
     * @param string $url    HTTP / HTTPS URL
57
     * @param array $options Connection options
58
     *
59
     * @return DOMDocument DOM document
60
     * @throws RuntimeException If the request wasn't successful
61
     * @throws RuntimeException If a runtime exception occurred
62
     */
63 2
    protected static function createViaHttpClient($url, array $options = [])
64
    {
65
        try {
66 2
            $clientOptions  = isset($options['client']) ? (array)$options['client'] : [];
67 2
            $client         = new Client(array_merge(['timeout' => 10.0], $clientOptions));
68 2
            $requestOptions = isset($options['request']) ? (array)$options['request'] : [];
69 2
            $response       = $client->get($url, $requestOptions);
70
71 1
            return self::createFromString(strval($response->getBody()));
72
73
            // If a runtime exception occurred
74 1
        } catch (RequestException $e) {
75 1
            throw new RuntimeException($e->getMessage(), $e->getCode());
76
        }
77
    }
78
79
    /**
80
     * Create a DOM document from a string
81
     *
82
     * @param string $str String
83
     *
84
     * @return DOMDocument DOM document
85
     */
86 8
    public static function createFromString($str)
87
    {
88 8
        $dom = new DomainDom(new HtmlParser());
89
90 8
        return $dom->load($str);
91
    }
92
93
    /**
94
     * Create a DOM document via the PHP stream wrapper
95
     *
96
     * @param string $url    URL
97
     * @param array $options Connection options
98
     *
99
     * @return DOMDocument DOM document
100
     */
101 1
    protected static function createViaStreamWrapper($url, array $options = [])
102
    {
103 1
        $clientOptions  = isset($options['client']) ? (array)$options['client'] : [];
104 1
        $opts           = array_merge_recursive([
105
            'http' => [
106 1
                'method'           => 'GET',
107
                'protocol_version' => 1.1,
108
                'user_agent'       => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.466.4 Safari/534.3',
109
                'max_redirects'    => 10,
110
                'timeout'          => 10.0,
111
                'header'           => "Accept-language: en\r\n",
112
            ]
113
        ], $clientOptions);
114 1
        $context        = stream_context_create($opts);
115 1
        $requestOptions = isset($options['request']) ? (array)$options['request'] : [];
116 1
        stream_context_set_params($context, $requestOptions);
117 1
        $response = @file_get_contents($url, false, $context);
118
119 1
        return self::createFromString($response);
120
    }
121
}
122