RequestFactory::buildRequestBody()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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