Completed
Push — master ( 040a1a...68f55d )
by Julián
02:10
created

AbstractTransport::head()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
/**
3
 * Spiral: PSR7 aware cURL client (https://github.com/juliangut/spiral)
4
 *
5
 * @link https://github.com/juliangut/spiral for the canonical source repository
6
 * @license https://raw.githubusercontent.com/juliangut/spiral/master/LICENSE
7
 */
8
9
namespace Jgut\Spiral\Transport;
10
11
use Jgut\Spiral\Exception\OptionException;
12
use Jgut\Spiral\Option;
13
use Jgut\Spiral\Option\OptionFactory;
14
use Jgut\Spiral\Transport as TransportInterface;
15
16
/**
17
 * Common transport trait.
18
 */
19
abstract class AbstractTransport implements TransportInterface
20
{
21
    /**
22
     * List of cURL options.
23
     *
24
     * @var \Jgut\Spiral\Option[]
25
     */
26
    protected $options = [];
27
28
    /**
29
     * Retrieve added cURL options.
30
     *
31
     * @return \Jgut\Spiral\Option[]
32
     */
33
    public function getOptions()
34
    {
35
        return $this->options;
36
    }
37
38
    /**
39
     * Set cURL options.
40
     *
41
     * @param array $options
42
     */
43
    public function setOptions(array $options)
44
    {
45
        foreach ($options as $name => $value) {
46
            $this->setOption($name, $value);
47
        }
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function setOption($option, $value = '', $quiet = false)
54
    {
55
        if (!$option instanceof Option) {
56
            try {
57
                $option = OptionFactory::build($option, $value);
58
            } catch (OptionException $exception) {
59
                if ($quiet !== true) {
60
                    throw $exception;
61
                }
62
            }
63
        }
64
65
        $this->removeOption($option->getOption());
66
        $this->options[] = $option;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function hasOption($option, $value = null)
73
    {
74 View Code Duplication
        if ($option instanceof Option) {
0 ignored issues
show
Duplication introduced by
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...
75
            $option = $option->getOption();
76
        } else {
77
            try {
78
                $option = OptionFactory::getOptionKey($option);
79
            } catch (OptionException $exception) {
80
                return false;
81
            }
82
        }
83
84
        foreach ($this->options as $transportOption) {
85
            if ($transportOption->getOption() === $option) {
86
                return $value === null ?: $transportOption->getValue() === $value;
87
            }
88
        }
89
90
        return false;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function removeOption($option)
97
    {
98 View Code Duplication
        if ($option instanceof Option) {
0 ignored issues
show
Duplication introduced by
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...
99
            $option = $option->getOption();
100
        } else {
101
            try {
102
                $option = OptionFactory::getOptionKey($option);
103
            } catch (OptionException $exception) {
104
                return;
105
            }
106
        }
107
108
        $this->options = array_filter(
109
            $this->options,
110
            function ($transportOption) use ($option) {
111
                /** @var \Jgut\Spiral\Option $transportOption */
112
                return !($transportOption->getOption() === $option);
113
            }
114
        );
115
    }
116
117
    /**
118
     * Shorthand for OPTIONS cURL request.
119
     *
120
     * @param string $uri
121
     * @param array  $headers
122
     * @param array  $vars
123
     *
124
     * @return string
125
     */
126
    public function options($uri, array $headers = [], array $vars = [])
127
    {
128
        return $this->request(TransportInterface::METHOD_OPTIONS, $uri, $headers, $vars);
129
    }
130
131
    /**
132
     * Shorthand for HEAD cURL request.
133
     *
134
     * @param string $uri
135
     * @param array  $headers
136
     * @param array  $vars
137
     *
138
     * @return string
139
     */
140
    public function head($uri, array $headers = [], array $vars = [])
141
    {
142
        return $this->request(TransportInterface::METHOD_HEAD, $uri, $headers, $vars);
143
    }
144
145
    /**
146
     * Shorthand for GET cURL request.
147
     *
148
     * @param string $uri
149
     * @param array  $headers
150
     * @param array  $vars
151
     *
152
     * @return string
153
     */
154
    public function get($uri, array $headers = [], array $vars = [])
155
    {
156
        return $this->request(TransportInterface::METHOD_GET, $uri, $headers, $vars);
157
    }
158
159
    /**
160
     * Shorthand for POST cURL request.
161
     *
162
     * @param string $uri
163
     * @param array  $headers
164
     * @param array  $vars
165
     * @param array  $flags
166
     *
167
     * @return string
168
     */
169
    public function post($uri, array $headers = [], array $vars = [], array $flags = [])
170
    {
171
        return $this->request(TransportInterface::METHOD_POST, $uri, $headers, $vars, $flags);
172
    }
173
174
    /**
175
     * Shorthand for PUT cURL request.
176
     *
177
     * @param string $uri
178
     * @param array  $headers
179
     * @param array  $vars
180
     *
181
     * @return string
182
     */
183
    public function put($uri, array $headers = [], array $vars = [])
184
    {
185
        return $this->request(TransportInterface::METHOD_PUT, $uri, $headers, $vars);
186
    }
187
188
    /**
189
     * Shorthand for DELETE cURL request.
190
     *
191
     * @param string $uri
192
     * @param array  $headers
193
     * @param array  $vars
194
     *
195
     * @return string
196
     */
197
    public function delete($uri, array $headers = [], array $vars = [])
198
    {
199
        return $this->request(TransportInterface::METHOD_DELETE, $uri, $headers, $vars);
200
    }
201
202
    /**
203
     * Shorthand for PATCH cURL request.
204
     *
205
     * @param string $uri
206
     * @param array  $headers
207
     * @param array  $vars
208
     *
209
     * @return string
210
     */
211
    public function patch($uri, array $headers = [], array $vars = [])
212
    {
213
        return $this->request(TransportInterface::METHOD_PATCH, $uri, $headers, $vars);
214
    }
215
}
216