Completed
Pull Request — master (#18)
by ARCANEDEV
07:31
created

CurlOptions::prepareMethodOptions()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 25
ccs 17
cts 17
cp 1
rs 8.8571
cc 3
eloc 15
nc 2
nop 3
crap 3
1
<?php namespace Arcanedev\Stripe\Http\Curl;
2
3
use Arcanedev\Stripe\Contracts\Http\Curl\CurlOptionsInterface;
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 CurlOptionsInterface
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 45
    public function __construct()
32
    {
33 45
        $this->options = [];
34 45
    }
35
36
    /* ------------------------------------------------------------------------------------------------
37
     |  Getters & Setters
38
     | ------------------------------------------------------------------------------------------------
39
     */
40
    /**
41
     * Set Options.
42
     *
43
     * @param  array  $options
44
     *
45
     * @return self
46
     */
47 674
    public function setOptions(array $options)
48
    {
49 674
        foreach($options as $option => $value) {
50 674
            $this->setOption($option, $value);
51 539
        }
52
53 674
        return $this;
54
    }
55
56
    /**
57
     * Add Option.
58
     *
59
     * @param  int    $option
60
     * @param  mixed  $value
61
     *
62
     * @return self
63
     */
64 674
    public function setOption($option, $value)
65
    {
66 674
        $this->options[$option] = $value;
67
68 674
        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 689
    public function make($method, $url, $params, $headers, $hasFile = false)
87
    {
88 689
        $this->checkMethod($method);
89
90 679
        $this->options = [];
91
92 679
        $this->prepareMethodOptions($method, $params, $hasFile);
93 674
        $this->setOptions([
94 674
            CURLOPT_URL            => str_utf8($url),
95 674
            CURLOPT_RETURNTRANSFER => true,
96 674
            CURLOPT_CONNECTTIMEOUT => 30,
97 674
            CURLOPT_TIMEOUT        => 80,
98 674
            CURLOPT_HTTPHEADER     => $headers,
99 539
        ]);
100
101 674
        if ( ! Stripe::$verifySslCerts) {
102 5
            $this->setOption(CURLOPT_SSL_VERIFYPEER, false);
103 4
        }
104
105 674
        $this->setOption(CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
106
107 674
        return $this;
108
    }
109
110
    /**
111
     * Prepare options based on METHOD.
112
     *
113
     * @param  string  $method
114
     * @param  string  $params
115
     * @param  bool    $hasFile
116
     *
117
     * @throws \Arcanedev\Stripe\Exceptions\ApiException
118
     */
119 679
    private function prepareMethodOptions($method, $params, $hasFile)
120
    {
121 679
        if ($method === 'GET' && $hasFile) {
122 5
            throw new ApiException(
123 1
                'Issuing a GET request with a file parameter'
124 4
            );
125
        }
126
127
        $options = [
128
            'GET'  => [
129 674
                CURLOPT_HTTPGET       => true,
130 674
                CURLOPT_CUSTOMREQUEST => 'GET'
131 674
            ],
132
            'POST' => [
133 674
                CURLOPT_POST          => true,
134 674
                CURLOPT_CUSTOMREQUEST => 'POST',
135 674
                CURLOPT_POSTFIELDS    => $params
136 539
            ],
137
            'DELETE' => [
138 674
                CURLOPT_CUSTOMREQUEST => 'DELETE'
139 539
            ]
140 539
        ];
141
142 674
        $this->setOptions($options[$method]);
143 674
    }
144
145
    /**
146
     * Get all options.
147
     *
148
     * @return array
149
     */
150 679
    public function get()
151
    {
152 679
        return $this->options;
153
    }
154
155
    /* ------------------------------------------------------------------------------------------------
156
     |  Check Functions
157
     | ------------------------------------------------------------------------------------------------
158
     */
159
    /**
160
     * Check Method.
161
     *
162
     * @param  string  $method
163
     *
164
     * @throws \Arcanedev\Stripe\Exceptions\BadMethodCallException
165
     * @throws \Arcanedev\Stripe\Exceptions\InvalidArgumentException
166
     */
167 689
    private function checkMethod(&$method)
168
    {
169 689
        if (! is_string($method)) {
170 5
            throw new InvalidArgumentException(
171 5
                'The method must be a string value, ' . gettype($method) . ' given',
172 1
                500
173 4
            );
174
        }
175
176 684
        $method = strtoupper(trim($method));
177
178 684
        if (! in_array($method, ['GET', 'POST', 'DELETE'])) {
179 5
            throw new BadMethodCallException(
180 5
                'The method [' . $method . '] is not allowed',
181 1
                405
182 4
            );
183
        }
184 679
    }
185
}
186