Fetch   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
eloc 41
c 0
b 0
f 0
dl 0
loc 69
ccs 43
cts 43
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A applyMethod() 0 20 5
A __construct() 0 6 1
A setMethod() 0 3 1
A applyParams() 0 9 4
A fetch() 0 7 1
A getMethod() 0 3 1
1
<?php
2
3
namespace Hab\MeModule;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
8
class Fetch implements ContainerInjectableInterface
9
{
10
    use ContainerInjectableTrait;
11
12
    private $curl;
13
    private $url;
14
    private $method;
15
    private $data;
16
17 6
    public function __construct(String $method, String $url, Array $data = [])
18
    {
19 6
        $this->curl = curl_init();
20 6
        $this->method = $method;
21 6
        $this->url = $url;
22 6
        $this->data = $data;
23 6
    }
24
25 3
    public function setMethod(String $method = "GET")
26
    {
27 3
        $this->method = $method;
28 3
    }
29
30 3
    public function getMethod() : String
31
    {
32 3
        return $this->method;
33
    }
34
35 2
    public function applyMethod(String $method)
36
    {
37 2
        switch ($method) {
38 2
            case "GET":
39 1
                $this->setMethod("GET");
40 1
                return "GET";
41 2
            case "POST":
42 1
                curl_setopt($this->curl, CURLOPT_POST, true);
0 ignored issues
show
Bug introduced by
It seems like $this->curl can also be of type boolean; 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 */ $this->curl, CURLOPT_POST, true);
Loading history...
43 1
                $this->setMethod("POST");
44 1
                return "POST";
45 2
            case "PUT":
46 2
                curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, "PUT");
47 2
                $this->setMethod("PUT");
48 2
                return "PUT";
49 2
            case "DELETE":
50 2
                curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, "DELETE");
51 2
                $this->setMethod("DELETE");
52 2
                return "DELETE";
53
            default:
54 2
                return false;
55
        }
56
    }
57
58 1
    public function applyParams(Array $data = [])
59
    {
60
        // $post = $this->di->get("request")->getPost();
61 1
        switch ($this->getMethod()) {
62 1
            case "POST":
63 1
            case "PUT":
64 1
            case "DELETE":
65 1
                curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data);
0 ignored issues
show
Bug introduced by
It seems like $this->curl can also be of type boolean; 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

65
                curl_setopt(/** @scrutinizer ignore-type */ $this->curl, CURLOPT_POSTFIELDS, $data);
Loading history...
66 1
                break;
67
        }
68 1
    }
69
70 3
    public function fetch()
71
    {
72 3
        curl_setopt($this->curl, CURLOPT_URL, $this->url);
0 ignored issues
show
Bug introduced by
It seems like $this->curl can also be of type boolean; 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

72
        curl_setopt(/** @scrutinizer ignore-type */ $this->curl, CURLOPT_URL, $this->url);
Loading history...
73 3
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
74 3
        $res = curl_exec($this->curl);
0 ignored issues
show
Bug introduced by
It seems like $this->curl can also be of type boolean; 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

74
        $res = curl_exec(/** @scrutinizer ignore-type */ $this->curl);
Loading history...
75 3
        curl_close($this->curl);
0 ignored issues
show
Bug introduced by
It seems like $this->curl can also be of type boolean; 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

75
        curl_close(/** @scrutinizer ignore-type */ $this->curl);
Loading history...
76 3
        return $res;
77
    }
78
}
79