Completed
Push — travis ( f0ed02...4279e5 )
by Eric
192:27 queued 127:20
created

src/AbstractCurlHttpAdapter.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
 * Abstract curl http adapter.
18
 *
19
 * @author GeLo <[email protected]>
20
 */
21
abstract class AbstractCurlHttpAdapter extends AbstractHttpAdapter
22
{
23
    /**
24
     * Creates a curl http adapter.
25
     *
26
     * @param \Ivory\HttpAdapter\ConfigurationInterface|null $configuration  The configuration.
27
     * @param boolean                                        $checkExtension TRUE if the extension should be checked else FALSE.
28
     *
29
     * @throws \Ivory\HttpAdapter\HttpAdapterException If the check extension is enabled and the curl extension is not loaded.
30
     */
31
    public function __construct(ConfigurationInterface $configuration = null, $checkExtension = true)
32
    {
33
        if ($checkExtension && !function_exists('curl_init')) {
34
            throw HttpAdapterException::extensionIsNotLoaded('curl', $this->getName());
35
        }
36
37
        parent::__construct($configuration);
38
    }
39
40
    /**
41
     * Prepares the protocol version.
42
     *
43
     * @param \Ivory\HttpAdapter\Message\InternalRequestInterface $internalRequest The internal request.
44
     *
45
     * @return integer The prepared protocol version.
46
     */
47
    protected function prepareProtocolVersion(InternalRequestInterface $internalRequest)
48
    {
49
        return $internalRequest->getProtocolVersion() === InternalRequestInterface::PROTOCOL_VERSION_1_0
50
            ? CURL_HTTP_VERSION_1_0
51
            : CURL_HTTP_VERSION_1_1;
52
    }
53
54
    /**
55
     * Prepares the content.
56
     *
57
     * @param \Ivory\HttpAdapter\Message\InternalRequestInterface $internalRequest The internal request.
58
     *
59
     * @return array|string The prepared content.
60
     */
61
    protected function prepareContent(InternalRequestInterface $internalRequest)
62
    {
63
        $files = $internalRequest->getFiles();
64
65
        if (empty($files)) {
66
            return $this->prepareBody($internalRequest);
67
        }
68
69
        $content = array();
70
71
        foreach ($internalRequest->getDatas() as $name => $data) {
72
            $content = array_merge($content, $this->prepareRawContent($name, $data));
73
        }
74
75
        foreach ($files as $name => $file) {
76
            $content = array_merge($content, $this->prepareRawContent($name, $file, true));
77
        }
78
79
        return $content;
80
    }
81
82
    /**
83
     * Creates a file.
84
     *
85
     * @param string $file The file.
86
     *
87
     * @return mixed The created file.
88
     */
89
    protected function createFile($file)
90
    {
91
        return $this->isSafeUpload() ? new \CurlFile($file) : '@'.$file;
92
    }
93
94
    /**
95
     * Checks if it is safe upload.
96
     *
97
     * @return boolean TRUE if it is safe upload else FALSE.
98
     */
99
    protected function isSafeUpload()
100
    {
101
        return defined('CURLOPT_SAFE_UPLOAD');
102
    }
103
104
    /**
105
     * Prepares the raw content.
106
     *
107
     * @param string       $name   The name.
108
     * @param array|string $data   The data.
109
     * @param boolean      $isFile TRUE if the data is a file path else FALSE.
110
     *
111
     * @return array The prepared raw content.
112
     */
113
    private function prepareRawContent($name, $data, $isFile = false)
114
    {
115 View Code Duplication
        if (is_array($data)) {
0 ignored issues
show
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...
116
            $preparedData = array();
117
118
            foreach ($data as $subName => $subData) {
119
                $preparedData = array_merge(
120
                    $preparedData,
121
                    $this->prepareRawContent($this->prepareName($name, $subName), $subData, $isFile)
122
                );
123
            }
124
125
            return $preparedData;
126
        }
127
128
        return array($name => $isFile ? $this->createFile($data) : $data);
129
    }
130
}
131