Passed
Push — master ( 2337a0...342657 )
by Niklas
02:58
created

Fetch2::setMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Hab\MeModule;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
8
class Fetch2 implements ContainerInjectableInterface
9
{
10
    use ContainerInjectableTrait;
11
12
    private $curl;
0 ignored issues
show
introduced by
The private property $curl is not used, and could be removed.
Loading history...
13
    private $url;
14
    private $method;
15
    private $data;
16
    private $sweChars;
17
18 10
    public function __construct(String $method = "GET", String $url = "", Array $data = [])
19
    {
20 10
        $this->method = $method;
21 10
        $this->url = $url;
22 10
        $this->data = $data;
23 10
        $this->sweChars = [
24
            "å" => "a",
25
            "ä" => "a",
26
            "ö" => "o"
27
        ];
28 10
    }
29
30 1
    public function setMethod(String $method = "GET")
31
    {
32 1
        $this->method = $method;
33 1
    }
34
35 1
    public function setUrl(String $url)
36
    {
37 1
        $this->url = $url;
38 1
    }
39
40 2
    public function getMethod() : String
41
    {
42 2
        return $this->method;
43
    }
44
45 8
    public function fetch(String $method, String $url, Array $params = [], Array $data = [])
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

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

45
    public function fetch(String $method, String $url, /** @scrutinizer ignore-unused */ Array $params = [], Array $data = [])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
46
    {
47 8
        $curl = curl_init();
48 8
        $url = str_replace(array_keys($this->sweChars), $this->sweChars, $url);;
49 8
        switch ($method) {
50 8
            case "GET":
51 4
                break;
52 4
            case "POST":
53 1
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
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

53
                curl_setopt(/** @scrutinizer ignore-type */ $curl, CURLOPT_POSTFIELDS, $data);
Loading history...
54 1
                curl_setopt($curl, CURLOPT_POST, true);
55 1
                break;    
56 3
            case "PUT":
57 1
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
58 1
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
59 1
                break;    
60 2
            case "DELETE":
61 1
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
62 1
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
63 1
                break;    
64
            default:
65 1
                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

65
                curl_close(/** @scrutinizer ignore-type */ $curl);
Loading history...
66 1
                return false;
67
                // break;
68
            }
69 7
        curl_setopt($curl, CURLOPT_URL, $url);
70 7
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
71 7
        $res = 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

71
        $res = curl_exec(/** @scrutinizer ignore-type */ $curl);
Loading history...
72 7
        curl_close($curl);
73
74 7
        return $res;
75
    }
76
77
    // public function multiFetch($urls = [])
78
    // {
79
    //     $mc = curl_multi_init();
80
    //     $curls = [];
81
    //     foreach ($urls as $url) {
82
    //         // var_dump($urls);
83
    //         // die();
84
    //         $curl = $this->prep($url[0], $url[1], $url[2]);
85
    //         curl_multi_add_handle($mc, $curl);
86
    //         array_push($curls, $curl);
87
    //     }
88
    //     do {
89
    //         $status = curl_multi_exec($mc, $active);
90
    //         if ($active) {
91
    //             curl_multi_select($mc);
92
    //         }
93
    //     } while ($active && $status == CURLM_OK);
94
    //     foreach ($curls as $curl) {
95
    //         curl_multi_remove_handle($mc, $curl);
96
    //     }
97
    //     curl_multi_close($mc);
98
99
    //     return $mc;
100
    // }
101
}
102