Total Complexity | 9 |
Total Lines | 92 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
5 | class RequestBuilder |
||
6 | { |
||
7 | /** |
||
8 | * @var |
||
9 | */ |
||
10 | private $requestMethod; |
||
11 | |||
12 | /** |
||
13 | * @var |
||
14 | */ |
||
15 | private $config; |
||
16 | |||
17 | /** |
||
18 | * QueryBuilder constructor. |
||
19 | * @param $requestMethod |
||
20 | * @param $config |
||
21 | */ |
||
22 | public function __construct($requestMethod = 'GET', $config = []) |
||
23 | { |
||
24 | $this->requestMethod = $requestMethod; |
||
25 | $this->config = $config; |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * @param $parameters |
||
30 | * @return array |
||
31 | */ |
||
32 | public function options($parameters = []) |
||
33 | { |
||
34 | |||
35 | $options = array_merge( |
||
36 | $this->header(), |
||
37 | $this->SSLOptions(), |
||
38 | $this->parameters($parameters) |
||
39 | ); |
||
40 | |||
41 | return $options; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @return array |
||
46 | */ |
||
47 | protected function header() |
||
48 | { |
||
49 | return ['headers' => [ |
||
50 | 'Accept' => 'application/json', |
||
51 | 'Content-type' => 'application/json', |
||
52 | 'APICode' => $this->config['APICode'], |
||
53 | 'TimetableId' => env('CELCAT_WEB_API_TIMETABLE_ID', '1'), |
||
54 | ] |
||
55 | ]; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @return mixed |
||
60 | */ |
||
61 | protected function SSLOptions() |
||
62 | { |
||
63 | if ($this->config['VerifySSL']) { |
||
64 | if (file_exists(base_path($this->config['PEM']))) { |
||
65 | return ['verify' => $this->config['PEM']]; |
||
66 | } else { |
||
67 | // TODO - Error Handling |
||
68 | throw new \RuntimeException('PEM Certificate does not exist'); |
||
69 | } |
||
70 | } |
||
71 | return ['verify' => false]; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param $parameters |
||
76 | * @return mixed |
||
77 | */ |
||
78 | protected function parameters($parameters) |
||
79 | { |
||
80 | $options = []; |
||
81 | |||
82 | if ($this->requestMethod == "GET") { |
||
83 | $options['query'] = $parameters; |
||
84 | } else { |
||
85 | $options['form_params'] = $parameters; |
||
86 | } |
||
87 | return $options; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @param $name |
||
92 | * @return string |
||
93 | */ |
||
94 | public function URL($name) |
||
97 | } |
||
98 | } |