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
![]() |
|||||
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
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
![]() |
|||||
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
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
![]() |
|||||
73 | 3 | curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1); |
|||
74 | 3 | $res = curl_exec($this->curl); |
|||
0 ignored issues
–
show
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
![]() |
|||||
75 | 3 | curl_close($this->curl); |
|||
0 ignored issues
–
show
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
![]() |
|||||
76 | 3 | return $res; |
|||
77 | } |
||||
78 | } |
||||
79 |