CurlOptions   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 160
ccs 45
cts 45
cp 1
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 4

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setOption() 0 6 1
A get() 0 4 1
A setOptions() 0 8 2
A make() 0 20 2
A prepareMethodOptions() 0 22 3
A checkMethod() 0 13 3
1
<?php namespace Arcanedev\Stripe\Http\Curl;
2
3
use Arcanedev\Stripe\Contracts\Http\Curl\CurlOptions as CurlOptionsContract;
4
use Arcanedev\Stripe\Exceptions\ApiException;
5
use Arcanedev\Stripe\Exceptions\BadMethodCallException;
6
use Arcanedev\Stripe\Exceptions\InvalidArgumentException;
7
use Arcanedev\Stripe\Stripe;
8
9
/**
10
 * Class     CurlOptions
11
 *
12
 * @package  Arcanedev\Stripe\Http\Curl
13
 * @author   ARCANEDEV <[email protected]>
14
 */
15
class CurlOptions implements CurlOptionsContract
16
{
17
    /* ------------------------------------------------------------------------------------------------
18
     |  Properties
19
     | ------------------------------------------------------------------------------------------------
20
     */
21
    /** @var array */
22
    protected $options = [];
23
24
    /* ------------------------------------------------------------------------------------------------
25
     |  Constructor
26
     | ------------------------------------------------------------------------------------------------
27
     */
28
    /**
29
     * Create the CurlOptions instance.
30
     */
31 20
    public function __construct()
32
    {
33 20
        $this->options = [];
34 20
    }
35
36
    /* ------------------------------------------------------------------------------------------------
37
     |  Getters & Setters
38
     | ------------------------------------------------------------------------------------------------
39
     */
40
    /**
41
     * Set Options.
42
     *
43
     * @param  array  $options
44
     *
45
     * @return self
46
     */
47 308
    public function setOptions(array $options)
48
    {
49 308
        foreach($options as $option => $value) {
50 308
            $this->setOption($option, $value);
51
        }
52
53 308
        return $this;
54
    }
55
56
    /**
57
     * Add Option.
58
     *
59
     * @param  int    $option
60
     * @param  mixed  $value
61
     *
62
     * @return self
63
     */
64 308
    public function setOption($option, $value)
65
    {
66 308
        $this->options[$option] = $value;
67
68 308
        return $this;
69
    }
70
71
    /* ------------------------------------------------------------------------------------------------
72
     |  Main Functions
73
     | ------------------------------------------------------------------------------------------------
74
     */
75
    /**
76
     * Make Curl Options.
77
     *
78
     * @param  string  $method
79
     * @param  string  $url
80
     * @param  string  $params
81
     * @param  array   $headers
82
     * @param  bool    $hasFile
83
     *
84
     * @return self
85
     */
86 312
    public function make($method, $url, $params, $headers, $hasFile = false)
87
    {
88 312
        $this->checkMethod($method);
89
90 308
        $this->options = [];
91
92 308
        $this->prepareMethodOptions($method, $params, $hasFile);
93 306
        $this->setOptions([
94 306
            CURLOPT_URL            => str_utf8($url),
95 306
            CURLOPT_RETURNTRANSFER => true,
96 306
            CURLOPT_CONNECTTIMEOUT => 30,
97 306
            CURLOPT_TIMEOUT        => 80,
98 306
            CURLOPT_HTTPHEADER     => $headers,
99
        ]);
100
101 306
        if ( ! Stripe::$verifySslCerts)
102 2
            $this->setOption(CURLOPT_SSL_VERIFYPEER, false);
103
104 306
        return $this;
105
    }
106
107
    /**
108
     * Prepare options based on METHOD.
109
     *
110
     * @param  string  $method
111
     * @param  string  $params
112
     * @param  bool    $hasFile
113
     *
114
     * @throws \Arcanedev\Stripe\Exceptions\ApiException
115
     */
116 308
    private function prepareMethodOptions($method, $params, $hasFile)
117
    {
118 308
        if ($method === 'GET' && $hasFile)
119 2
            throw new ApiException('Issuing a GET request with a file parameter');
120
121
        $options = [
122 306
            'GET'  => [
123 306
                CURLOPT_HTTPGET       => true,
124 306
                CURLOPT_CUSTOMREQUEST => 'GET'
125
            ],
126
            'POST' => [
127 306
                CURLOPT_POST          => true,
128 306
                CURLOPT_CUSTOMREQUEST => 'POST',
129 306
                CURLOPT_POSTFIELDS    => $params
130
            ],
131
            'DELETE' => [
132 306
                CURLOPT_CUSTOMREQUEST => 'DELETE'
133
            ]
134
        ];
135
136 306
        $this->setOptions($options[$method]);
137 306
    }
138
139
    /**
140
     * Get all options.
141
     *
142
     * @return array
143
     */
144 310
    public function get()
145
    {
146 310
        return $this->options;
147
    }
148
149
    /* ------------------------------------------------------------------------------------------------
150
     |  Check Functions
151
     | ------------------------------------------------------------------------------------------------
152
     */
153
    /**
154
     * Check Method.
155
     *
156
     * @param  string  $method
157
     *
158
     * @throws \Arcanedev\Stripe\Exceptions\BadMethodCallException
159
     * @throws \Arcanedev\Stripe\Exceptions\InvalidArgumentException
160
     */
161 312
    private function checkMethod(&$method)
162
    {
163 312
        if ( ! is_string($method))
164 2
            throw new InvalidArgumentException(
165 2
                'The method must be a string value, '.gettype($method).' given', 500
166
            );
167
168 310
        $method = strtoupper(trim($method));
169
170 310
        if ( ! in_array($method, ['GET', 'POST', 'DELETE'])) {
171 2
            throw new BadMethodCallException("The method [{$method}] is not allowed", 405);
172
        }
173 308
    }
174
}
175