Completed
Pull Request — master (#131)
by Eric
63:39 queued 61:20
created

AbstractHttpAdapter   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 163
Duplicated Lines 5.52 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 10.67%

Importance

Changes 0
Metric Value
wmc 28
lcom 1
cbo 5
dl 9
loc 163
ccs 8
cts 75
cp 0.1067
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A getConfiguration() 0 4 1
A setConfiguration() 0 4 1
C prepareHeaders() 0 47 14
B prepareBody() 0 28 5
A prepareName() 0 4 1
A prepareRawBody() 9 21 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
use Ivory\HttpAdapter\Normalizer\HeadersNormalizer;
16
17
/**
18
 * @author GeLo <[email protected]>
19
 */
20
abstract class AbstractHttpAdapter implements HttpAdapterInterface
21
{
22
    use HttpAdapterTrait;
23
24
    /**
25
     * @var ConfigurationInterface
26
     */
27
    private $configuration;
28
29
    /**
30
     * @param ConfigurationInterface|null $configuration
31
     */
32 413
    public function __construct(ConfigurationInterface $configuration = null)
33
    {
34 413
        $this->setConfiguration($configuration ?: new Configuration());
35 413
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 36
    public function getConfiguration()
41
    {
42 36
        return $this->configuration;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 413
    public function setConfiguration(ConfigurationInterface $configuration)
49
    {
50 413
        $this->configuration = $configuration;
51 413
    }
52
53
    /**
54
     * @param InternalRequestInterface $internalRequest
55
     * @param bool                     $associative
56
     * @param bool                     $contentType
57
     * @param bool                     $contentLength
58
     *
59
     * @return array the prepared headers
60
     */
61
    protected function prepareHeaders(
62
        InternalRequestInterface &$internalRequest,
63
        $associative = true,
64
        $contentType = true,
65
        $contentLength = false
66
    ) {
67
        if (!$internalRequest->hasHeader('Connection')) {
68
            $internalRequest = $internalRequest->withHeader(
69
                'Connection',
70
                $this->configuration->getKeepAlive() ? 'keep-alive' : 'close'
71
            );
72
        }
73
74
        if (!$internalRequest->hasHeader('Content-Type')) {
75
            $rawDatas = (string) $internalRequest->getBody();
76
            $datas = $internalRequest->getDatas();
77
            $files = $internalRequest->getFiles();
78
79
            if ($this->configuration->hasEncodingType()) {
80
                $internalRequest = $internalRequest->withHeader(
81
                    'Content-Type',
82
                    $this->configuration->getEncodingType()
83
                );
84
            } elseif ($contentType && !empty($files)) {
85
                $internalRequest = $internalRequest->withHeader(
86
                    'Content-Type',
87
                    ConfigurationInterface::ENCODING_TYPE_FORMDATA.'; boundary='.$this->configuration->getBoundary()
88
                );
89
            } elseif ($contentType && (!empty($datas) || !empty($rawDatas))) {
90
                $internalRequest = $internalRequest->withHeader(
91
                    'Content-Type',
92
                    ConfigurationInterface::ENCODING_TYPE_URLENCODED
93
                );
94
            }
95
        }
96
97
        if ($contentLength && !$internalRequest->hasHeader('Content-Length')
98
            && ($length = strlen($this->prepareBody($internalRequest))) > 0) {
99
            $internalRequest = $internalRequest->withHeader('Content-Length', (string) $length);
100
        }
101
102
        if (!$internalRequest->hasHeader('User-Agent')) {
103
            $internalRequest = $internalRequest->withHeader('User-Agent', $this->configuration->getUserAgent());
104
        }
105
106
        return HeadersNormalizer::normalize($internalRequest->getHeaders(), $associative);
107
    }
108
109
    /**
110
     * @param InternalRequestInterface $internalRequest
111
     *
112
     * @return string
113
     */
114
    protected function prepareBody(InternalRequestInterface $internalRequest)
115
    {
116
        $body = (string) $internalRequest->getBody();
117
118
        if (!empty($body)) {
119
            return $body;
120
        }
121
122
        $files = $internalRequest->getFiles();
123
124
        if (empty($files)) {
125
            return http_build_query($internalRequest->getDatas(), null, '&');
126
        }
127
128
        $body = '';
129
130
        foreach ($internalRequest->getDatas() as $name => $value) {
131
            $body .= $this->prepareRawBody($name, $value);
132
        }
133
134
        foreach ($internalRequest->getFiles() as $name => $file) {
135
            $body .= $this->prepareRawBody($name, $file, true);
136
        }
137
138
        $body .= '--'.$this->configuration->getBoundary().'--'."\r\n";
139
140
        return $body;
141
    }
142
143
    /**
144
     * @param string $name
145
     * @param string $subName
146
     *
147
     * @return string
148
     */
149
    protected function prepareName($name, $subName)
150
    {
151
        return $name.'['.$subName.']';
152
    }
153
154
    /**
155
     * @param string       $name
156
     * @param array|string $data
157
     * @param bool         $isFile
158
     *
159
     * @return string
160
     */
161
    private function prepareRawBody($name, $data, $isFile = false)
162
    {
163 View Code Duplication
        if (is_array($data)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
164
            $body = '';
165
166
            foreach ($data as $subName => $subData) {
167
                $body .= $this->prepareRawBody($this->prepareName($name, $subName), $subData, $isFile);
168
            }
169
170
            return $body;
171
        }
172
173
        $body = '--'.$this->configuration->getBoundary()."\r\n".'Content-Disposition: form-data; name="'.$name.'"';
174
175
        if ($isFile) {
176
            $body .= '; filename="'.basename($data).'"';
177
            $data = file_get_contents($data);
178
        }
179
180
        return $body."\r\n\r\n".$data."\r\n";
181
    }
182
}
183