Passed
Branch ci (542cc6)
by Florian
02:33
created

FileFactory::fromUploadedFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.4218

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 15
ccs 2
cts 8
cp 0.25
crap 1.4218
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 GuzzleHttp\Psr7\StreamWrapper;
20
use Phauthentic\Infrastructure\Storage\Exception\FileDoesNotExistException;
21
use Phauthentic\Infrastructure\Storage\Exception\FileNotReadableException;
22
use Phauthentic\Infrastructure\Storage\Utility\MimeType;
23
use Phauthentic\Infrastructure\Storage\Utility\PathInfo;
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 1
    public static function fromUploadedFile(
36
        UploadedFileInterface $uploadedFile,
37
        string $storage
38
    ): FileInterface {
39 1
        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 6
    public static function fromDisk(string $path, string $storage): FileInterface
57
    {
58 6
        static::checkFile($path);
59
60 6
        $info = PathInfo::for($path);
61 6
        $filesize = filesize($path);
62 6
        $mimeType = MimeType::byExtension($info->extension());
63
64 6
        $file = File::create(
65 6
            $info->basename(),
66
            $filesize,
67
            $mimeType,
68
            $storage,
69
        );
70
71 6
        $result = fopen($path, 'rb');
72 6
        if ($result === false) {
73
            throw new RuntimeException(sprintf(
74
                'Failed to open file `%s for reading`',
75
                $path
76
            ));
77
        }
78
79 6
        return $file->withResource($result);
80
    }
81
82
    /**
83
     * Checks if the uploaded file is a valid upload
84
     *
85
     * @param \Psr\Http\Message\UploadedFileInterface $uploadedFile Uploaded File
86
     * @return void
87
     */
88 1
    protected static function checkUploadedFile(UploadedFileInterface $uploadedFile): void
89
    {
90 1
        if ($uploadedFile->getError() !== UPLOAD_ERR_OK) {
91 1
            throw new RuntimeException(sprintf(
92 1
                'Can\'t create storage object from upload with error code: %d',
93 1
                $uploadedFile->getError()
94
            ));
95
        }
96
    }
97
98
    /**
99
     * @param string $path Path
100
     * @return void
101
     */
102 6
    protected static function checkFile(string $path): void
103
    {
104 6
        if (!file_exists($path)) {
105
            throw FileDoesNotExistException::filename($path);
106
        }
107
108 6
        if (!is_readable($path)) {
109
            throw FileNotReadableException::filename($path);
110
        }
111 6
    }
112
}
113