Completed
Push — master ( d87479...829d71 )
by Joschi
02:45
created

Dom   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 67
ccs 28
cts 28
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 16 4
A createFromString() 0 5 1
A createViaStreamWrapper() 0 19 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 © 2017 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 © 2017 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 Guzzle\Common\Exception\RuntimeException as GuzzleRuntimeException;
40
use Guzzle\Http\Client;
41
use Guzzle\Http\Url;
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
     * @return \DOMDocument DOM document
59
     * @throws RuntimeException If the request wasn't successful
60
     * @throws RuntimeException If a runtime exception occurred
61
     */
62 2
    protected static function createViaHttpClient($url, array $options = [])
63
    {
64
        try {
65 2
            $guzzleUrl = Url::factory($url);
66 2
            $clientOptions = isset($options['client']) ? (array)$options['client'] : [];
67 2
            $client = new Client($guzzleUrl, array_merge(['timeout' => 10.0], $clientOptions));
68 2
            $requestOptions = isset($options['request']) ? (array)$options['request'] : [];
69 2
            $request = $client->get($guzzleUrl, null, $requestOptions);
70 2
            $response = $client->send($request);
71 1
            return self::createFromString(strval($response->getBody()));
72
73
            // If a runtime exception occurred
74 1
        } catch (GuzzleRuntimeException $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
     * @return \DOMDocument DOM document
84
     */
85 8
    public static function createFromString($str)
86
    {
87 8
        $dom = new DomainDom(new HtmlParser());
88 8
        return $dom->load($str);
89
    }
90
91
    /**
92
     * Create a DOM document via the PHP stream wrapper
93
     *
94
     * @param string $url URL
95
     * @param array $options Connection options
96
     * @return \DOMDocument DOM document
97
     */
98 1
    protected static function createViaStreamWrapper($url, array $options = [])
99
    {
100 1
        $clientOptions = isset($options['client']) ? (array)$options['client'] : [];
101 1
        $opts = array_merge_recursive([
102
            'http' => [
103 1
                'method' => 'GET',
104 1
                'protocol_version' => 1.1,
105 1
                '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',
106 1
                'max_redirects' => 10,
107 1
                'timeout' => 10.0,
108 1
                'header' => "Accept-language: en\r\n",
109
            ]
110 1
        ], $clientOptions);
111 1
        $context = stream_context_create($opts);
112 1
        $requestOptions = isset($options['request']) ? (array)$options['request'] : [];
113 1
        stream_context_set_params($context, $requestOptions);
114 1
        $response = @file_get_contents($url, false, $context);
115 1
        return self::createFromString($response);
116
    }
117
}
118