Completed
Push — master ( aa8b89...6eacd0 )
by John
01:54
created

DefaultBodyParserFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
namespace LunixRESTBasics\APIRequest\BodyParser\BodyParserFactory;
3
4
use LunixRESTBasics\APIRequest\BodyParser\BodyParser;
5
use LunixRESTBasics\APIRequest\BodyParser\JSONBodyParser;
6
use LunixRESTBasics\APIRequest\BodyParser\URLEncodedBodyParser;
7
8
/**
9
 * A BodyParserFactory that understands url encoded and json encoded content-types.
10
 * Class DefaultBodyParserFactory
11
 * @package LunixRESTBasics\Request\BodyParser
12
 */
13
class DefaultBodyParserFactory implements BodyParserFactory
14
{
15
16
    protected $registeredBodyParserFactory;
17
18 4
    public function __construct()
19
    {
20 4
        $this->registeredBodyParserFactory = new RegisteredBodyParserFactory([
21 4
            'application/json' => new JSONBodyParser(),
22 4
            'application/x-www-form-urlencoded' => new URLEncodedBodyParser()
23
        ]);
24 4
    }
25
26 1
    public function add($contentType, BodyParser $bodyParser)
27
    {
28 1
        $this->registeredBodyParserFactory->add($contentType, $bodyParser);
29 1
    }
30
31
    /**
32
     * Parses API request data out of a url
33
     * @param string $contentType
34
     * @return BodyParser
35
     */
36 4
    public function create(string $contentType): BodyParser
37
    {
38 4
        return $this->registeredBodyParserFactory->create($contentType);
39
    }
40
}
41