AbstractHttpAdapter   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 163
Duplicated Lines 5.52 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 28
lcom 1
cbo 5
dl 9
loc 163
ccs 72
cts 76
cp 0.9474
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 16133
    public function __construct(ConfigurationInterface $configuration = null)
33
    {
34 16133
        $this->setConfiguration($configuration ?: new Configuration());
35 16133
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 15390
    public function getConfiguration()
41
    {
42 15390
        return $this->configuration;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 16133
    public function setConfiguration(ConfigurationInterface $configuration)
49
    {
50 16133
        $this->configuration = $configuration;
51 16133
    }
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 15345
    protected function prepareHeaders(
62
        InternalRequestInterface &$internalRequest,
63 7
        $associative = true,
64
        $contentType = true,
65
        $contentLength = false
66
    ) {
67 15345
        if (!$internalRequest->hasHeader('Connection')) {
68 15345
            $internalRequest = $internalRequest->withHeader(
69 15345
                'Connection',
70 15345
                $this->configuration->getKeepAlive() ? 'keep-alive' : 'close'
71 11823
            );
72 11823
        }
73
74 15345
        if (!$internalRequest->hasHeader('Content-Type')) {
75 15345
            $rawDatas = (string) $internalRequest->getBody();
76 15345
            $datas = $internalRequest->getDatas();
77 15345
            $files = $internalRequest->getFiles();
78
79 15345
            if ($this->configuration->hasEncodingType()) {
80
                $internalRequest = $internalRequest->withHeader(
81
                    'Content-Type',
82
                    $this->configuration->getEncodingType()
83
                );
84 15345
            } elseif ($contentType && !empty($files)) {
85 1750
                $internalRequest = $internalRequest->withHeader(
86 1750
                    'Content-Type',
87 1750
                    ConfigurationInterface::ENCODING_TYPE_FORMDATA.'; boundary='.$this->configuration->getBoundary()
88 1350
                );
89 14945
            } elseif ($contentType && (!empty($datas) || !empty($rawDatas))) {
90 3150
                $internalRequest = $internalRequest->withHeader(
91 3150
                    'Content-Type',
92 720
                    ConfigurationInterface::ENCODING_TYPE_URLENCODED
93 2430
                );
94 2430
            }
95 11823
        }
96
97 15345
        if ($contentLength && !$internalRequest->hasHeader('Content-Length')
98 15345
            && ($length = strlen($this->prepareBody($internalRequest))) > 0) {
99 504
            $internalRequest = $internalRequest->withHeader('Content-Length', (string) $length);
100 392
        }
101
102 15345
        if (!$internalRequest->hasHeader('User-Agent')) {
103 15345
            $internalRequest = $internalRequest->withHeader('User-Agent', $this->configuration->getUserAgent());
104 11823
        }
105
106 15345
        return HeadersNormalizer::normalize($internalRequest->getHeaders(), $associative);
107
    }
108
109
    /**
110
     * @param InternalRequestInterface $internalRequest
111
     *
112
     * @return string
113
     */
114 14737
    protected function prepareBody(InternalRequestInterface $internalRequest)
115
    {
116 14737
        $body = (string) $internalRequest->getBody();
117
118 14737
        if (!empty($body)) {
119 1281
            return $body;
120
        }
121
122 13822
        $files = $internalRequest->getFiles();
123
124 13822
        if (empty($files)) {
125 12392
            return http_build_query($internalRequest->getDatas(), null, '&');
126
        }
127
128 1430
        $body = '';
129
130 1430
        foreach ($internalRequest->getDatas() as $name => $value) {
131 1430
            $body .= $this->prepareRawBody($name, $value);
132 1090
        }
133
134 1430
        foreach ($internalRequest->getFiles() as $name => $file) {
135 1430
            $body .= $this->prepareRawBody($name, $file, true);
136 1090
        }
137
138 1430
        $body .= '--'.$this->configuration->getBoundary().'--'."\r\n";
139
140 1430
        return $body;
141
    }
142
143
    /**
144
     * @param string $name
145
     * @param string $subName
146
     *
147
     * @return string
148
     */
149 1830
    protected function prepareName($name, $subName)
150
    {
151 1830
        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 1430
    private function prepareRawBody($name, $data, $isFile = false)
162
    {
163 1430 View Code Duplication
        if (is_array($data)) {
0 ignored issues
show
Duplication introduced by Eric GELOEN
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 1430
            $body = '';
165
166 1430
            foreach ($data as $subName => $subData) {
167 1430
                $body .= $this->prepareRawBody($this->prepareName($name, $subName), $subData, $isFile);
168 1090
            }
169
170 1430
            return $body;
171
        }
172
173 1430
        $body = '--'.$this->configuration->getBoundary()."\r\n".'Content-Disposition: form-data; name="'.$name.'"';
174
175 1430
        if ($isFile) {
176 1430
            $body .= '; filename="'.basename($data).'"';
177 1430
            $data = file_get_contents($data);
178 1090
        }
179
180 1430
        return $body."\r\n\r\n".$data."\r\n";
181
    }
182
}
183