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

AbstractCurlHttpAdapter::isSafeUpload()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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 86
    public function __construct(ConfigurationInterface $configuration = null, $checkExtension = true)
28
    {
29 86
        if ($checkExtension && !function_exists('curl_init')) {
30 4
            throw HttpAdapterException::extensionIsNotLoaded('curl', $this->getName());
31
        }
32
33 82
        parent::__construct($configuration);
34 82
    }
35
36
    /**
37
     * @param InternalRequestInterface $internalRequest
38
     *
39
     * @return int
40
     */
41
    protected function prepareProtocolVersion(InternalRequestInterface $internalRequest)
42
    {
43
        return $internalRequest->getProtocolVersion() === InternalRequestInterface::PROTOCOL_VERSION_1_0
44
            ? CURL_HTTP_VERSION_1_0
45
            : CURL_HTTP_VERSION_1_1;
46
    }
47
48
    /**
49
     * @param InternalRequestInterface $internalRequest
50
     *
51
     * @return array|string
52
     */
53
    protected function prepareContent(InternalRequestInterface $internalRequest)
54
    {
55
        $files = $internalRequest->getFiles();
56
57
        if (empty($files)) {
58
            return $this->prepareBody($internalRequest);
59
        }
60
61
        $content = [];
62
63
        foreach ($internalRequest->getDatas() as $name => $data) {
64
            $content = array_merge($content, $this->prepareRawContent($name, $data));
65
        }
66
67
        foreach ($files as $name => $file) {
68
            $content = array_merge($content, $this->prepareRawContent($name, $file, true));
69
        }
70
71
        return $content;
72
    }
73
74
    /**
75
     * @param string $file
76
     *
77
     * @return mixed
78
     */
79
    protected function createFile($file)
80
    {
81
        return $this->isSafeUpload() ? new \CurlFile($file) : '@'.$file;
82
    }
83
84
    /**
85
     * @return bool
86
     */
87
    protected function isSafeUpload()
88
    {
89
        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
    private function prepareRawContent($name, $data, $isFile = false)
100
    {
101 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...
102
            $preparedData = [];
103
104
            foreach ($data as $subName => $subData) {
105
                $preparedData = array_merge(
106
                    $preparedData,
107
                    $this->prepareRawContent($this->prepareName($name, $subName), $subData, $isFile)
108
                );
109
            }
110
111
            return $preparedData;
112
        }
113
114
        return [$name => $isFile ? $this->createFile($data) : $data];
115
    }
116
}
117