1 | <?php |
||||
2 | namespace ElePHPant\Quotation\QuotationTraits; |
||||
3 | |||||
4 | use ElePHPant\Quotation\Quotation; |
||||
5 | |||||
6 | /** |
||||
7 | * Trait QuotationTraits |
||||
8 | * User: Sérgio Danilo Jr ( @sergiodanilojr ) |
||||
9 | * Date: 15/06/2020 |
||||
10 | * Time: 18:52 |
||||
11 | */ |
||||
12 | trait QuotationTraits |
||||
13 | { |
||||
14 | /** |
||||
15 | * @var |
||||
16 | */ |
||||
17 | private $data; |
||||
18 | /** |
||||
19 | * @var string |
||||
20 | */ |
||||
21 | private $base; |
||||
22 | /** |
||||
23 | * @var string|null |
||||
24 | */ |
||||
25 | private $format; |
||||
26 | |||||
27 | /** |
||||
28 | * @var |
||||
29 | */ |
||||
30 | protected $date; |
||||
31 | /** |
||||
32 | * @var |
||||
33 | */ |
||||
34 | protected $endpoint; |
||||
35 | /** |
||||
36 | * @var string |
||||
37 | */ |
||||
38 | protected $currency; |
||||
39 | |||||
40 | |||||
41 | /** |
||||
42 | * @return mixed|null |
||||
43 | */ |
||||
44 | private function connect() |
||||
45 | { |
||||
46 | $url = $this->base . $this->endpoint; |
||||
47 | $ch = curl_init($url); |
||||
48 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
49 | $result = curl_exec($ch); |
||||
0 ignored issues
–
show
It seems like
$ch 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
![]() |
|||||
50 | $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||||
0 ignored issues
–
show
It seems like
$ch can also be of type false ; however, parameter $ch of curl_getinfo() 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
![]() |
|||||
51 | curl_close($ch); |
||||
0 ignored issues
–
show
It seems like
$ch 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
![]() |
|||||
52 | |||||
53 | if (!$this->codeResponse($httpcode)) { |
||||
54 | return null; |
||||
55 | } |
||||
56 | |||||
57 | return $this->setResponse($result); |
||||
58 | } |
||||
59 | |||||
60 | /** |
||||
61 | * @param $result |
||||
62 | * @return mixed |
||||
63 | */ |
||||
64 | private function setResponse($result) |
||||
65 | { |
||||
66 | if ($this->format == Quotation::RESPONSE_XML) { |
||||
67 | return $result; |
||||
68 | } |
||||
69 | |||||
70 | return json_decode($result); |
||||
71 | } |
||||
72 | |||||
73 | /** |
||||
74 | * @param $httpcode |
||||
75 | * @return bool |
||||
76 | */ |
||||
77 | private function codeResponse($httpcode): bool |
||||
78 | { |
||||
79 | if ($httpcode != 200) { |
||||
80 | return false; |
||||
81 | } |
||||
82 | |||||
83 | return true; |
||||
84 | } |
||||
85 | |||||
86 | /** |
||||
87 | * @param string $formatResponse |
||||
88 | * @return string |
||||
89 | */ |
||||
90 | private function setFormat(string $formatResponse): string |
||||
91 | { |
||||
92 | return ($formatResponse == Quotation::RESPONSE_JSON ? Quotation::RESPONSE_JSON : Quotation::RESPONSE_XML); |
||||
93 | } |
||||
94 | } |