Completed
Push — master ( 113c9d...6bb3ba )
by Lorenzo
02:38
created

UploadedFileHelper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 2
dl 0
loc 44
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isValidUploadFile() 0 10 4
A hasValidMimeType() 0 4 3
A getFilenameWithoutExtension() 0 4 1
1
<?php
2
3
namespace Padosoft\Uploadable\Helpers;
4
5
use Illuminate\Http\UploadedFile;
6
7
/**
8
 * UploadedFile Helper Class
9
 * @package Padosoft\Uploadable\Helpers
10
 */
11
class UploadedFileHelper
12
{
13
    /**
14
     * Check if uploaded File is valid and has a valid Mime Type.
15
     * Return true is all ok, otherwise return false.
16
     * @param array $arrMimeType
17
     * @param UploadedFile $uploadedFile
18
     * @return bool
19
     */
20
    public static function isValidUploadFile(array $arrMimeType = array(), UploadedFile $uploadedFile) : bool
21
    {
22
        //check if is valid file
23
        if ($uploadedFile===null || empty($uploadedFile) || !$uploadedFile->isValid()) {
24
            return false;
25
        }
26
27
        // Check if uploaded File has a correct MimeType if specified.
28
        return UplodedFileHelper::hasValidMimeType($arrMimeType, $uploadedFile);
29
    }
30
31
    /**
32
     * Check if uploaded File has a correct MimeType if specified.
33
     * If $arrMimeType is empty array return true.
34
     * @param array $arrMimeType
35
     * @param UploadedFile $uploadedFile
36
     * @return bool
37
     */
38
    public static function hasValidMimeType(array $arrMimeType, UploadedFile $uploadedFile) : bool
39
    {
40
        return !($arrMimeType && count($arrMimeType) > 0 && !in_array($uploadedFile->getMimeType(), $arrMimeType));
0 ignored issues
show
Bug Best Practice introduced by
The expression $arrMimeType of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
41
    }
42
43
    /**
44
     * Return the file name of uploaded file (without path and witout extension).
45
     * Ex.: \public\upload\pippo.txt ritorna 'pippo'
46
     * @param UploadedFile $uploadedFile
47
     * @return string
48
     */
49
    public static function getFilenameWithoutExtension(UploadedFile $uploadedFile)
50
    {
51
        return FileHelper::getFilenameWithoutExtension($uploadedFile->getClientOriginalName());
52
    }
53
54
}
55