Passed
Branch sudav3 (acb061)
by 世昌
02:16
created

UploadedFile   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 74
rs 10
c 0
b 0
f 0
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getError() 0 3 1
A setMimeType() 0 5 1
A getMimeType() 0 3 1
A setError() 0 5 1
A isValid() 0 3 2
A __construct() 0 5 3
A __destruct() 0 3 2
1
<?php
2
namespace suda\framework\server\request;
3
4
use suda\component\file\File;
5
6
7
8
9
/**
10
 * HTTP请求文件
11
 */
12
class UploadedFile  extends File  {
13
14
    private $originalName;
15
    private $mimeType;
16
    private $error;
17
18
    /**
19
     * 创建文件
20
     *
21
     * @param string $path 路径 
22
     * @param string $name 文件名
23
     * @param string $mimeType mime类型
24
     * @param integer $error 错误码
25
     */
26
    public function __construct(string $path, string $name, string $mimeType = null, int $error = null) {
27
        $this->mimeType = $mimeType ?: 'application/octet-stream';
28
        $this->error = $error ?: UPLOAD_ERR_OK;
29
        $this->originalName = pathinfo($name, PATHINFO_FILENAME);
30
        parent::__construct($path);
31
    }
32
33
    /**
34
     * Get the value of error
35
     */ 
36
    public function getError()
37
    {
38
        return $this->error;
39
    }
40
41
    /**
42
     * Set the value of error
43
     *
44
     * @return  self
45
     */ 
46
    public function setError($error)
47
    {
48
        $this->error = $error;
49
50
        return $this;
51
    }
52
53
    /**
54
     * Get the value of mimeType
55
     */ 
56
    public function getMimeType()
57
    {
58
        return $this->mimeType;
59
    }
60
61
    /**
62
     * Set the value of mimeType
63
     *
64
     * @return  self
65
     */ 
66
    public function setMimeType($mimeType)
67
    {
68
        $this->mimeType = $mimeType;
69
70
        return $this;
71
    }
72
73
    /**
74
     * 判断文件是否可用
75
     *
76
     * @return boolean
77
     */
78
    public function isValid()
79
    {
80
        return UPLOAD_ERR_OK === $this->error && is_uploaded_file($this->getPathname());
81
    }
82
83
    public function __destruct() {
84
        if (file_exists($this->getPathname())) {
85
            @unlink($this->getPathname());
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

85
            /** @scrutinizer ignore-unhandled */ @unlink($this->getPathname());

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
86
        }
87
    }
88
}