Completed
Push — master ( 25fc39...0c3403 )
by ARCANEDEV
29s
created

CurlOptions::setOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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