1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ivory Http Adapter package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ivory\HttpAdapter; |
13
|
|
|
|
14
|
|
|
use Ivory\HttpAdapter\Message\InternalRequestInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author GeLo <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
abstract class AbstractCurlHttpAdapter extends AbstractHttpAdapter |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @param ConfigurationInterface|null $configuration |
23
|
|
|
* @param bool $checkExtension |
24
|
|
|
* |
25
|
|
|
* @throws HttpAdapterException |
26
|
|
|
*/ |
27
|
4300 |
|
public function __construct(ConfigurationInterface $configuration = null, $checkExtension = true) |
28
|
|
|
{ |
29
|
4300 |
|
if ($checkExtension && !function_exists('curl_init')) { |
30
|
4 |
|
throw HttpAdapterException::extensionIsNotLoaded('curl', $this->getName()); |
31
|
|
|
} |
32
|
|
|
|
33
|
4296 |
|
parent::__construct($configuration); |
34
|
4296 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param InternalRequestInterface $internalRequest |
38
|
|
|
* |
39
|
|
|
* @return int |
40
|
|
|
*/ |
41
|
1344 |
|
protected function prepareProtocolVersion(InternalRequestInterface $internalRequest) |
42
|
|
|
{ |
43
|
1344 |
|
return $internalRequest->getProtocolVersion() === InternalRequestInterface::PROTOCOL_VERSION_1_0 |
44
|
1012 |
|
? CURL_HTTP_VERSION_1_0 |
45
|
1344 |
|
: CURL_HTTP_VERSION_1_1; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param InternalRequestInterface $internalRequest |
50
|
|
|
* |
51
|
|
|
* @return array|string |
52
|
|
|
*/ |
53
|
3152 |
|
protected function prepareContent(InternalRequestInterface $internalRequest) |
54
|
|
|
{ |
55
|
3152 |
|
$files = $internalRequest->getFiles(); |
56
|
|
|
|
57
|
3152 |
|
if (empty($files)) { |
58
|
2752 |
|
return $this->prepareBody($internalRequest); |
59
|
|
|
} |
60
|
|
|
|
61
|
400 |
|
$content = []; |
62
|
|
|
|
63
|
400 |
|
foreach ($internalRequest->getDatas() as $name => $data) { |
64
|
400 |
|
$content = array_merge($content, $this->prepareRawContent($name, $data)); |
65
|
320 |
|
} |
66
|
|
|
|
67
|
400 |
|
foreach ($files as $name => $file) { |
68
|
400 |
|
$content = array_merge($content, $this->prepareRawContent($name, $file, true)); |
69
|
320 |
|
} |
70
|
|
|
|
71
|
400 |
|
return $content; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $file |
76
|
|
|
* |
77
|
|
|
* @return mixed |
78
|
|
|
*/ |
79
|
240 |
|
protected function createFile($file) |
80
|
|
|
{ |
81
|
240 |
|
return $this->isSafeUpload() ? new \CurlFile($file) : '@'.$file; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
240 |
|
protected function isSafeUpload() |
88
|
|
|
{ |
89
|
240 |
|
return defined('CURLOPT_SAFE_UPLOAD'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $name |
94
|
|
|
* @param array|string $data |
95
|
|
|
* @param bool $isFile |
96
|
|
|
* |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
400 |
|
private function prepareRawContent($name, $data, $isFile = false) |
100
|
|
|
{ |
101
|
400 |
View Code Duplication |
if (is_array($data)) { |
|
|
|
|
102
|
400 |
|
$preparedData = []; |
103
|
|
|
|
104
|
400 |
|
foreach ($data as $subName => $subData) { |
105
|
400 |
|
$preparedData = array_merge( |
106
|
320 |
|
$preparedData, |
107
|
400 |
|
$this->prepareRawContent($this->prepareName($name, $subName), $subData, $isFile) |
108
|
320 |
|
); |
109
|
320 |
|
} |
110
|
|
|
|
111
|
400 |
|
return $preparedData; |
112
|
|
|
} |
113
|
|
|
|
114
|
400 |
|
return [$name => $isFile ? $this->createFile($data) : $data]; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
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.