Completed
Push — master ( b2d48e...6ab109 )
by ARCANEDEV
9s
created

CurlOptions::make()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 3

Importance

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