Completed
Push — master ( 73536c...874498 )
by Joschi
03:03
created

Dom   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 48%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 6
dl 0
loc 65
ccs 12
cts 25
cp 0.48
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createViaHttpClient() 0 18 3
A createFromString() 0 5 1
A createViaStreamWrapper() 0 16 1
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\InvalidArgumentException as GuzzleInvalidArgumentException;
40
use Guzzle\Common\Exception\RuntimeException as GuzzleRuntimeException;
41
use Guzzle\Http\Client;
42
use Guzzle\Http\Url;
43
use Jkphl\Domfactory\Domain\Dom as DomainDom;
44
use Jkphl\Domfactory\Ports\InvalidArgumentException;
45
use Jkphl\Domfactory\Ports\RuntimeException;
46
47
/**
48
 * DOM factory (internal)
49
 *
50
 * @package Jkphl\Domfactory
51
 * @subpackage Jkphl\Domfactory\Infrastructure
52
 */
53
class Dom
54
{
55
    /**
56
     * Create a DOM document using a HTTP client implementation
57
     *
58
     * @param string $url HTTP / HTTPS URL
59
     * @return \DOMDocument DOM document
60
     * @throws RuntimeException If the request wasn't successful
61
     * @throws InvalidArgumentException If an argument was invalid
62
     * @throws RuntimeException If a runtime exception occurred
63
     */
64 1
    protected static function createViaHttpClient($url)
65
    {
66
        try {
67 1
            $guzzleUrl = Url::factory($url);
68 1
            $client = new Client($guzzleUrl, ['timeout' => 10.0]);
69 1
            $request = $client->get($guzzleUrl);
70 1
            $response = $client->send($request);
71
            return self::createFromString(strval($response->getBody()));
72
73
            // If an argument was invalid
74 1
        } catch (GuzzleInvalidArgumentException $e) {
75
            throw new InvalidArgumentException($e->getMessage(), $e->getCode());
76
77
            // If a runtime exception occurred
78 1
        } catch (GuzzleRuntimeException $e) {
79 1
            throw new RuntimeException($e->getMessage(), $e->getCode());
80
        }
81 1
    }
82
83
    /**
84
     * Create a DOM document from a string
85
     *
86
     * @param string $str String
87
     * @return \DOMDocument DOM document
88
     */
89 4
    public static function createFromString($str)
90
    {
91 4
        $dom = new DomainDom(new HtmlParser());
92 4
        return $dom->load($str);
93
    }
94
95
    /**
96
     * Create a DOM document via the PHP stream wrapper
97
     *
98
     * @param string $url URL
99
     * @return \DOMDocument DOM document
100
     */
101
    protected static function createViaStreamWrapper($url)
102
    {
103
        $opts = array(
104
            'http' => array(
105
                'method' => 'GET',
106
                'protocol_version' => 1.1,
107
                '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',
108
                'max_redirects' => 10,
109
                'timeout' => 120,
110
                'header' => "Accept-language: en\r\n",
111
            )
112
        );
113
        $context = stream_context_create($opts);
114
        $response = @file_get_contents($url, false, $context);
115
        return self::createFromString($response);
116
    }
117
}
118