Test Setup Failed
Push — master ( b82f41...795523 )
by Php Easy Api
04:16
created

ClientHttpManager::resolve()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 14
nc 6
nop 0
dl 0
loc 29
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Resta\Client;
4
5
use Resta\Contracts\ApplicationContracts;
6
use Resta\Foundation\ApplicationProvider;
7
8
class ClientHttpManager extends ApplicationProvider
9
{
10
    /**
11
     * @var string $method
12
     */
13
    protected $method;
14
15
    /**
16
     * @var object
17
     */
18
    protected $client;
19
20
    /**
21
     * ClientHttpManager constructor.
22
     * @param ApplicationContracts $app
23
     * @param object $client
24
     */
25
    public function __construct(ApplicationContracts $app, $client)
26
    {
27
        parent::__construct($app);
28
29
        $this->client = $client;
30
31
        $this->method = httpMethod();
32
    }
33
34
    /**
35
     * http data resolve
36
     *
37
     * @return mixed
38
     */
39
    public function resolve()
40
    {
41
        $inputs = $this->app->get($this->method);
42
43
        $content = json_decode($this->app['request']->getContent(),1);
44
45
        if(is_array($inputs) && count($inputs)){
46
47
            if(isset($inputs[$this->client->getClientName()])){
48
                return $inputs[$this->client->getClientName()];
49
            }
50
            return $inputs;
51
        }
52
53
        if(is_array($content)){
54
55
            if(isset($content[$this->client->getClientName()])){
56
                return $content[$this->client->getClientName()];
57
            }
58
            return $content;
59
        }
60
61
        $this->inputVars();
62
63
        if(is_array($GLOBALS[ '_PUT' ])){
64
            return $GLOBALS[ '_PUT' ];
65
        }
66
67
        return [];
68
    }
69
70
    /**
71
     * @return void|mixed
72
     */
73
    private function inputVars()
74
    {
75
        global $_PUT;
76
77
        /* PUT data comes in on the stdin stream */
78
        $putdata = fopen("php://input", "r");
79
80
        /* Open a file for writing */
81
        // $fp = fopen("myputfile.ext", "w");
82
83
        $raw_data = '';
84
85
        /* Read the data 1 KB at a time
86
           and write to the file */
87
        while ($chunk = fread($putdata, 1024))
0 ignored issues
show
Bug introduced by
It seems like $putdata can also be of type false; however, parameter $handle of fread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
        while ($chunk = fread(/** @scrutinizer ignore-type */ $putdata, 1024))
Loading history...
88
            $raw_data .= $chunk;
89
90
        /* Close the streams */
91
        fclose($putdata);
0 ignored issues
show
Bug introduced by
It seems like $putdata can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
        fclose(/** @scrutinizer ignore-type */ $putdata);
Loading history...
92
93
        // Fetch content and determine boundary
94
        $boundary = substr($raw_data, 0, strpos($raw_data, "\r\n"));
95
96
        if(empty($boundary)){
97
            parse_str($raw_data,$data);
98
            $GLOBALS[ '_PUT' ] = $data;
99
            return;
100
        }
101
102
        // Fetch each part
103
        $parts = array_slice(explode($boundary, $raw_data), 1);
104
        $data = array();
105
106
        foreach ($parts as $part) {
107
            // If this is the last part, break
108
            if ($part == "--\r\n") break;
109
110
            // Separate content from headers
111
            $part = ltrim($part, "\r\n");
112
            list($raw_headers, $body) = explode("\r\n\r\n", $part, 2);
113
114
            // Parse the headers list
115
            $raw_headers = explode("\r\n", $raw_headers);
116
            $headers = array();
117
            foreach ($raw_headers as $header) {
118
                list($name, $value) = explode(':', $header);
119
                $headers[strtolower($name)] = ltrim($value, ' ');
120
            }
121
122
            // Parse the Content-Disposition to get the field name, etc.
123
            if (isset($headers['content-disposition'])) {
124
                $filename = null;
125
                $tmp_name = null;
126
                preg_match(
127
                    '/^(.+); *name="([^"]+)"(; *filename="([^"]+)")?/',
128
                    $headers['content-disposition'],
129
                    $matches
130
                );
131
                list(, $type, $name) = $matches;
132
133
                //Parse File
134
                if( isset($matches[4]) )
135
                {
136
                    //if labeled the same as previous, skip
137
                    if( isset( $_FILES[ $matches[ 2 ] ] ) )
138
                    {
139
                        continue;
140
                    }
141
142
                    //get filename
143
                    $filename = $matches[4];
144
145
                    //get tmp name
146
                    $filename_parts = pathinfo( $filename );
147
                    $tmp_name = tempnam( ini_get('upload_tmp_dir'), $filename_parts['filename']);
148
149
                    //populate $_FILES with information, size may be off in multibyte situation
150
                    $_FILES[ $matches[ 2 ] ] = array(
151
                        'error'=>0,
152
                        'name'=>$filename,
153
                        'tmp_name'=>$tmp_name,
154
                        'size'=>strlen( $body ),
155
                        'type'=>$value
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $value does not seem to be defined for all execution paths leading up to this point.
Loading history...
156
                    );
157
158
                    //place in temporary directory
159
                    file_put_contents($tmp_name, $body);
160
                }
161
                //Parse Field
162
                else
163
                {
164
                    $data[$name] = substr($body, 0, strlen($body) - 2);
165
                }
166
            }
167
168
        }
169
        $GLOBALS[ '_PUT' ] = $data;
170
        return;
171
    }
172
173
    /**
174
     * get http method
175
     *
176
     * @return mixed
177
     */
178
    public function getMethod()
179
    {
180
        return $this->method;
181
    }
182
183
}