Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
UploadedFileFactory
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 createUploadedFile
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <dev@noladev.com>
8 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Http\Factory;
16
17use Psr\Http\Message\UploadedFileFactoryInterface;
18use Psr\Http\Message\UploadedFileInterface;
19use Psr\Http\Message\StreamInterface;
20use Pop\Http\Server\UploadedFile;
21
22/**
23 * PSR-17 uploaded file factory class
24 *
25 * @category   Pop
26 * @package    Pop\Http
27 * @author     Nick Sagona, III <dev@noladev.com>
28 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
29 * @license    https://www.popphp.org/license     New BSD License
30 * @version    6.0.0
31 */
32class UploadedFileFactory implements UploadedFileFactoryInterface
33{
34
35    /**
36     * Create a new uploaded file, materializing the given stream's contents
37     * into a temporary file so UploadedFile has a real path to work from
38     *
39     * @param  StreamInterface $stream
40     * @param  ?int            $size
41     * @param  int             $error
42     * @param  ?string         $clientFilename
43     * @param  ?string         $clientMediaType
44     * @return UploadedFileInterface
45     */
46    public function createUploadedFile(
47        StreamInterface $stream,
48        ?int $size = null,
49        int $error = UPLOAD_ERR_OK,
50        ?string $clientFilename = null,
51        ?string $clientMediaType = null
52    ): UploadedFileInterface
53    {
54        $content = (string)$stream;
55        $tmpFile = tempnam(sys_get_temp_dir(), 'pop-http-uploaded-');
56        file_put_contents($tmpFile, $content);
57
58        return new UploadedFile($tmpFile, $size ?? strlen($content), $error, $clientFilename, $clientMediaType);
59    }
60
61}