Completed
Push — master ( 388c50...c3616e )
by ARCANEDEV
8s
created

HeaderBag::make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
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 320
    private function getDefaults($apiKey, $hasFile = false)
46
    {
47 320
        $uaString = 'Stripe/v1 PhpBindings/' . Stripe::VERSION;
48 320
        $ua       = self::getUserAgent();
49 320
        $appInfo  = Stripe::getAppInfo();
50
51 320
        if ( ! empty($appInfo)) {
52 306
            $uaString         .= ' ' . self::formatAppInfo($appInfo);
53 306
            $ua['application'] = $appInfo;
54 306
        }
55
56
        $defaults = [
57 320
            'X-Stripe-Client-User-Agent' => json_encode($ua),
58 320
            'User-Agent'                 => $uaString,
59 320
            'Authorization'              => 'Bearer ' . $apiKey,
60 320
            'Content-Type'               => $hasFile ? 'multipart/form-data' : 'application/x-www-form-urlencoded',
61 320
            'Expect'                     => null,
62 320
        ];
63
64 320
        if (Stripe::hasApiVersion())
65 320
            $defaults['Stripe-Version'] = Stripe::getApiVersion();
66
67 320
        if (Stripe::hasAccountId())
68 320
            $defaults['Stripe-Account'] = Stripe::getAccountId();
69
70 320
        return $defaults;
71
    }
72
73
    /**
74
     * Get User Agent.
75
     *
76
     * @return array
77
     */
78 320
    private static function getUserAgent()
79
    {
80 320
        $httplib = 'unknown';
81 320
        $ssllib  = 'unknown';
82
83 320
        if (function_exists('curl_version')) {
84 320
            $curlVersion = curl_version();
85 320
            $httplib     = 'curl '.$curlVersion['version'];
86 320
            $ssllib      = $curlVersion['ssl_version'];
87 320
        }
88
89
        return [
90 320
            'bindings_version' => Stripe::VERSION,
91 320
            'lang'             => 'php',
92 320
            'lang_version'     => phpversion(),
93 320
            'publisher'        => 'stripe',
94 320
            'uname'            => php_uname(),
95 320
            'httplib'          => $httplib,
96 320
            'ssllib'           => $ssllib,
97 320
        ];
98
    }
99
100
    /**
101
     * Format the Application's information.
102
     *
103
     * @param  array  $appInfo
104
     *
105
     * @return string|null
106
     */
107 306
    private static function formatAppInfo(array $appInfo)
108
    {
109 306
        $string = $appInfo['name'];
110
111 306
        if ($appInfo['version'] !== null)
112 306
            $string .= '/'.$appInfo['version'];
113
114 306
        if ($appInfo['url'] !== null)
115 306
            $string .= ' ('.$appInfo['url'].')';
116
117 306
        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 316
    public function prepare($apiKey, array $headers = [], $hasFile = false)
148
    {
149 316
        $this->headers = []; // RESET
150 316
        $this->headers = array_merge(self::getDefaults($apiKey, $hasFile), $headers);
151
152 316
        return $this;
153
    }
154
155
    /**
156
     * Get all the headers.
157
     *
158
     * @return array
159
     */
160 316
    public function all()
161
    {
162 316
        return $this->headers;
163
    }
164
165
    /**
166
     * Get all headers.
167
     *
168
     * @return array
169
     */
170 312
    public function get()
171
    {
172 312
        $headers = $this->all();
173
174 312
        array_walk($headers, function(&$value, $header) {
175 312
            $value = "$header: $value";
176 312
        });
177
178 312
        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