Completed
Pull Request — master (#60)
by ARCANEDEV
08:31
created

HeaderBag::getDefaults()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5.0061

Importance

Changes 0
Metric Value
cc 5
eloc 18
nc 16
nop 2
dl 0
loc 27
ccs 15
cts 16
cp 0.9375
crap 5.0061
rs 8.439
c 0
b 0
f 0
1
<?php namespace Arcanedev\Stripe\Http\Curl;
2
3
use Arcanedev\Stripe\Contracts\Http\Curl\HeaderBag as HeaderBagContract;
4
use Arcanedev\Stripe\Stripe;
5
6
/**
7
 * Class     HeaderBag
8
 *
9
 * @package  Arcanedev\Stripe\Http\Curl
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class HeaderBag implements HeaderBagContract
13
{
14
    /* ------------------------------------------------------------------------------------------------
15
     |  Properties
16
     | ------------------------------------------------------------------------------------------------
17
     */
18
    /** @var array */
19
    protected $headers = [];
20
21
    /* ------------------------------------------------------------------------------------------------
22
     |  Constructor
23
     | ------------------------------------------------------------------------------------------------
24
     */
25
    /**
26
     * Create the HeaderBag instance.
27
     */
28 20
    public function __construct()
29
    {
30 20
        $this->headers = [];
31 20
    }
32
33
    /* ------------------------------------------------------------------------------------------------
34
     |  Getters & Setters
35
     | ------------------------------------------------------------------------------------------------
36
     */
37
    /**
38
     * Get default headers.
39
     *
40
     * @param  string  $apiKey
41
     * @param  bool    $hasFile
42
     *
43
     * @return array
44
     */
45 312
    private function getDefaults($apiKey, $hasFile = false)
46
    {
47 312
        $uaString = 'Stripe/v1 PhpBindings/' . Stripe::VERSION;
48 312
        $ua       = self::getUserAgent();
49 312
        $appInfo  = Stripe::getAppInfo();
50
51 312
        if ( ! empty($appInfo)) {
52 298
            $uaString         .= ' ' . self::formatAppInfo($appInfo);
53 298
            $ua['application'] = $appInfo;
54
        }
55
56
        $defaults = [
57 312
            'X-Stripe-Client-User-Agent' => json_encode($ua),
58 312
            'User-Agent'                 => $uaString,
59 312
            'Authorization'              => 'Bearer ' . $apiKey,
60 312
            'Content-Type'               => $hasFile ? 'multipart/form-data' : 'application/x-www-form-urlencoded',
61
            'Expect'                     => null,
62
        ];
63
64 312
        if (Stripe::hasApiVersion())
65 312
            $defaults['Stripe-Version'] = Stripe::getApiVersion();
66
67 312
        if (Stripe::hasAccountId())
68
            $defaults['Stripe-Account'] = Stripe::getAccountId();
69
70 312
        return $defaults;
71
    }
72
73
    /**
74
     * Get User Agent.
75
     *
76
     * @return array
77
     */
78 312
    private static function getUserAgent()
79
    {
80 312
        $httplib = 'unknown';
81 312
        $ssllib  = 'unknown';
82
83 312
        if (function_exists('curl_version')) {
84 312
            $curlVersion = curl_version();
85 312
            $httplib     = 'curl '.$curlVersion['version'];
86 312
            $ssllib      = $curlVersion['ssl_version'];
87
        }
88
89
        return [
90 312
            'bindings_version' => Stripe::VERSION,
91 312
            'lang'             => 'php',
92 312
            'lang_version'     => phpversion(),
93 312
            'publisher'        => 'stripe',
94 312
            'uname'            => php_uname(),
95 312
            'httplib'          => $httplib,
96 312
            'ssllib'           => $ssllib,
97
        ];
98
    }
99
100
    /**
101
     * Format the Application's information.
102
     *
103
     * @param  array  $appInfo
104
     *
105
     * @return string|null
106
     */
107 298
    private static function formatAppInfo(array $appInfo)
108
    {
109 298
        $string = $appInfo['name'];
110
111 298
        if ($appInfo['version'] !== null)
112 298
            $string .= '/'.$appInfo['version'];
113
114 298
        if ($appInfo['url'] !== null)
115 298
            $string .= ' ('.$appInfo['url'].')';
116
117 298
        return $string;
118
    }
119
120
    /* ------------------------------------------------------------------------------------------------
121
     |  Main functions
122
     | ------------------------------------------------------------------------------------------------
123
     */
124
    /**
125
     * Make Header Bag.
126
     *
127
     * @param  string  $apiKey
128
     * @param  array   $headers
129
     * @param  bool    $hasFile
130
     *
131
     * @return array
132
     */
133 4
    public function make($apiKey, array $headers = [], $hasFile = false)
134
    {
135 4
        return $this->prepare($apiKey, $headers, $hasFile)->get();
136
    }
137
138
    /**
139
     * Prepare Headers.
140
     *
141
     * @param  string  $apiKey
142
     * @param  array   $headers
143
     * @param  bool    $hasFile
144
     *
145
     * @return self
146
     */
147 308
    public function prepare($apiKey, array $headers = [], $hasFile = false)
148
    {
149 308
        $this->headers = []; // RESET
150 308
        $this->headers = array_merge(self::getDefaults($apiKey, $hasFile), $headers);
151
152 308
        return $this;
153
    }
154
155
    /**
156
     * Get all the headers.
157
     *
158
     * @return array
159
     */
160 308
    public function all()
161
    {
162 308
        return $this->headers;
163
    }
164
165
    /**
166
     * Get all headers.
167
     *
168
     * @return array
169
     */
170 304
    public function get()
171
    {
172 304
        $headers = $this->all();
173
174 304
        array_walk($headers, function(&$value, $header) {
175 304
            $value = "$header: $value";
176 304
        });
177
178 304
        return array_values($headers);
179
    }
180
181
    /**
182
     * Add a Header to collection.
183
     *
184
     * @param  string  $name
185
     * @param  string  $value
186
     *
187
     * @return self
188
     */
189 2
    public function set($name, $value)
190
    {
191 2
        $this->headers[$name] = $value;
192
193 2
        return $this;
194
    }
195
196
    /**
197
     * Get all headers.
198
     *
199
     * @return array
200
     */
201 4
    public function toArray()
202
    {
203 4
        return $this->all();
204
    }
205
206
    /**
207
     * Return headers count.
208
     *
209
     * @return int
210
     */
211 4
    public function count()
212
    {
213 4
        return count($this->all());
214
    }
215
}
216