Passed
Push — master ( fb282f...fe0d43 )
by Terry
01:45
created

UploadedFileFactory::fromGlobal()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/*
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Shieldon\Psr17;
14
15
use Psr\Http\Message\StreamInterface;
16
use Psr\Http\Message\UploadedFileFactoryInterface;
17
use Psr\Http\Message\UploadedFileInterface;
18
use Shieldon\Psr7\UploadedFile;
19
use Shieldon\Psr7\Utils\UploadedFileHelper;
20
use InvalidArgumentException;
21
22
/**
23
 * PSR-17 Uploaded File Factory
24
 */
25
class UploadedFileFactory implements UploadedFileFactoryInterface
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function createUploadedFile(
31
        StreamInterface $stream,
32
        int             $size            = null,
33
        int             $error           = \UPLOAD_ERR_OK,
34
        string          $clientFilename  = null,
35
        string          $clientMediaType = null
36
    ): UploadedFileInterface
37
    {
38
        if (! $stream->isReadable()) {
39
            throw new InvalidArgumentException(
40
                'File is not readable.'
41
            );
42
        }
43
44
        return new UploadedFile(
45
            $stream,
46
            $clientFilename,
47
            $clientMediaType,
48
            $size,
49
            $error
50
        );
51
    }
52
53
    /*
54
    |--------------------------------------------------------------------------
55
    | Non PSR-7 Methods.
56
    |--------------------------------------------------------------------------
57
    */
58
59
    /**
60
     * Create an array with UriInterface structure.
61
     *
62
     * @return array
63
     */
64
    public static function fromGlobal(): array
65
    {
66
        $filesParams = $_FILES ?? [];
67
        $uploadedFiles = [];
68
69
        if (! empty($filesParams)) {
70
            $uploadedFiles = UploadedFileHelper::uploadedFileSpecsConvert(
71
                UploadedFileHelper::uploadedFileParse($filesParams)
72
            );
73
        }
74
75
        return $uploadedFiles;
76
    }
77
}