ServerRequestFactory   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 4
dl 0
loc 102
ccs 51
cts 51
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createServerRequest() 0 4 1
B createServerRequestFromGlobals() 0 12 8
A createUriFromGlobals() 0 19 6
B normalizeFiles() 0 26 6
A createUploadedFileFromSpec() 0 15 2
A normalizeNestedFileSpec() 0 18 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Aidphp\Http;
6
7
use Psr\Http\Message\ServerRequestFactoryInterface;
8
use Interop\Http\PhpServerRequestFactoryInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Message\UriInterface;
11
use Psr\Http\Message\UploadedFileInterface;
12
use InvalidArgumentException;
13
14
class ServerRequestFactory implements ServerRequestFactoryInterface, PhpServerRequestFactoryInterface
15
{
16 12
    public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
17
    {
18 12
        return new ServerRequest($method, $uri, [], null, '1.1', $serverParams);
19
    }
20
21 16
    public function createServerRequestFromGlobals(array $server = null, array $get = null, array $post = null, array $cookies = null, array $files = null): ServerRequestInterface
22
    {
23 16
        $server  = $server ?: $_SERVER;
24 16
        $method  = $server['REQUEST_METHOD'] ?? 'GET';
25 16
        $uri     = $this->createUriFromGlobals($server);
26 16
        $headers = function_exists('getallheaders') ? getallheaders() : [];
27 16
        $body    = new Stream(fopen('php://input', 'r'));
28 16
        $version = isset($server['SERVER_PROTOCOL']) ? str_replace('HTTP/', '', $server['SERVER_PROTOCOL']) : '1.1';
29 16
        $files   = $this->normalizeFiles($files ?: $_FILES);
30
31 15
        return new ServerRequest($method, $uri, $headers, $body, $version, $server, $get ?: $_GET, $post ?: $_POST, $cookies ?: $_COOKIE, $files);
32
    }
33
34 16
    private function createUriFromGlobals(array $server): UriInterface
35
    {
36 16
        $scheme = isset($server['HTTPS']) && 'on' === $server['HTTPS'] ? 'https' : 'http';
37 16
        $host   = $server['HTTP_HOST'] ?? $server['SERVER_NAME'] ?? 'localhost';
38
39 16
        if (preg_match('#\:(\d+)$#', $host, $matches))
40
        {
41 1
            $host = substr($host, 0, -1 * (strlen($matches[1]) + 1));
42 1
            $port = (int) $matches[1];
43
        }
44
        else
45
        {
46 15
            $port = isset($server['SERVER_PORT']) ? $server['SERVER_PORT'] : ($scheme === 'https' ? 443 : 80);
47
        }
48
49 16
        $uri = $server['REQUEST_URI'] ?? '';
50
51 16
        return new Uri($scheme . '://' . $host . ':' . $port . $uri);
52
    }
53
54 16
    private function normalizeFiles(array $files): array
55
    {
56 16
        $result = [];
57 16
        foreach ($files as $key => $value)
58
        {
59 8
            if ($value instanceof UploadedFileInterface)
60
            {
61 2
                $result[$key] = $value;
62
            }
63 7
            elseif (is_array($value) && isset($value['tmp_name']))
64
            {
65 5
                $result[$key] = $this->createUploadedFileFromSpec($value);
66
            }
67 2
            elseif (is_array($value))
68
            {
69 1
                $result[$key] = $this->normalizeFiles($value);
70 1
                continue;
71
            }
72
            else
73
            {
74 8
                throw new InvalidArgumentException('Invalid value in files specification');
75
            }
76
        }
77
78 15
        return $result;
79
    }
80
81 5
    private function createUploadedFileFromSpec(array $value)
82
    {
83 5
        if (is_array($value['tmp_name']))
84
        {
85 1
            return $this->normalizeNestedFileSpec($value);
86
        }
87
88 5
        return new UploadedFile(
89 5
            $value['tmp_name'],
90 5
            (int) $value['size'],
91 5
            (int) $value['error'],
92 5
            $value['name'],
93 5
            $value['type']
94
        );
95
    }
96
97 1
    private function normalizeNestedFileSpec(array $files = []): array
98
    {
99 1
        $result = [];
100 1
        foreach (array_keys($files['tmp_name']) as $key)
101
        {
102
            $spec = [
103 1
                'tmp_name' => $files['tmp_name'][$key],
104 1
                'size'     => $files['size'][$key],
105 1
                'error'    => $files['error'][$key],
106 1
                'name'     => $files['name'][$key],
107 1
                'type'     => $files['type'][$key],
108
            ];
109
110 1
            $result[$key] = $this->createUploadedFileFromSpec($spec);
111
        }
112
113 1
        return $result;
114
    }
115
}