Completed
Pull Request — master (#48)
by Chad
02:20
created

RequestBridge::convertUploadedFiles()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 4
nc 3
nop 1
1
<?php
2
namespace Chadicus\Slim\OAuth2\Http;
3
4
use Psr\Http\Message\ServerRequestInterface;
5
use Psr\Http\Message\UploadedFileInterface;
6
use OAuth2;
7
8
/**
9
 * Static utility class for bridging Psr-7 Requests to OAuth2 Requests.
10
 */
11
class RequestBridge
12
{
13
    /**
14
     * Returns a new instance of \OAuth2\Request based on the given \Slim\Http\Request
15
     *
16
     * @param ServerRequestInterface $request The psr-7 request.
17
     *
18
     * @return OAuth2\Request
19
     */
20
    final public static function toOAuth2(ServerRequestInterface $request)
21
    {
22
        $contents = $request->getBody()->getContents();
23
        $request->getBody()->rewind();
24
25
        return new OAuth2\Request(
26
            (array)$request->getQueryParams(),
27
            (array)$request->getParsedBody(),
28
            $request->getAttributes(),
29
            $request->getCookieParams(),
30
            self::convertUploadedFiles($request->getUploadedFiles()),
31
            $request->getServerParams(),
32
            $contents,
33
            self::cleanupHeaders($request->getHeaders())
34
        );
35
    }
36
37
    /**
38
     * Helper method to clean header keys and values.
39
     *
40
     * Slim will convert all headers to Camel-Case style. There are certain headers such as PHP_AUTH_USER that the
41
     * OAuth2 library requires CAPS_CASE format. This method will adjust those headers as needed.  The OAuth2 library
42
     * also does not expect arrays for header values, this method will implode the multiple values with a ', '
43
     *
44
     * @param array $uncleanHeaders The headers to be cleaned.
45
     *
46
     * @return array The cleaned headers
47
     */
48
    private static function cleanupHeaders(array $uncleanHeaders = [])
49
    {
50
        $cleanHeaders = [];
51
        $headerMap = [
52
            'Php-Auth-User' => 'PHP_AUTH_USER',
53
            'Php-Auth-Pw' => 'PHP_AUTH_PW',
54
            'Php-Auth-Digest' => 'PHP_AUTH_DIGEST',
55
            'Auth-Type' => 'AUTH_TYPE',
56
            'HTTP_AUTHORIZATION' => 'AUTHORIZATION',
57
        ];
58
59
        foreach ($uncleanHeaders as $key => $value) {
60
            if (array_key_exists($key, $headerMap)) {
61
                $key = $headerMap[$key];
62
            }
63
64
            $cleanHeaders[$key] = is_array($value) ? implode(', ', $value) : $value;
65
        }
66
67
        return $cleanHeaders;
68
    }
69
70
    /**
71
     * Convert a PSR-7 uploaded files structure to a $_FILES structure.
72
     *
73
     * @param array $uploadedFiles Array of file objects.
74
     *
75
     * @return array
76
     */
77
    private static function convertUploadedFiles(array $uploadedFiles)
78
    {
79
        $files = [];
80
        foreach ($uploadedFiles as $name => $uploadedFile) {
81
            if (!is_array($uploadedFile)) {
82
                $files[$name] = self::convertUploadedFile($uploadedFile);
83
                continue;
84
            }
85
86
            $files[$name] = [];
87
            foreach ($uploadedFile as $file) {
88
                $files[$name][] = self::convertUploadedFile($file);
89
            }
90
        }
91
92
        return $files;
93
    }
94
95
    private static function convertUploadedFile(UploadedFileInterface $uploadedFile)
96
    {
97
        return [
98
            'name' => $uploadedFile->getClientFilename(),
99
            'type' => $uploadedFile->getClientMediaType(),
100
            'size' => $uploadedFile->getSize(),
101
            'tmp_name' => $uploadedFile->getStream()->getMetadata('uri'),
102
            'error' => $uploadedFile->getError(),
103
        ];
104
    }
105
}
106