Completed
Pull Request — master (#13)
by ARCANEDEV
27:17 queued 10:33
created

CurlOptions::make()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 23
ccs 15
cts 15
cp 1
rs 9.0856
cc 2
eloc 14
nc 2
nop 5
crap 2
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 664
    public function setOptions(array $options)
48
    {
49 664
        foreach($options as $option => $value) {
50 664
            $this->setOption($option, $value);
51 531
        }
52
53 664
        return $this;
54
    }
55
56
    /**
57
     * Add Option.
58
     *
59
     * @param  int    $option
60
     * @param  mixed  $value
61
     *
62
     * @return self
63
     */
64 664
    public function setOption($option, $value)
65
    {
66 664
        $this->options[$option] = $value;
67
68 664
        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
    public function make($method, $url, $params, $headers, $hasFile = false)
87
    {
88 679
        $this->checkMethod($method);
89
90 679
        $this->options = [];
91
92 669
        $this->prepareMethodOptions($method, $params, $hasFile);
93
        $this->setOptions([
94 669
            CURLOPT_URL            => str_utf8($url),
95
            CURLOPT_RETURNTRANSFER => true,
96 664
            CURLOPT_CONNECTTIMEOUT => 30,
97 664
            CURLOPT_TIMEOUT        => 80,
98 664
            CURLOPT_HTTPHEADER     => $headers,
99 664
        ]);
100 664
101 664
        if ( ! Stripe::$verifySslCerts) {
102 531
            $this->setOption(CURLOPT_SSL_VERIFYPEER, false);
103
        }
104 664
105 5
        $this->setOption(CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
106 4
107
        return $this;
108 664
    }
109
110 664
    /**
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
    private function prepareMethodOptions($method, $params, $hasFile)
120
    {
121
        if ($method === 'GET' && $hasFile) {
122 669
            throw new ApiException(
123
                'Issuing a GET request with a file parameter'
124 669
            );
125 5
        }
126 1
127 4
        $options = [
128
            'GET'  => [
129
                CURLOPT_HTTPGET       => true,
130
                CURLOPT_CUSTOMREQUEST => 'GET'
131
            ],
132 664
            'POST' => [
133 664
                CURLOPT_POST          => true,
134 664
                CURLOPT_CUSTOMREQUEST => 'POST',
135
                CURLOPT_POSTFIELDS    => $params
136 664
            ],
137 664
            'DELETE' => [
138 664
                CURLOPT_CUSTOMREQUEST => 'DELETE'
139 531
            ]
140
        ];
141 664
142 531
        $this->setOptions($options[$method]);
143 531
    }
144
145 664
    /**
146 664
     * Get all options.
147
     *
148
     * @return array
149
     */
150
    public function get()
151
    {
152
        return $this->options;
153 669
    }
154
155 669
    /* ------------------------------------------------------------------------------------------------
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
    private function checkMethod(&$method)
168
    {
169
        if (! is_string($method)) {
170 679
            throw new InvalidArgumentException(
171
                'The method must be a string value, ' . gettype($method) . ' given',
172 679
                500
173 5
            );
174 5
        }
175 1
176 4
        $method = strtoupper(trim($method));
177
178
        if (! in_array($method, ['GET', 'POST', 'DELETE'])) {
179 674
            throw new BadMethodCallException(
180
                'The method [' . $method . '] is not allowed',
181 674
                405
182 5
            );
183 5
        }
184 1
    }
185
}
186