RequestBuilder   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
eloc 28
c 2
b 0
f 0
dl 0
loc 92
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A URL() 0 3 1
A parameters() 0 10 2
A header() 0 7 1
A SSLOptions() 0 11 3
A __construct() 0 4 1
A options() 0 10 1
1
<?php
2
3
namespace neilherbertuk\celcatwebapi\Classes;
4
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)
95
    {
96
        return rtrim($this->config['ServerAddress'], '/').'/'.$name;
97
    }
98
}