1 | <?php |
||||||||
2 | |||||||||
3 | namespace MedianetDev\PConnector\Concerns; |
||||||||
4 | |||||||||
5 | trait Configurations |
||||||||
6 | { |
||||||||
7 | /** |
||||||||
8 | * @var bool |
||||||||
9 | * |
||||||||
10 | * Whether to use authentication header or not |
||||||||
11 | */ |
||||||||
12 | private $withAuthentication; |
||||||||
13 | |||||||||
14 | /** |
||||||||
15 | * @var bool |
||||||||
16 | * |
||||||||
17 | * The gateway profile |
||||||||
18 | */ |
||||||||
19 | private $profile; |
||||||||
20 | |||||||||
21 | /** |
||||||||
22 | * @var bool |
||||||||
23 | * |
||||||||
24 | * Should we log the request and the response or not |
||||||||
25 | */ |
||||||||
26 | private $allowDebugging; |
||||||||
27 | |||||||||
28 | /** |
||||||||
29 | * @var string |
||||||||
30 | * |
||||||||
31 | * Parse response as [string, object, array] |
||||||||
32 | */ |
||||||||
33 | private $decodeResponse; |
||||||||
34 | |||||||||
35 | /** |
||||||||
36 | * @var string |
||||||||
37 | * |
||||||||
38 | * The url for the current used profile. |
||||||||
39 | */ |
||||||||
40 | private $url; |
||||||||
41 | |||||||||
42 | /** |
||||||||
43 | * @var bool |
||||||||
44 | * |
||||||||
45 | * Should send data in json with get METHOD |
||||||||
46 | */ |
||||||||
47 | private $withJson = false; |
||||||||
48 | |||||||||
49 | private function updateSettings($profile) |
||||||||
50 | { |
||||||||
51 | $this->withAuthentication = config('p-connector.profiles.'.$profile.'.auth.authenticate_by_default', config('p-connector.auth.authenticate_by_default', false)); |
||||||||
52 | $this->allowDebugging = config('p-connector.profiles.'.$profile.'.log', config('p-connector.log', false)); |
||||||||
53 | $this->decodeResponse = config('p-connector.profiles.'.$profile.'.decode_response', config('p-connector.decode_response', false)); |
||||||||
54 | } |
||||||||
55 | |||||||||
56 | /** |
||||||||
57 | * Set the profile to use before sending the request. |
||||||||
58 | * |
||||||||
59 | * It's **RECOMMENDED** to use the profile before using any other setting function to not override any setting |
||||||||
60 | * |
||||||||
61 | * @param string $profile The profile name |
||||||||
62 | * @return \MedianetDev\PConnector\PConnector |
||||||||
63 | */ |
||||||||
64 | public function profile(string $profile) |
||||||||
65 | { |
||||||||
66 | if (! in_array($profile, array_keys(config('p-connector.profiles')))) { |
||||||||
67 | throw new InvalidArgumentException('The profile"'.$profile.'" does not exist!'); |
||||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||||
68 | } |
||||||||
69 | $this->profile = $profile; |
||||||||
0 ignored issues
–
show
The property
$profile was declared of type boolean , but $profile is of type string . Maybe add a type cast?
This check looks for assignments to scalar types that may be of the wrong type. To ensure the code behaves as expected, it may be a good idea to add an explicit type cast. $answer = 42;
$correct = false;
$correct = (bool) $answer;
![]() |
|||||||||
70 | $this->updateSettings($profile); |
||||||||
71 | |||||||||
72 | return $this; |
||||||||
73 | } |
||||||||
74 | |||||||||
75 | /** |
||||||||
76 | * Send language using the app locale through the Accept-Language header. |
||||||||
77 | * |
||||||||
78 | * @param string $locale [optional] The locale will default to the app.locale if not provided |
||||||||
79 | * @return \MedianetDev\PConnector\PConnector |
||||||||
80 | */ |
||||||||
81 | public function lang(?string $locale = null) |
||||||||
82 | { |
||||||||
83 | $this->withHeader('Accept-Language', $locale ?? app()->getLocale()); |
||||||||
0 ignored issues
–
show
It seems like
withHeader() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() The method
getLocale() does not exist on Illuminate\Container\Container . Are you sure you never get this type here, but always one of the subclasses?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||||
84 | |||||||||
85 | return $this; |
||||||||
86 | } |
||||||||
87 | |||||||||
88 | /** |
||||||||
89 | * Use authentication for this request. |
||||||||
90 | * |
||||||||
91 | * @return \MedianetDev\PConnector\PConnector |
||||||||
92 | */ |
||||||||
93 | public function withAuth() |
||||||||
94 | { |
||||||||
95 | $this->withAuthentication = true; |
||||||||
96 | |||||||||
97 | return $this; |
||||||||
98 | } |
||||||||
99 | |||||||||
100 | /** |
||||||||
101 | * Don't use authentication for this request. |
||||||||
102 | * |
||||||||
103 | * @return \MedianetDev\PConnector\PConnector |
||||||||
104 | */ |
||||||||
105 | public function withoutAuth() |
||||||||
106 | { |
||||||||
107 | $this->withAuthentication = false; |
||||||||
108 | |||||||||
109 | return $this; |
||||||||
110 | } |
||||||||
111 | |||||||||
112 | /** |
||||||||
113 | * Log this request. |
||||||||
114 | * |
||||||||
115 | * @return \MedianetDev\PConnector\PConnector |
||||||||
116 | */ |
||||||||
117 | public function withLog() |
||||||||
118 | { |
||||||||
119 | $this->allowDebugging = true; |
||||||||
120 | |||||||||
121 | return $this; |
||||||||
122 | } |
||||||||
123 | |||||||||
124 | /** |
||||||||
125 | * Log this request. |
||||||||
126 | * |
||||||||
127 | * @return \MedianetDev\PConnector\PConnector |
||||||||
128 | */ |
||||||||
129 | public function withJson() |
||||||||
130 | { |
||||||||
131 | $this->withJson = true; |
||||||||
132 | |||||||||
133 | return $this; |
||||||||
134 | } |
||||||||
135 | |||||||||
136 | /** |
||||||||
137 | * Don't log this request. |
||||||||
138 | * |
||||||||
139 | * @return \MedianetDev\PConnector\PConnector |
||||||||
140 | */ |
||||||||
141 | public function withoutLog() |
||||||||
142 | { |
||||||||
143 | $this->allowDebugging = false; |
||||||||
144 | |||||||||
145 | return $this; |
||||||||
146 | } |
||||||||
147 | |||||||||
148 | /** |
||||||||
149 | * Parse the response as an object. |
||||||||
150 | * |
||||||||
151 | * @return \MedianetDev\PConnector\PConnector |
||||||||
152 | */ |
||||||||
153 | public function objectResponse() |
||||||||
154 | { |
||||||||
155 | $this->decodeResponse = 'object'; |
||||||||
156 | |||||||||
157 | return $this; |
||||||||
158 | } |
||||||||
159 | |||||||||
160 | /** |
||||||||
161 | * Parse the response as a string. |
||||||||
162 | * |
||||||||
163 | * @return \MedianetDev\PConnector\PConnector |
||||||||
164 | */ |
||||||||
165 | public function htmlResponse() |
||||||||
166 | { |
||||||||
167 | $this->decodeResponse = 'string'; |
||||||||
168 | |||||||||
169 | return $this; |
||||||||
170 | } |
||||||||
171 | |||||||||
172 | /** |
||||||||
173 | * Parse the response as an array. |
||||||||
174 | * |
||||||||
175 | * @return \MedianetDev\PConnector\PConnector |
||||||||
176 | */ |
||||||||
177 | public function arrayResponse() |
||||||||
178 | { |
||||||||
179 | $this->decodeResponse = 'array'; |
||||||||
180 | |||||||||
181 | return $this; |
||||||||
182 | } |
||||||||
183 | |||||||||
184 | /** |
||||||||
185 | * Change the url for the current used profile. |
||||||||
186 | * |
||||||||
187 | * @param string $url The new url |
||||||||
188 | * @return \MedianetDev\PConnector\PConnector |
||||||||
189 | */ |
||||||||
190 | public function url(string $url) |
||||||||
191 | { |
||||||||
192 | $this->url = $url; |
||||||||
193 | |||||||||
194 | return $this; |
||||||||
195 | } |
||||||||
196 | |||||||||
197 | /** |
||||||||
198 | * Change the url for the current used profile. |
||||||||
199 | * This is an alias for url() function. |
||||||||
200 | * |
||||||||
201 | * @param string $url The new url |
||||||||
202 | * @return \MedianetDev\PConnector\PConnector |
||||||||
203 | */ |
||||||||
204 | public function setUrl(string $url) |
||||||||
205 | { |
||||||||
206 | return $this->url($url); |
||||||||
207 | } |
||||||||
208 | } |
||||||||
209 |