Completed
Pull Request — master (#29)
by ARCANEDEV
17:52
created

HeaderBag::all()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
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 764
    private function getDefaults($apiKey, $hasFile)
46
    {
47
        $defaults = [
48 764
            'X-Stripe-Client-User-Agent' => self::getUserAgent(),
49 611
            'User-Agent'                 => 'Stripe/v1 PhpBindings/' . Stripe::VERSION,
50 764
            'Authorization'              => 'Bearer ' . $apiKey,
51 764
            'Content-Type'               => $hasFile ? 'multipart/form-data' : 'application/x-www-form-urlencoded',
52 611
            'Expect'                     => null,
53 611
        ];
54
55 764
        if (Stripe::hasApiVersion())
56 613
            $defaults['Stripe-Version'] = Stripe::getApiVersion();
57
58 764
        if (Stripe::hasAccountId())
59 611
            $defaults['Stripe-Account'] = Stripe::getAccountId();
60
61 764
        return $defaults;
62
    }
63
64
    /**
65
     * Get User Agent (JSON format).
66
     *
67
     * @return string
68
     */
69 764
    private static function getUserAgent()
70
    {
71 764
        return json_encode([
72 764
            'bindings_version' => Stripe::VERSION,
73 764
            'lang'             => 'php',
74 764
            'lang_version'     => phpversion(),
75 764
            'publisher'        => 'stripe',
76 764
            'uname'            => php_uname(),
77 611
        ]);
78
    }
79
80
    /* ------------------------------------------------------------------------------------------------
81
     |  Main functions
82
     | ------------------------------------------------------------------------------------------------
83
     */
84
    /**
85
     * Make Header Bag.
86
     *
87
     * @param  string  $apiKey
88
     * @param  array   $headers
89
     * @param  bool    $hasFile
90
     *
91
     * @return array
92
     */
93 10
    public function make($apiKey, array $headers = [], $hasFile = false)
94
    {
95 10
        return $this->prepare($apiKey, $headers, $hasFile)->get();
96
    }
97
98
    /**
99
     * Prepare Headers.
100
     *
101
     * @param  string  $apiKey
102
     * @param  array   $headers
103
     * @param  bool    $hasFile
104
     *
105
     * @return self
106
     */
107 764
    public function prepare($apiKey, array $headers = [], $hasFile = false)
108
    {
109 764
        $this->headers = [];
110
111 764
        $this->headers = array_merge(
112 764
            self::getDefaults($apiKey, $hasFile),
113
            $headers
114 611
        );
115
116 764
        return $this;
117
    }
118
119
    /**
120
     * Get all the headers.
121
     *
122
     * @return array
123
     */
124 754
    public function all()
125
    {
126 754
        return $this->headers;
127
    }
128 754
129 754
    /**
130 754
     * Get all headers.
131
     *
132 754
     * @return array
133
     */
134
    public function get()
135
    {
136
        $headers = $this->all();
137
138
        array_walk($headers, function(&$value, $header) {
139
            $value = "$header: $value";
140
        });
141
142
        return array_values($headers);
143 5
    }
144
145 5
    /**
146
     * Add a Header to collection.
147 5
     *
148
     * @param  string  $name
149
     * @param  string  $value
150
     *
151
     * @return self
152
     */
153
    public function set($name, $value)
154
    {
155 10
        $this->headers[$name] = $value;
156
157 10
        return $this;
158
    }
159
160
    /**
161
     * Get all headers.
162
     *
163
     * @return array
164
     */
165 10
    public function toArray()
166
    {
167 10
        return $this->all();
168
    }
169
170
    /**
171
     * Return headers count.
172
     *
173
     * @return int
174
     */
175
    public function count()
176
    {
177
        return count($this->all());
178
    }
179
}
180