Issues (88)

src/Spec/File.php (2 issues)

1
<?php
2
3
/**
4
 * This file is part of Dimtrovich/Validation.
5
 *
6
 * (c) 2023 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Dimtrovich\Validation\Spec;
13
14
use BlitzPHP\Filesystem\Files\Mimes;
15
use BlitzPHP\Filesystem\Files\UploadedFile;
16
17
class File extends UploadedFile
18
{
19
    /**
20
     * The name of the file.
21
     *
22
     * @var string
23
     */
24
    public $name;
25
26
    /**
27
     * The temporary file resource.
28
     *
29
     * @var resource
30
     */
31
    public $tempFile;
32
33
    /**
34
     * The "size" to report.
35
     *
36
     * @var int
37
     */
38
    public $sizeToReport;
39
40
    /**
41
     * The MIME type to report.
42
     *
43
     * @var string|null
44
     */
45
    public $mimeTypeToReport;
46
47
    /**
48
     * Create a new file instance.
49
     *
50
     * @param resource $tempFile
51
     *
52
     * @return void
53
     */
54
    public function __construct(string $name, $tempFile)
55
    {
56 2
        $this->name     = $name;
57 2
        $this->tempFile = $tempFile;
58
59 2
        parent::__construct($this->tempFilePath(), null, 0, $name, $this->getMimeType());
60
    }
61
62
    /**
63
     * Create a new fake file.
64
     */
65
    public static function create(string $name, int|string $kilobytes = 0): self
66
    {
67 2
        return (new FileFactory())->create($name, $kilobytes);
68
    }
69
70
    /**
71
     * Create a new fake file with content.
72
     */
73
    public static function createWithContent(string $name, string $content): self
74
    {
75 2
        return (new FileFactory())->createWithContent($name, $content);
76
    }
77
78
    /**
79
     * Create a new fake image.
80
     */
81
    public static function image(string $name, int $width = 10, int $height = 10): self
82
    {
83
        return (new FileFactory())->image($name, $width, $height);
84
    }
85
86
    /**
87
     * Set the "size" of the file in kilobytes.
88
     */
89
    public function size(int $kilobytes): static
90
    {
91
        $this->sizeToReport = $kilobytes * 1024;
92
93
        return $this;
94
    }
95
96
    /**
97
     * Get the size of the file.
98
     */
99
    public function getSize(): int
100
    {
101 2
        return $this->sizeToReport ?: parent::getSize();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->sizeToReport ?: parent::getSize() could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
102
    }
103
104
    /**
105
     * Set the "MIME type" for the file.
106
     */
107
    public function mimeType(string $mimeType): static
108
    {
109
        $this->mimeTypeToReport = $mimeType;
110
111
        return $this;
112
    }
113
114
    /**
115
     * Get the MIME type of the file.
116
     */
117
    public function getMimeType(): string
118
    {
119
        if (null === $this->mimeTypeToReport) {
120 2
            $extension = pathinfo($this->name, PATHINFO_EXTENSION);
121
122 2
            $this->mimeTypeToReport = Mimes::guessTypeFromExtension($extension);
0 ignored issues
show
It seems like $extension can also be of type array; however, parameter $extension of BlitzPHP\Filesystem\File...uessTypeFromExtension() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

122
            $this->mimeTypeToReport = Mimes::guessTypeFromExtension(/** @scrutinizer ignore-type */ $extension);
Loading history...
123
        }
124
125 2
        return $this->mimeTypeToReport ?? '';
126
    }
127
128
    /**
129
     * Get the path to the temporary file.
130
     */
131
    protected function tempFilePath(): string
132
    {
133 2
        return stream_get_meta_data($this->tempFile)['uri'];
134
    }
135
}
136