Passed
Push — master ( d5d5d5...7b081d )
by Alexandre
04:49
created

Curl::getRequestModifierPipeline()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\RequestModifier\FileRequestModifier;
23
use PhpGuard\Curl\RequestModifier\PlainTextRequestModifier;
24
25
class Curl
26
{
27
    private $requestModifierPipeline;
28
    private $curlResponseFactory;
29
    private $curlRequestFactory;
30
31
    public function __construct(?string $host = null)
32
    {
33
        $this->requestModifierPipeline = new RequestModifierPipeline();
34
        $this->curlResponseFactory = new CurlResponseFactory();
35
        $this->curlRequestFactory = new CurlRequestFactory($this, $host);
36
37
        $this->requestModifierPipeline
38
            ->pipe(new FileRequestModifier())
39
            ->pipe(new PlainTextRequestModifier());
40
    }
41
42
    /**
43
     * @param string $url
44
     * @param null   $query
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $query is correct as it would always require null to be passed?
Loading history...
45
     * @param array  $headers
46
     *
47
     * @return CurlResponse
48
     *
49
     * @throws CurlError
50
     */
51 15
    public function get(string $url, $query = null, array $headers = []): CurlResponse
52
    {
53 15
        return $this->curlRequestFactory->create('GET', $url, null, $query, $headers)->execute();
54
    }
55
56
    /**
57
     * @param string $url
58
     * @param null   $data
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $data is correct as it would always require null to be passed?
Loading history...
59
     * @param null   $query
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $query is correct as it would always require null to be passed?
Loading history...
60
     * @param array  $headers
61
     *
62
     * @return CurlResponse
63
     *
64
     * @throws CurlError
65
     */
66 15
    public function post(string $url, $data = null, $query = null, array $headers = [])
67
    {
68 15
        return $this->curlRequestFactory->create('POST', $url, $data, $query, $headers)->execute();
69
    }
70
71
    /**
72
     * @param string $url
73
     * @param null   $data
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $data is correct as it would always require null to be passed?
Loading history...
74
     * @param null   $query
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $query is correct as it would always require null to be passed?
Loading history...
75
     * @param array  $headers
76
     *
77
     * @return CurlResponse
78
     *
79
     * @throws CurlError
80
     */
81 3
    public function put(string $url, $data = null, $query = null, array $headers = [])
82
    {
83 3
        return $this->curlRequestFactory->create('PUT', $url, $data, $query, $headers)->execute();
84
    }
85
86
    /**
87
     * @param string $url
88
     * @param null   $data
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $data is correct as it would always require null to be passed?
Loading history...
89
     * @param null   $query
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $query is correct as it would always require null to be passed?
Loading history...
90
     * @param array  $headers
91
     *
92
     * @return CurlResponse
93
     *
94
     * @throws CurlError
95
     */
96 3
    public function patch(string $url, $data = null, $query = null, array $headers = [])
97
    {
98 3
        return $this->curlRequestFactory->create('PATCH', $url, $data, $query, $headers)->execute();
99
    }
100
101
    /**
102
     * @param string $url
103
     * @param null   $data
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $data is correct as it would always require null to be passed?
Loading history...
104
     * @param null   $query
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $query is correct as it would always require null to be passed?
Loading history...
105
     * @param array  $headers
106
     *
107
     * @return CurlResponse
108
     *
109
     * @throws CurlError
110
     */
111 3
    public function delete(string $url, $data = null, $query = null, array $headers = [])
112
    {
113 3
        return $this->curlRequestFactory->create('DELETE', $url, $data, $query, $headers)->execute();
114
    }
115
116
    /**
117
     * @param array $options Définit les options pour le gestionnaire multiple cURL
118
     *
119
     * @return MultiCurl
120
     */
121 3
    public function multi(array $options = [])
122
    {
123 3
        return new MultiCurl($this, $options);
124
    }
125
126
    /**
127
     * @param CurlRequest $request
128
     *
129
     * @return CurlResponse
130
     *
131
     * @throws CurlError
132
     */
133 39
    public function execute(CurlRequest $request): CurlResponse
134
    {
135 39
        $ch = $this->requestModifierPipeline->process($request)->resource();
136
137
        try {
138 39
            return $this->curlResponseFactory->create($ch, curl_exec($ch));
139
        } finally {
140 39
            curl_close($ch);
141
        }
142
    }
143
144
    /**
145
     * @return RequestModifierPipeline
146
     */
147 3
    public function getRequestModifierPipeline(): RequestModifierPipeline
148
    {
149 3
        return $this->requestModifierPipeline;
150
    }
151
152
    /**
153
     * @return CurlRequestFactory
154
     */
155 6
    public function getCurlRequestFactory(): CurlRequestFactory
156
    {
157 6
        return $this->curlRequestFactory;
158
    }
159
160
    /**
161
     * @return CurlResponseFactory
162
     */
163 3
    public function getCurlResponseFactory(): CurlResponseFactory
164
    {
165 3
        return $this->curlResponseFactory;
166
    }
167
}
168