UploadedFileFactory::createUploadedFile()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 20
ccs 11
cts 11
cp 1
rs 9.9666
cc 2
nc 2
nop 5
crap 2
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 2
    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 2
        if (!$stream->isReadable()) {
39 1
            throw new InvalidArgumentException(
40 1
                'File is not readable.'
41 1
            );
42
        }
43
44 1
        return new UploadedFile(
45 1
            $stream,
46 1
            $clientFilename,
47 1
            $clientMediaType,
48 1
            $size,
49 1
            $error
50 1
        );
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 2
    public static function fromGlobal(): array
65
    {
66 2
        $filesParams = $_FILES ?? [];
67 2
        $uploadedFiles = [];
68
69 2
        if (!empty($filesParams)) {
70 2
            $uploadedFiles = UploadedFileHelper::uploadedFileSpecsConvert(
71 2
                UploadedFileHelper::uploadedFileParse($filesParams)
72 2
            );
73
        }
74
75 2
        return $uploadedFiles;
76
    }
77
}
78