RequestFactory::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 10
ccs 9
cts 9
cp 1
crap 1
rs 10
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