Passed
Push — master ( 0aa43e...eaba1f )
by Jean Paul
06:20
created

ApiReader::setTimeout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
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 int
30
     */
31
    protected int $currentAttempt;
32
33
    /**
34
     * @var int
35
     */
36
    protected int $attempts = 3;
37
38
    /**
39
     * ApiReader constructor.
40
     */
41
    public function __construct ()
42
    {
43
        $this->currentAttempt = 1;
44
    }
45
46
    /**
47
     * @return string
48
     */
49
    public function getResourceURL () : string
50
    {
51
        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...
52
    }
53
54
    /**
55
     * @param string $resourceURL
56
     */
57
    public function setResourceURL ( string $resourceURL ) : void
58
    {
59
        $this->resourceURL = $resourceURL;
60
    }
61
62
    /**
63
     * @return int
64
     */
65
    public function getTimeout () : int
66
    {
67
        return $this->timeout;
68
    }
69
70
    /**
71
     * @param int $timeout
72
     */
73
    public function setTimeout ( int $timeout ) : void
74
    {
75
        $this->timeout = $timeout;
76
    }
77
78
    /**
79
     * @return bool|string
80
     * @throws SourceWatcherException
81
     */
82
    public function read ()
83
    {
84
        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...
85
            throw new SourceWatcherException( i18n::getInstance()->getText( "en_US", ApiReader::class, "No_Resource_URL_Found" ) );
86
        }
87
88
        $curl = curl_init();
89
90
        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

90
        curl_setopt( /** @scrutinizer ignore-type */ $curl, CURLOPT_URL, $this->resourceURL );
Loading history...
91
        curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
92
        curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, $this->timeout );
93
94
        /**
95
         * https://www.php.net/manual/en/function.curl-exec.php
96
         *
97
         * Returns TRUE on success or FALSE on failure.
98
         * However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure.
99
         */
100
101
        $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

101
        $response = curl_exec( /** @scrutinizer ignore-type */ $curl );
Loading history...
102
103
        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

103
        curl_close( /** @scrutinizer ignore-type */ $curl );
Loading history...
104
105
        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...
106
            if ( $this->currentAttempt < $this->attempts ) {
107
                $this->currentAttempt++;
108
                $this->read();
109
            }
110
        }
111
112
        return $response;
113
    }
114
}
115