1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) 2015 Nate Brunette. |
4
|
|
|
* Distributed under the MIT License (http://opensource.org/licenses/MIT) |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Tebru\Retrofit\Generation\Handler; |
8
|
|
|
|
9
|
|
|
use Tebru\Retrofit\Annotation\Body; |
10
|
|
|
use Tebru\Retrofit\Annotation\FormUrlEncoded; |
11
|
|
|
use Tebru\Retrofit\Annotation\Header; |
12
|
|
|
use Tebru\Retrofit\Annotation\Headers; |
13
|
|
|
use Tebru\Retrofit\Annotation\JsonBody; |
14
|
|
|
use Tebru\Retrofit\Annotation\Multipart; |
15
|
|
|
use Tebru\Retrofit\Annotation\Part; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class RequestHeaderHandler |
19
|
|
|
* |
20
|
|
|
* @author Nate Brunette <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class RequestHeaderHandler extends Handler |
23
|
|
|
{ |
24
|
|
|
/** {@inheritdoc} */ |
25
|
|
|
public function handle() |
26
|
|
|
{ |
27
|
|
|
$headers = []; |
28
|
|
|
$headers = $this->setStaticHeaders($headers); |
29
|
|
|
$headers = $this->setHeaders($headers); |
30
|
|
|
$headers = $this->setContentTypeHeader($headers); |
31
|
|
|
|
32
|
|
|
$this->methodBodyBuilder->setHeaders($headers); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Set request headers if annotation exists |
37
|
|
|
* |
38
|
|
|
* @param array $headers |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
|
View Code Duplication |
private function setHeaders(array $headers) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
if (!$this->annotations->exists(Header::NAME)) { |
44
|
|
|
|
45
|
|
|
return $headers; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** @var Header $headerAnnotation */ |
49
|
|
|
foreach ($this->annotations->get(Header::NAME) as $headerAnnotation) { |
|
|
|
|
50
|
|
|
$headers[$headerAnnotation->getRequestKey()] = $headerAnnotation->getVariable(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $headers; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Set request headers from list if annotation exists |
58
|
|
|
* |
59
|
|
|
* @param array $headers |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
|
private function setStaticHeaders(array $headers) |
63
|
|
|
{ |
64
|
|
|
if (!$this->annotations->exists(Headers::NAME)) { |
65
|
|
|
return $headers; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
/** @var Headers $headersAnnotation */ |
70
|
|
|
$headersAnnotation = $this->annotations->get(Headers::NAME); |
71
|
|
|
foreach ($headersAnnotation->getHeaders() as $key => $value) { |
72
|
|
|
$headers[$key] = $value; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $headers; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Add a content type header |
80
|
|
|
* |
81
|
|
|
* @param array $headers |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
private function setContentTypeHeader(array $headers) |
85
|
|
|
{ |
86
|
|
|
if ($this->annotations->exists(JsonBody::NAME)) { |
87
|
|
|
$this->methodBodyBuilder->setJsonEncode(true); |
88
|
|
|
$headers['Content-Type'] = 'application/json'; |
89
|
|
|
|
90
|
|
|
return $headers; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if ($this->annotations->exists(FormUrlEncoded::NAME)) { |
94
|
|
|
$this->methodBodyBuilder->setFormUrlEncoded(true); |
95
|
|
|
$headers['Content-Type'] = 'application/x-www-form-urlencoded'; |
96
|
|
|
|
97
|
|
|
return $headers; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if ($this->annotations->exists(Multipart::NAME)) { |
101
|
|
|
$this->methodBodyBuilder->setMultipartEncoded(true); |
102
|
|
|
|
103
|
|
|
/** @var Multipart $annotation */ |
104
|
|
|
$annotation = $this->annotations->get(Multipart::NAME); |
105
|
|
|
$boundary = $annotation->getBoundary() ? $annotation->getBoundary() : null; |
106
|
|
|
$this->methodBodyBuilder->setBoundaryId($boundary); |
107
|
|
|
|
108
|
|
|
$headers['Content-Type'] = 'multipart/form-data; boundary=' . $boundary; |
109
|
|
|
|
110
|
|
|
return $headers; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// if there is a body, display warning |
114
|
|
|
if ($this->annotations->exists(Body::NAME) || $this->annotations->exists(Part::NAME)) { |
115
|
|
|
trigger_error('The default content type is changing in the next' |
116
|
|
|
. ' major version of Retrofit from application/x-www-form-urlencoded' |
117
|
|
|
. ' to application/json. In order to ensure a clean upgrade, make' |
118
|
|
|
. ' sure you specify a specific content type with the proper annotation' |
119
|
|
|
); |
120
|
|
|
// @codeCoverageIgnoreStart |
121
|
|
|
} |
122
|
|
|
// @codeCoverageIgnoreEnd |
123
|
|
|
|
124
|
|
|
$this->methodBodyBuilder->setFormUrlEncoded(true); |
125
|
|
|
$headers['Content-Type'] = 'application/x-www-form-urlencoded'; |
126
|
|
|
|
127
|
|
|
return $headers; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.