Request::withHeader()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types = 1);
4
5
/*
6
 * This file is part of the AppleApnPush package
7
 *
8
 * (c) Vitaliy Zhuk <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code
12
 */
13
14
namespace Apple\ApnPush\Protocol\Http;
15
16
/**
17
 * Object for presentation http request
18
 */
19
class Request
20
{
21
    /**
22
     * @var string
23
     */
24
    private $url;
25
26
    /**
27
     * @var array
28
     */
29
    private $headers;
30
31
    /**
32
     * @var string
33
     */
34
    private $content;
35
36
    /**
37
     * @var string
38
     */
39
    private $certificate = '';
40
41
    /**
42
     * @var string
43
     */
44
    private $certificatePassPhrase = '';
45
46
    /**
47
     * Constructor.
48
     *
49
     * @param string $url
50
     * @param string $content
51
     * @param array  $headers
52
     */
53
    public function __construct(string $url, string $content, array $headers = [])
54
    {
55
        $this->url = $url;
56
        $this->headers = $headers;
57
        $this->content = $content;
58
    }
59
60
    /**
61
     * Get url
62
     *
63
     * @return string
64
     */
65
    public function getUrl(): string
66
    {
67
        return $this->url;
68
    }
69
70
    /**
71
     * Add or replace header
72
     *
73
     * @param string $name
74
     * @param string $value
75
     *
76
     * @return Request
77
     */
78
    public function withHeader(string $name, string $value): Request
79
    {
80
        $cloned = clone $this;
81
82
        $cloned->headers[$name] = $value;
83
84
        return $cloned;
85
    }
86
87
    /**
88
     * Add multiple headers
89
     *
90
     * @param array $headers
91
     *
92
     * @return Request
93
     */
94
    public function withHeaders(array $headers): Request
95
    {
96
        $cloned = clone $this;
97
98
        foreach ($headers as $name => $value) {
99
            $cloned = $cloned->withHeader($name, $value);
100
        }
101
102
        return $cloned;
103
    }
104
105
    /**
106
     * Get headers
107
     *
108
     * @return array
109
     */
110
    public function getHeaders(): array
111
    {
112
        return $this->headers;
113
    }
114
115
    /**
116
     * Get content
117
     *
118
     * @return string
119
     */
120
    public function getContent(): string
121
    {
122
        return $this->content;
123
    }
124
125
    /**
126
     * Use certificate for send request
127
     *
128
     * @param string $certificate
129
     *
130
     * @return Request
131
     */
132
    public function withCertificate(string $certificate): Request
133
    {
134
        $cloned = clone $this;
135
136
        $cloned->certificate = $certificate;
137
138
        return $cloned;
139
    }
140
141
    /**
142
     * Get certificate
143
     *
144
     * @return string
145
     */
146
    public function getCertificate(): string
147
    {
148
        return $this->certificate;
149
    }
150
151
    /**
152
     * Use pass phrase of certificate for send request
153
     *
154
     * @param string $passPhrase
155
     *
156
     * @return Request
157
     */
158
    public function withCertificatePassPhrase(string $passPhrase): Request
159
    {
160
        $cloned = clone $this;
161
162
        $cloned->certificatePassPhrase = $passPhrase;
163
164
        return $cloned;
165
    }
166
167
    /**
168
     * Get certificate pass phrase
169
     *
170
     * @return string
171
     */
172
    public function getCertificatePassPhrase(): string
173
    {
174
        return $this->certificatePassPhrase;
175
    }
176
}
177