Completed
Push — master ( 50f7e2...ff7682 )
by arto
02:34
created

RequestFactory::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace Net\Bazzline\Component\Curl\Request;
4
5
use Net\Bazzline\Component\Curl\Dispatcher\Dispatcher;
6
use Net\Bazzline\Component\Curl\Dispatcher\DispatcherInterface;
7
use Net\Bazzline\Component\Curl\FactoryInterface;
8
use Net\Bazzline\Component\Curl\HeaderLine\ContentTypeIsFormUtf8;
9
use Net\Bazzline\Component\Curl\HeaderLine\HeaderLineInterface;
10
use Net\Bazzline\Component\Curl\Option\Behaviour\SetTimeOutInSeconds;
11
use Net\Bazzline\Component\Curl\Option\OptionInterface;
12
use Net\Bazzline\Component\Toolbox\HashMap\Merge;
13
14
class RequestFactory implements FactoryInterface
15
{
16
    /** @var DispatcherInterface */
17
    private $dispatcher;
18
19
    /**
20
     * @return mixed|Request
21
     */
22
    public function create()
23
    {
24
        $request = new Request($this->getDispatcher(), new Merge());
25
26
        $request = $this->addDefaultHeaderLines($this->getDefaultHeaderLines(), $request);
27
        $request = $this->addDefaultRawHeaderLines($this->getDefaultRawHeaderLine(), $request);
28
        $request = $this->addDefaultOptions($this->getDefaultOptions(), $request);
29
        $request = $this->addDefaultRawOptions($this->getDefaultRawOptions(), $request);
30
31
        return $request;
32
    }
33
34
    /**
35
     * @param DispatcherInterface $dispatcher
36
     */
37
    public function overwriteDispatcher(DispatcherInterface $dispatcher)
38
    {
39
        $this->dispatcher = $dispatcher;
40
    }
41
42
    /**
43
     * @return DispatcherInterface
44
     */
45
    protected function getDispatcher()
46
    {
47
        $createDispatcher = !($this->dispatcher instanceof  DispatcherInterface);
48
49
        if ($createDispatcher) {
50
            $this->dispatcher = $this->getNewDispatcher();
51
        }
52
53
        return $this->dispatcher;
54
    }
55
56
    /**
57
     * @return DispatcherInterface
58
     */
59
    protected function getNewDispatcher()
60
    {
61
        return new Dispatcher();
62
    }
63
64
    /**
65
     * @return array|HeaderLineInterface[]
66
     */
67
    protected function getDefaultHeaderLines()
68
    {
69
        return array(
70
            new ContentTypeIsFormUtf8()
71
        );
72
    }
73
74
    /**
75
     * @return array
76
     */
77
    protected function getDefaultRawHeaderLine()
78
    {
79
        return array();
80
    }
81
82
    /**
83
     * @return array|OptionInterface[]
84
     */
85
    protected function getDefaultOptions()
86
    {
87
        return array(
88
            new SetTimeOutInSeconds(10)
89
        );
90
    }
91
92
    /**
93
     * @return array
94
     */
95
    protected function getDefaultRawOptions()
96
    {
97
        return array(
98
            CURLOPT_AUTOREFERER     => true,
99
            CURLOPT_CONNECTTIMEOUT  => 5,
100
            CURLOPT_FOLLOWLOCATION  => true,
101
            CURLOPT_MAXREDIRS       => 10,
102
            CURLOPT_USERAGENT       => 'net/bazzline curl component for php'
103
        );
104
    }
105
106
    /**
107
     * @param array|HeaderLineInterface[] $headerLines
108
     * @param Request $request
109
     * @return Request
110
     */
111
    private function addDefaultHeaderLines(array $headerLines, Request $request)
112
    {
113
        //demonstration of using an object as header line
114
        foreach ($headerLines as $headLine) {
115
            $request->addHeaderLine($headLine);
116
        }
117
118
        return $request;
119
    }
120
121
    /**
122
     * @param array $headerLines
123
     * @param Request $request
124
     * @return Request
125
     */
126
    private function addDefaultRawHeaderLines(array $headerLines, Request $request)
127
    {
128
        //demonstration of using strings as header lines
129
        foreach ($headerLines as $headLine) {
130
            $request->addRawHeaderLine($headLine);
131
        }
132
133
        return $request;
134
    }
135
136
    /**
137
     * @param array|OptionInterface[] $options
138
     * @param Request $request
139
     * @return Request
140
     */
141
    private function addDefaultOptions(array $options, Request $request)
142
    {
143
        //demonstration of using an object as option
144
        foreach ($options as $option) {
145
            $request->addOption($option);
146
        }
147
148
        return $request;
149
    }
150
151
    /**
152
     * @param array $options
153
     * @param Request $request
154
     * @return Request
155
     */
156
    private function addDefaultRawOptions(array $options, Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
157
    {
158
        //demonstration of using predefined constants
159
        foreach ($this->getDefaultRawOptions() as $key => $value) {
160
            $request->addRawOption($key, $value);
161
        }
162
163
        return $request;
164
    }
165
}
166