Completed
Pull Request — master (#15)
by ARCANEDEV
11:13
created

RequestOptions::setApiKey()   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 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php namespace Arcanedev\Stripe\Http;
2
3
use Arcanedev\Stripe\Contracts\Http\RequestOptionsInterface;
4
use Arcanedev\Stripe\Exceptions\ApiException;
5
6
/**
7
 * Class     RequestOptions
8
 *
9
 * @package  Arcanedev\Stripe\Http
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class RequestOptions implements RequestOptionsInterface
13
{
14
    /* ------------------------------------------------------------------------------------------------
15
     |  Properties
16
     | ------------------------------------------------------------------------------------------------
17
     */
18
    /** @var string */
19
    public $apiKey;
20
21
    /** @var array */
22
    public $headers;
23
24
    /* ------------------------------------------------------------------------------------------------
25
     |  Constructor
26
     | ------------------------------------------------------------------------------------------------
27
     */
28
    /**
29
     * Create a RequestOptions instance.
30
     *
31
     * @param  string|null  $apiKey
32
     * @param  array        $headers
33
     */
34 975
    public function __construct($apiKey = null, array $headers = [])
35
    {
36 975
        $this->setApiKey($apiKey);
37 975
        $this->setHeaders($headers);
38 975
    }
39
40
    /* ------------------------------------------------------------------------------------------------
41
     |  Getters & Setters
42
     | ------------------------------------------------------------------------------------------------
43
     */
44
    /**
45
     * Get API Key.
46
     *
47
     * @return string
48
     */
49 80
    public function getApiKey()
50
    {
51 80
        return $this->apiKey;
52
    }
53
54
    /**
55
     * Set API Key.
56
     *
57
     * @param  string  $apiKey
58
     *
59
     * @return self
60
     */
61 975
    public function setApiKey($apiKey)
62
    {
63 975
        if ( ! is_null($apiKey)) {
64 20
            $this->apiKey = trim($apiKey);
65 16
        }
66
67 975
        return $this;
68
    }
69
70
    /**
71
     * Get Headers.
72
     *
73
     * @return array
74
     */
75 35
    public function getHeaders()
76
    {
77 35
        return $this->headers;
78
    }
79
80
    /**
81
     * Set Headers.
82
     *
83
     * @param  array  $headers
84
     *
85
     * @return self
86
     */
87 975
    protected function setHeaders($headers)
88
    {
89 975
        $this->headers = $headers;
90
91 975
        return $this;
92
    }
93
94
    /* ------------------------------------------------------------------------------------------------
95
     |  Main Functions
96
     | ------------------------------------------------------------------------------------------------
97
     */
98
    /**
99
     * Unpacks an options array and merges it into the existing RequestOptions object.
100
     *
101
     * @param  array|string|null  $options
102
     *
103
     * @return self
104
     */
105 280
    public function merge($options)
106
    {
107 280
        $otherOptions = self::parse($options);
108
109 280
        if (is_null($otherOptions->apiKey))
110 280
            $otherOptions->apiKey = $this->apiKey;
111
112 280
        $otherOptions->headers = array_merge($this->headers, $otherOptions->headers);
113
114 280
        return $otherOptions;
115
    }
116
117
    /**
118
     * Unpacks an options array into an Options object.
119
     *
120
     * @param  array|string|null  $options
121
     *
122
     * @return self
123
     */
124 709
    public static function parse($options)
125
    {
126 709
        self::checkOptions($options);
127
128 704
        if ($options instanceof self) return $options;
129
130 704
        if (is_null($options) || is_string($options)) {
131 679
            return new self($options, []);
132
        }
133
134
        // $options is array
135 25
        $key     = null;
136 25
        if (isset($options['api_key'])) {
137 10
            $key = $options['api_key'];
138 8
        }
139
140 25
        $headers = self::prepareHeaders($options);
141
142 25
        return new self($key, $headers);
143
    }
144
145
    /**
146
     * Prepare headers.
147
     *
148
     * @param  array  $options
149
     *
150
     * @return array
151
     */
152 25
    private static function prepareHeaders($options = [])
153
    {
154 25
        $headers = [];
155
        $keys    = [
156 25
            'idempotency_key' => 'Idempotency-Key',
157 20
            'stripe_account'  => 'Stripe-Account',
158 20
            'stripe_version'  => 'Stripe-Version',
159 20
        ];
160
161 25
        foreach ($keys as $keyFrom => $keyTo) {
162 25
            if (isset($options[$keyFrom]))
163 25
                $headers[$keyTo] = $options[$keyFrom];
164 20
        }
165
166 25
        return $headers;
167
    }
168
169
    /* ------------------------------------------------------------------------------------------------
170
     |  Check Functions
171
     | ------------------------------------------------------------------------------------------------
172
     */
173
    /**
174
     * Check if API exists.
175
     *
176
     * @return bool
177
     */
178 10
    public function hasApiKey()
179
    {
180 10
        return ! is_null($this->apiKey) && ! empty($this->apiKey);
181
    }
182
183
    /**
184
     * Check Options.
185
     *
186
     * @param  array|string|null  $options
187
     *
188
     * @throws \Arcanedev\Stripe\Exceptions\ApiException
189
     */
190 709
    private static function checkOptions($options)
191
    {
192
        if (
193 709
            ! ($options instanceof self) &&
194 709
            ! is_null($options) &&
195 709
            ! is_string($options) &&
196 166
            ! is_array($options)
197 567
        ) {
198 5
            throw new ApiException(
199 5
                'Options must be a string, an array, or null, ' . gettype($options) . ' given.'
200 4
            );
201
        }
202 704
    }
203
}
204