FileFactory::checkFile()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
/**
4
 * Copyright (c) Florian Krämer (https://florian-kraemer.net)
5
 * Licensed under The MIT License
6
 * For full copyright and license information, please see the LICENSE.txt
7
 * Redistributions of files must retain the above copyright notice.
8
 *
9
 * @copyright Copyright (c) Florian Krämer (https://florian-kraemer.net)
10
 * @author    Florian Krämer
11
 * @link      https://github.com/Phauthentic
12
 * @license   https://opensource.org/licenses/MIT MIT License
13
 */
14
15
declare(strict_types=1);
16
17
namespace Phauthentic\Infrastructure\Storage;
18
19
use Phauthentic\Infrastructure\Storage\Exception\FileDoesNotExistException;
20
use Phauthentic\Infrastructure\Storage\Exception\FileNotReadableException;
21
use Phauthentic\Infrastructure\Storage\Utility\MimeType;
22
use Phauthentic\Infrastructure\Storage\Utility\PathInfo;
23
use Phauthentic\Infrastructure\Storage\Utility\StreamWrapper;
24
use Psr\Http\Message\UploadedFileInterface;
25
use RuntimeException;
26
27
/**
28
 * File Factory
29
 */
30
class FileFactory implements FileFactoryInterface
31
{
32
    /**
33
     * @inheritDoc
34
     */
35
    public static function fromUploadedFile(
36
        UploadedFileInterface $uploadedFile,
37
        string $storage
38
    ): FileInterface {
39
        static::checkUploadedFile($uploadedFile);
40
41
        $file = File::create(
42
            (string)$uploadedFile->getClientFilename(),
43
            (int)$uploadedFile->getSize(),
44
            (string)$uploadedFile->getClientMediaType(),
45
            $storage
46
        );
47
48
        return $file->withResource(
49
            StreamWrapper::getResource($uploadedFile->getStream())
50
        );
51
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56
    public static function fromDisk(string $path, string $storage): FileInterface
57
    {
58
        static::checkFile($path);
59
60
        $info = PathInfo::for($path);
61
        $filesize = filesize($path);
62
        if ($filesize === false) {
63
            throw new RuntimeException(sprintf('Failed to get file size for: %s', $path));
64
        }
65
66
        $extension = $info->extension();
67
        $mimeType = $extension !== null ? MimeType::byExtension($extension) : 'application/octet-stream';
68
        if ($mimeType === null) {
0 ignored issues
show
introduced by
The condition $mimeType === null is always false.
Loading history...
69
            $mimeType = 'application/octet-stream';
70
        }
71
72
        $file = File::create(
73
            $info->basename(),
74
            $filesize,
75
            $mimeType,
76
            $storage,
77
        );
78
79
        $resource = fopen($path, 'rb');
80
81
        return $file->withResource($resource);
82
    }
83
84
    /**
85
     * Checks if the uploaded file is a valid upload
86
     *
87
     * @param \Psr\Http\Message\UploadedFileInterface $uploadedFile Uploaded File
88
     * @return void
89
     */
90
    protected static function checkUploadedFile(UploadedFileInterface $uploadedFile): void
91
    {
92
        if ($uploadedFile->getError() !== UPLOAD_ERR_OK) {
93
            throw new RuntimeException(sprintf(
94
                'Can\'t create storage object from upload with error code: %d',
95
                $uploadedFile->getError()
96
            ));
97
        }
98
    }
99
100
    /**
101
     * @param string $path Path
102
     * @return void
103
     */
104
    protected static function checkFile(string $path): void
105
    {
106
        if (!file_exists($path)) {
107
            throw FileDoesNotExistException::filename($path);
108
        }
109
110
        if (!is_readable($path)) {
111
            throw FileNotReadableException::filename($path);
112
        }
113
    }
114
}
115