Test Failed
Push — master ( b2d464...e1b9bb )
by Erandir
02:06
created

FormUrlEncoded   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 8
eloc 16
c 3
b 0
f 0
dl 0
loc 45
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A next() 0 3 1
A handle() 0 7 2
A __construct() 0 3 1
A handleSentData() 0 8 2
A getBody() 0 9 2
1
<?php
2
3
namespace PlugHttp\Body;
4
5
use PlugHttp\Utils\ContentHelper;
6
7
class FormUrlEncoded implements Handler, Advancer
8
{
9
	private Handler $handler;
10
11
	private array $data;
12
13
	public function __construct()
14
    {
15
        $this->data = [];
16
    }
17
18
    public function getBody($content): array
19
	{
20
	    if (is_array($content)) {
21
	        return $content;
22
        }
23
24
	    $this->handleSentData($content);
25
26
	    return $this->data;
27
	}
28
29
    private function handleSentData(string $content): void
30
    {
31
        $body = explode('&', $content);
32
33
        foreach ($body as $parameter) {
34
            $parameterParsed = explode('=', $parameter);
35
36
            $this->data[$parameterParsed[0]] = str_replace("%0A", "\n", $parameterParsed[1]);
37
        }
38
	}
39
40
	public function next(Handler $handler)
41
	{
42
		$this->handler = $handler;
43
	}
44
45
	public function handle($server): array
46
	{
47
		if (ContentHelper::contentIs($server, 'x-www-form-urlencoded')) {
48
			return $this->getBody($server->getContent());
49
		}
50
51
		return $this->handler->handle($server);
52
	}
53
}