Completed
Push — master ( 7ecef5...dbbb06 )
by arto
03:42
created

Builder::withTheRawOption()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4286
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2015-12-08
5
 */
6
namespace Net\Bazzline\Component\Curl;
7
8
use Exception;
9
use Net\Bazzline\Component\Curl\HeadLine\HeadLineInterface;
10
use Net\Bazzline\Component\Curl\HeadLine\ContentTypeIsJson;
11
use Net\Bazzline\Component\Curl\Option\OptionInterface;
12
use Net\Bazzline\Component\Curl\ResponseBehaviour\ConvertJsonToArrayBehaviour;
13
use Net\Bazzline\Component\Curl\ResponseBehaviour\ResponseBehaviourInterface;
14
use Net\Bazzline\Component\Toolbox\HashMap\Merge;
15
use RuntimeException;
16
17
class Builder
18
{
19
    const METHOD_DELETE = 0;
20
    const METHOD_GET    = 1;
21
    const METHOD_PATCH  = 2;
22
    const METHOD_POST   = 3;
23
    const METHOD_PUT    = 4;
24
25
    /** @var boolean */
26
    private $asJson;
27
28
    /** @var null|string|array */
29
    private $data;
30
31
    /** @var array|ResponseBehaviourInterface[] */
32
    private $defaultResponseBehaviours;
33
34
    /** @var Merge */
35
    private $merge;
36
37
    /** @var int */
38
    private $method;
39
40
    /** @var array */
41
    private $parameters;
42
43
    /** @var Request */
44
    private $request;
45
46
    /** @var array|ResponseBehaviourInterface[] */
47
    private $responseBehaviours;
48
49
    /** @var string */
50
    private $url;
51
52
    /**
53
     * @param Request $request
54
     * @param Merge $merge
55
     * @param array|ResponseBehaviourInterface[] $defaultResponseBehaviours
56
     */
57
    public function __construct(Request $request, Merge $merge, array $defaultResponseBehaviours = array())
58
    {
59
        $this->defaultResponseBehaviours    = $defaultResponseBehaviours;
60
        $this->merge                        = $merge;
61
        $this->request                      = $request;
62
        $this->reset();
63
    }
64
65
    /**
66
     * @return Response
67
     * @throws Exception|RuntimeException
68
     */
69
    public function andFetchTheResponse()
70
    {
71
        $asJson     = $this->asJson;
72
        $merge      = $this->merge;
73
        /** @var ResponseBehaviourInterface[] $behaviours */
74
        $behaviours = $merge($this->responseBehaviours, $this->defaultResponseBehaviours);
75
        $data       = $this->data;
76
        $method     = $this->method;
77
        $parameters = $this->parameters;
78
        $request    = $this->request;
79
        $url        = $this->url;
80
81
        if ($asJson) {
82
            $data = json_encode($data);
83
        }
84
85
        switch ($method) {
86
            case self::METHOD_DELETE:
87
                $response = $request->delete($url, $parameters);
88
                break;
89
            case self::METHOD_GET:
90
                $response = $request->get($url, $parameters);
91
                break;
92
            case self::METHOD_PATCH:
93
                $response = $request->patch($url, $parameters, $data);
94
                break;
95
            case self::METHOD_POST:
96
                $response = $request->post($url, $parameters, $data);
97
                break;
98
            case self::METHOD_PUT:
99
                $response = $request->put($url, $parameters, $data);
100
                break;
101
            default:
102
                throw new RuntimeException(
103
                    'no http method set'
104
                );
105
        }
106
107
        foreach ($behaviours as $behaviour) {
108
            $response = $behaviour->behave($response);
109
        }
110
111
        return $response;
112
    }
113
114
    /**
115
     * @return $this
116
     */
117
    public function asJson()
118
    {
119
        $this->asJson = true;
120
        $this->request->addHeaderLine(new ContentTypeIsJson());
121
        $this->responseBehaviours[] = new ConvertJsonToArrayBehaviour();
122
123
        return $this;
124
    }
125
126
    /**
127
     * @param bool $alsoTheDefaults
128
     * @return $this
129
     */
130
    public function reset($alsoTheDefaults = false)
131
    {
132
        $this->asJson               = false;
133
        $this->data                 = null;
134
        $this->parameters           = array();
135
        $this->request->reset($alsoTheDefaults);
136
        $this->responseBehaviours   = array();
137
        $this->url                  = null;
138
139
        return $this;
140
    }
141
142
    /**
143
     * @param string $url
144
     * @return $this
145
     */
146
    public function onTheUrl($url)
147
    {
148
        $this->url = $url;
149
150
        return $this;
151
    }
152
153
    /**
154
     * @param null|string|array $data
155
     * @return $this
156
     */
157
    public function withTheData($data)
158
    {
159
        $this->data = $data;
160
161
        return $this;
162
    }
163
164
    /**
165
     * @param HeadLineInterface $line
166
     * @return $this
167
     */
168
    public function withTheHeaderLine(HeadLineInterface $line)
169
    {
170
        $this->request->addHeaderLine($line);
171
172
        return $this;
173
    }
174
175
    /**
176
     * @param OptionInterface $option
177
     * @return $this
178
     */
179
    public function withTheOption(OptionInterface $option)
180
    {
181
        $this->request->addOption($option);
182
183
        return $this;
184
    }
185
186
    /**
187
     * @param array $parameters
188
     * @return $this
189
     */
190
    public function withTheParameters(array $parameters)
191
    {
192
        $this->parameters = $parameters;
193
194
        return $this;
195
    }
196
197
    /**
198
     * @param string $line
199
     * @return $this
200
     */
201
    public function withTheRawHeaderLine($line)
202
    {
203
        $this->request->addRawHeaderLine($line);
204
205
        return $this;
206
    }
207
208
    /**
209
     * @param string $key
210
     * @param string $value
211
     * @return $this
212
     */
213
    public function withTheRawOption($key, $value)
214
    {
215
        $this->request->addRawOption($key, $value);
216
217
        return $this;
218
    }
219
220
    /**
221
     * @param ResponseBehaviourInterface $behaviour
222
     * @return $this
223
     */
224
    public function withTheResponseBehaviour(ResponseBehaviourInterface $behaviour)
225
    {
226
        $this->responseBehaviours[] = $behaviour;
227
228
        return $this;
229
    }
230
231
    //@todo better nameing?
232
    //  callDelete
233
    /**
234
     * @return $this
235
     */
236
    public function useDelete()
237
    {
238
        $this->method = self::METHOD_DELETE;
239
240
        return $this;
241
    }
242
243
    /**
244
     * @return $this
245
     */
246
    public function useGet()
247
    {
248
        $this->method = self::METHOD_GET;
249
250
        return $this;
251
    }
252
253
    /**
254
     * @return $this
255
     */
256
    public function usePatch()
257
    {
258
        $this->method = self::METHOD_PATCH;
259
260
        return $this;
261
    }
262
263
    /**
264
     * @return $this
265
     */
266
    public function usePost()
267
    {
268
        $this->method = self::METHOD_POST;
269
270
        return $this;
271
    }
272
273
    /**
274
     * @return $this
275
     */
276
    public function usePut()
277
    {
278
        $this->method = self::METHOD_PUT;
279
280
        return $this;
281
    }
282
}
283