Issues (5)

src/CurlRequest.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * php-guard/curl <https://github.com/php-guard/curl>
4
 * Copyright (C) ${YEAR} by Alexandre Le Borgne <[email protected]>.
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
namespace PhpGuard\Curl;
21
22
use PhpGuard\Curl\Collection\CurlOptions;
23
use PhpGuard\Curl\Collection\Headers;
24
25
class CurlRequest
26
{
27
    /**
28
     * @var string
29
     */
30
    protected $url;
31
    /**
32
     * @var string
33
     */
34
    protected $method = 'GET';
35
    /**
36
     * @var mixed
37
     */
38
    protected $data;
39
    /**
40
     * @var Headers
41
     */
42
    protected $headers;
43
    /**
44
     * @var Curl
45
     */
46
    private $curl;
47
    /**
48
     * @var CurlOptions
49
     */
50
    private $curlOptions;
51
52
    /**
53
     * CurlRequest constructor.
54
     *
55
     * @param Curl       $curl
56
     * @param string     $url
57
     * @param string     $method
58
     * @param array|null $data
59
     * @param array      $headers
60
     * @param array      $curlOptions
61
     */
62 45
    public function __construct(Curl $curl, string $url, string $method = 'GET', $data = null, array $headers = [],
63
                                array $curlOptions = [])
64
    {
65 45
        $this->curl = $curl;
66 45
        $this->url = $url;
67 45
        $this->method = $method;
68 45
        $this->data = $data;
69 45
        $this->headers = new Headers($headers);
70 45
        $this->curlOptions = new CurlOptions($curlOptions);
71 45
    }
72
73
    /**
74
     * @return string
75
     */
76 45
    public function getUrl(): string
77
    {
78 45
        return $this->url;
79
    }
80
81
    /**
82
     * @param string $url
83
     */
84
    public function setUrl(string $url): void
85
    {
86
        $this->url = $url;
87
    }
88
89
    /**
90
     * @return string
91
     */
92 45
    public function getMethod(): string
93
    {
94 45
        return $this->method;
95
    }
96
97
    /**
98
     * @param string $method
99
     */
100
    public function setMethod(string $method): void
101
    {
102
        $this->method = $method;
103
    }
104
105
    /**
106
     * @return mixed
107
     */
108 45
    public function getData()
109
    {
110 45
        return $this->data;
111
    }
112
113
    /**
114
     * @param mixed $data
115
     */
116 18
    public function setData($data): void
117
    {
118 18
        $this->data = $data;
119 18
    }
120
121
    /**
122
     * @return Headers
123
     */
124 45
    public function getHeaders(): Headers
125
    {
126 45
        return $this->headers;
127
    }
128
129
    public function setHeaderContentType(string $contentType)
130
    {
131
        $this->headers['Content-Type'] = $contentType;
132
    }
133
134
    /**
135
     * @return CurlOptions
136
     */
137 45
    public function getCurlOptions(): CurlOptions
138
    {
139 45
        return $this->curlOptions;
140
    }
141
142
    /**
143
     * @param bool $throwExceptionOnHttpError
144
     *
145
     * @return CurlResponse
146
     *
147
     * @throws CurlError
148
     */
149 42
    public function execute(bool $throwExceptionOnHttpError = false): CurlResponse
150
    {
151 42
        $response = $this->curl->execute($this);
152
153 42
        if ($throwExceptionOnHttpError && $response->isError()) {
154
            throw new CurlError($response->raw(), $response->statusCode());
155
        }
156
157 42
        return $response;
158
    }
159
160 45
    public function resource()
161
    {
162
        // create curl resource
163 45
        $ch = curl_init();
164
165 45
        curl_setopt($ch, CURLOPT_URL, $this->getUrl());
0 ignored issues
show
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt() 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

165
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_URL, $this->getUrl());
Loading history...
166 45
        curl_setopt($ch, CURLOPT_POSTFIELDS, $this->getData());
167 45
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->getMethod());
168 45
        curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getHeaders()->toHttp());
169
170 45
        foreach ($this->getCurlOptions()->all() as $key => $value) {
171 45
            curl_setopt($ch, $key, $value);
172
        }
173
174 45
        return $ch;
175
    }
176
}
177