Passed
Push — master ( dcc761...fe98e3 )
by Jean Paul
05:52
created

ApiReader::setEndpoint()   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
7
class ApiReader implements Reader
8
{
9
    /**
10
     * @var string
11
     */
12
    protected string $endpoint;
13
14
    /**
15
     * ApiReader constructor.
16
     */
17
    public function __construct ()
18
    {
19
20
    }
21
22
    /**
23
     * @return string
24
     */
25
    public function getEndpoint () : string
26
    {
27
        return $this->endpoint;
28
    }
29
30
    /**
31
     * @param string $endpoint
32
     */
33
    public function setEndpoint ( string $endpoint ) : void
34
    {
35
        $this->endpoint = $endpoint;
36
    }
37
38
    /**
39
     * @return bool|string
40
     * @throws SourceWatcherException
41
     */
42
    public function read ()
43
    {
44
        if ( $this->endpoint == null || $this->endpoint == "" ) {
45
            throw new SourceWatcherException( "No endpoint found." );
46
        }
47
48
        $curl = curl_init();
49
50
        $timeout = 5;
51
52
        curl_setopt( $curl, CURLOPT_URL, $this->endpoint );
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

52
        curl_setopt( /** @scrutinizer ignore-type */ $curl, CURLOPT_URL, $this->endpoint );
Loading history...
53
        curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
54
        curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, $timeout );
55
56
        $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

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

58
        curl_close( /** @scrutinizer ignore-type */ $curl );
Loading history...
59
60
        return $response;
61
    }
62
}
63