Passed
Branch etl-core (c0e492)
by Jean Paul
01:52
created

WebPageHandler::read()   A

Complexity

Conditions 3
Paths 10

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 25
rs 9.8333
cc 3
nc 10
nop 0
1
<?php
2
3
namespace Coco\SourceWatcher\Watcher\Handler;
4
5
use DOMDocument;
6
use Exception;
7
8
class WebPageHandler implements Handler
9
{
10
    protected string $url;
11
    protected string $html;
12
    protected DOMDocument $dom;
13
14
    public function __construct ( string $url )
15
    {
16
        $this->url = $url;
17
        $this->html = "";
18
        $this->dom = new DOMDocument();
19
    }
20
21
    public function getUrl () : string
22
    {
23
        return $this->url;
24
    }
25
26
    public function setUrl ( string $url ) : void
27
    {
28
        $this->url = $url;
29
    }
30
31
    public function read () : void
32
    {
33
        if ( $this->url == null ) {
34
            throw new Exception( "A URL must be set before reading" );
35
        }
36
37
        try {
38
            $ch = curl_init();
39
40
            $timeout = 5;
41
42
            curl_setopt( $ch, CURLOPT_URL, $this->url );
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
            curl_setopt( /** @scrutinizer ignore-type */ $ch, CURLOPT_URL, $this->url );
Loading history...
43
            curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
44
            curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
45
46
            $this->html = curl_exec( $ch );
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
            $this->html = curl_exec( /** @scrutinizer ignore-type */ $ch );
Loading history...
47
48
            curl_close( $ch );
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
            curl_close( /** @scrutinizer ignore-type */ $ch );
Loading history...
49
50
            # Create a DOM parser object
51
            $this->dom = new DOMDocument();
52
53
            # The @ before the method call suppresses any warnings that loadHTML might throw because of invalid HTML in the page.
54
            @$this->dom->loadHTML( $this->html );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for loadHTML(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

54
            /** @scrutinizer ignore-unhandled */ @$this->dom->loadHTML( $this->html );

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
55
        } catch ( Exception $e ) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
56
57
        }
58
    }
59
60
    public function getDom () : DOMDocument
61
    {
62
        return $this->dom;
63
    }
64
65
    public function getHtml () : string
66
    {
67
        return $this->html;
68
    }
69
}
70