Completed
Pull Request — master (#129)
by Eric
257:01 queued 192:06
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 8328
    public function __construct(ConfigurationInterface $configuration = null, $checkExtension = true)
32
    {
33 8328
        if ($checkExtension && !function_exists('curl_init')) {
34 4
            throw HttpAdapterException::extensionIsNotLoaded('curl', $this->getName());
35
        }
36
37 8324
        parent::__construct($configuration);
38 8324
    }
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 3024
    protected function prepareProtocolVersion(InternalRequestInterface $internalRequest)
48
    {
49 3024
        return $internalRequest->getProtocolVersion() === InternalRequestInterface::PROTOCOL_VERSION_1_0
50 2858
            ? CURL_HTTP_VERSION_1_0
51 3024
            : 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 5916
    protected function prepareContent(InternalRequestInterface $internalRequest)
62
    {
63 5916
        $files = $internalRequest->getFiles();
64
65 5916
        if (empty($files)) {
66 5156
            return $this->prepareBody($internalRequest);
67
        }
68
69 760
        $content = array();
70
71 760
        foreach ($internalRequest->getDatas() as $name => $data) {
72 760
            $content = array_merge($content, $this->prepareRawContent($name, $data));
73 720
        }
74
75 760
        foreach ($files as $name => $file) {
76 760
            $content = array_merge($content, $this->prepareRawContent($name, $file, true));
77 720
        }
78
79 760
        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 540
    protected function createFile($file)
90
    {
91 540
        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 540
    protected function isSafeUpload()
100
    {
101 540
        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 5646
    private function prepareRawContent($name, $data, $isFile = false)
114
    {
115 760 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 760
            $preparedData = array();
117
118 760
            foreach ($data as $subName => $subData) {
119 760
                $preparedData = array_merge(
120 720
                    $preparedData,
121 760
                    $this->prepareRawContent($this->prepareName($name, $subName), $subData, $isFile)
122 5606
                );
123 720
            }
124
125 760
            return $preparedData;
126
        }
127
128 760
        return array($name => $isFile ? $this->createFile($data) : $data);
129
    }
130
}
131