Completed
Branch ci (403ea4)
by Florian
02:31
created

FileFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 67.86%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 28
c 2
b 0
f 0
dl 0
loc 74
ccs 19
cts 28
cp 0.6786
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fromUploadedFile() 0 15 1
A fromDisk() 0 18 1
A checkUploadedFile() 0 6 2
A checkFile() 0 8 3
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
        $resource = fopen($path, 'rb');
72
73 6
        return $file->withResource($resource);
74
    }
75
76
    /**
77
     * Checks if the uploaded file is a valid upload
78
     *
79
     * @param \Psr\Http\Message\UploadedFileInterface $uploadedFile Uploaded File
80
     * @return void
81
     */
82 1
    protected static function checkUploadedFile(UploadedFileInterface $uploadedFile): void
83
    {
84 1
        if ($uploadedFile->getError() !== UPLOAD_ERR_OK) {
85 1
            throw new RuntimeException(sprintf(
86 1
                'Can\'t create storage object from upload with error code: %d',
87 1
                $uploadedFile->getError()
88
            ));
89
        }
90
    }
91
92
    /**
93
     * @param string $path Path
94
     * @return void
95
     */
96 6
    protected static function checkFile(string $path): void
97
    {
98 6
        if (!file_exists($path)) {
99
            throw FileDoesNotExistException::filename($path);
100
        }
101
102 6
        if (!is_readable($path)) {
103
            throw FileNotReadableException::filename($path);
104
        }
105 6
    }
106
}
107