Completed
Push — master ( 2b0b4a...2b0b4a )
by ARCANEDEV
51s
created

HeaderBag::getDefaults()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

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