RequestFactory   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 0
loc 132
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 21 1
A overwriteDispatcher() 0 4 1
A getDispatcher() 0 10 2
A getNewDispatcher() 0 4 1
A getDefaultHeaderLines() 0 4 1
A getDefaultRawHeaderLine() 0 4 1
A getDefaultOptions() 0 6 1
A getDefaultRawOptions() 0 10 1
A createDefaultHeaderLines() 0 10 2
A createDefaultOptions() 0 10 2
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\ContentTypeIsUtf8Form;
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
        $defaultHeaderLines = $this->createDefaultHeaderLines(
25
            $this->getDefaultRawHeaderLine(),
26
            $this->getDefaultHeaderLines()
27
        );
28
29
        $defaultOptions     = $this->createDefaultOptions(
30
            $this->getDefaultRawOptions(),
31
            $this->getDefaultOptions()
32
        );
33
34
        $request = new Request(
35
            $this->getDispatcher(),
36
            new Merge(),
37
            $defaultHeaderLines,
38
            $defaultOptions
39
        );
40
41
        return $request;
42
    }
43
44
    /**
45
     * @param DispatcherInterface $dispatcher
46
     */
47
    public function overwriteDispatcher(DispatcherInterface $dispatcher)
48
    {
49
        $this->dispatcher = $dispatcher;
50
    }
51
52
    /**
53
     * @return DispatcherInterface
54
     */
55
    protected function getDispatcher()
56
    {
57
        $createDispatcher = !($this->dispatcher instanceof  DispatcherInterface);
58
59
        if ($createDispatcher) {
60
            $this->dispatcher = $this->getNewDispatcher();
61
        }
62
63
        return $this->dispatcher;
64
    }
65
66
    /**
67
     * @return DispatcherInterface
68
     */
69
    protected function getNewDispatcher()
70
    {
71
        return new Dispatcher();
72
    }
73
74
    /**
75
     * @return array|HeaderLineInterface[]
76
     */
77
    protected function getDefaultHeaderLines()
78
    {
79
        return [];
80
    }
81
82
    /**
83
     * @return array
84
     */
85
    protected function getDefaultRawHeaderLine()
86
    {
87
        return [];
88
    }
89
90
    /**
91
     * @return array|OptionInterface[]
92
     */
93
    protected function getDefaultOptions()
94
    {
95
        return [
96
            new SetTimeOutInSeconds(10)
97
        ];
98
    }
99
100
    /**
101
     * @return array
102
     */
103
    protected function getDefaultRawOptions()
104
    {
105
        return [
106
            CURLOPT_AUTOREFERER     => true,
107
            CURLOPT_CONNECTTIMEOUT  => 5,
108
            CURLOPT_FOLLOWLOCATION  => true,
109
            CURLOPT_MAXREDIRS       => 10,
110
            CURLOPT_USERAGENT       => 'net/bazzline curl component for php'
111
        ];
112
    }
113
114
    /**
115
     * @param array $rawHeaderLines
116
     * @param array|HeaderLineInterface[] $headerLines
117
     * @return array
118
     */
119
    private function createDefaultHeaderLines(array $rawHeaderLines, array $headerLines)
120
    {
121
        $defaultHeaderLines = $rawHeaderLines;
122
123
        foreach ($headerLines as $headerLine) {
124
            $defaultHeaderLines[] = $headerLine->line();
125
        }
126
127
        return $defaultHeaderLines;
128
    }
129
130
    /**
131
     * @param array $rawOptions
132
     * @param array|OptionInterface[] $options
133
     * @return array
134
     */
135
    private function createDefaultOptions(array $rawOptions, array $options)
136
    {
137
        $defaultOptions = $rawOptions;
138
139
        foreach ($options as $option) {
140
            $defaultOptions[$option->identifier()] = $option->value();
141
        }
142
143
        return $defaultOptions;
144
    }
145
}
146