Passed
Push — master ( eaba1f...4ca8dc )
by Jean Paul
01:59
created

ApiReader::getResourceURL()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Coco\SourceWatcher\Core\Api;
4
5
use Coco\SourceWatcher\Core\SourceWatcherException;
6
use Coco\SourceWatcher\Utils\i18n;
7
8
/**
9
 * Class ApiReader
10
 *
11
 * When you describe the endpoint, you list the end path only (hence the term "end point").
12
 * The full path that contains both the base path and the endpoint is often called a resource URL.
13
 *
14
 * @package Coco\SourceWatcher\Core\Api
15
 */
16
class ApiReader implements Reader
17
{
18
    /**
19
     * @var string
20
     */
21
    protected ?string $resourceURL = null;
22
23
    /**
24
     * @var int
25
     */
26
    protected int $timeout = 5;
27
28
    /**
29
     * @var array
30
     */
31
    protected array $headers = [];
32
33
    /**
34
     * @var int
35
     */
36
    protected int $currentAttempt;
37
38
    /**
39
     * @var int
40
     */
41
    protected int $attempts = 3;
42
43
    /**
44
     * ApiReader constructor.
45
     */
46
    public function __construct ()
47
    {
48
        $this->currentAttempt = 1;
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    public function getResourceURL () : string
55
    {
56
        return $this->resourceURL;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->resourceURL could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
57
    }
58
59
    /**
60
     * @param string $resourceURL
61
     */
62
    public function setResourceURL ( string $resourceURL ) : void
63
    {
64
        $this->resourceURL = $resourceURL;
65
    }
66
67
    /**
68
     * @return int
69
     */
70
    public function getTimeout () : int
71
    {
72
        return $this->timeout;
73
    }
74
75
    /**
76
     * @param int $timeout
77
     */
78
    public function setTimeout ( int $timeout ) : void
79
    {
80
        $this->timeout = $timeout;
81
    }
82
83
    /**
84
     * @return array
85
     */
86
    public function getHeaders () : array
87
    {
88
        return $this->headers;
89
    }
90
91
    /**
92
     * @param array $headers
93
     */
94
    public function setHeaders ( array $headers ) : void
95
    {
96
        $this->headers = $headers;
97
    }
98
99
    /**
100
     * @return bool|string
101
     * @throws SourceWatcherException
102
     */
103
    public function read ()
104
    {
105
        if ( $this->resourceURL == null || $this->resourceURL == "" ) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $this->resourceURL of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
106
            throw new SourceWatcherException( i18n::getInstance()->getText( "en_US", ApiReader::class, "No_Resource_URL_Found" ) );
107
        }
108
109
        $curl = curl_init();
110
111
        curl_setopt( $curl, CURLOPT_URL, $this->resourceURL );
0 ignored issues
show
Bug introduced by
It seems like $curl 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

111
        curl_setopt( /** @scrutinizer ignore-type */ $curl, CURLOPT_URL, $this->resourceURL );
Loading history...
112
        curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
113
        curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, $this->timeout );
114
115
        if ( !empty( $this->headers ) ) {
116
            curl_setopt( $curl, CURLOPT_HTTPHEADER, $this->headers );
117
        }
118
119
        /**
120
         * https://www.php.net/manual/en/function.curl-exec.php
121
         *
122
         * Returns TRUE on success or FALSE on failure.
123
         * However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure.
124
         */
125
126
        $response = curl_exec( $curl );
0 ignored issues
show
Bug introduced by
It seems like $curl 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

126
        $response = curl_exec( /** @scrutinizer ignore-type */ $curl );
Loading history...
127
128
        curl_close( $curl );
0 ignored issues
show
Bug introduced by
It seems like $curl 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

128
        curl_close( /** @scrutinizer ignore-type */ $curl );
Loading history...
129
130
        if ( $response == false ) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $response of type boolean|string against false; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
131
            if ( $this->currentAttempt < $this->attempts ) {
132
                $this->currentAttempt++;
133
                $this->read();
134
            }
135
        }
136
137
        return $response;
138
    }
139
}
140