RequestFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 35
ccs 13
cts 13
cp 1
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A buildRequestBody() 0 7 2
A create() 0 10 1
1
<?php
2
/**
3
 * Copyright (c) 2020.
4
 * @author Paweł Antosiak <[email protected]>
5
 */
6
7
declare(strict_types=1);
8
9
namespace Gorynych\Http;
10
11
use Gorynych\Util\EnvAccess;
12
use Symfony\Component\HttpFoundation\Request;
13
14
final class RequestFactory implements RequestFactoryInterface
15
{
16
    public const REQUEST_JSON = 'json';
17
18
    private const DEFAULT_REQUEST_HEADERS = [
19
        'HTTP_ACCEPT' => 'application/json',
20
        'CONTENT_TYPE' => 'application/json',
21
    ];
22
23
    /**
24
     * {@inheritdoc}
25
     */
26 1
    public function create(string $method, string $uri, array $options = []): Request
27
    {
28 1
        return Request::create(
29 1
            EnvAccess::get('BASE_URI') . $uri,
30 1
            $method,
31 1
            $options['parameters'] ?? [],
32 1
            $options['cookies'] ?? [],
33 1
            $options['files'] ?? [],
34 1
            array_merge(self::DEFAULT_REQUEST_HEADERS, $options['headers'] ?? []),
35 1
            $this->buildRequestBody($options),
36
        );
37
    }
38
39
    /**
40
     * @param string[][] $options
41
     */
42 1
    private function buildRequestBody(array $options): ?string
43
    {
44 1
        if (true === array_key_exists(self::REQUEST_JSON, $options)) {
45 1
            $body = json_encode($options[self::REQUEST_JSON]);
46
        }
47
48 1
        return $body ?? null;
49
    }
50
}
51