ContentsFileValidation   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 15
lcom 0
cbo 2
dl 0
loc 103
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A checkMaxSize() 0 5 1
A uploadMaxSizeCheck() 0 4 1
A calcFileSizeUnit() 0 12 5
A checkExtension() 0 16 4
A extensionDd() 0 16 4
1
<?php
2
3
namespace ContentsFile\Validation;
4
5
use Cake\Validation\Validation;
6
use Laminas\Diactoros\UploadedFile;
7
8
class ContentsFileValidation extends Validation
9
{
10
    /**
11
     * checkMaxSize
12
     *
13
     * @param UploadedFile $value
14
     * @param int|string $max
15
     * @param mixed $context
16
     * @return bool
17
     */
18
    public static function checkMaxSize(UploadedFile $value, $max, $context): bool
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
19
    {
20
        $maxValue = self::calcFileSizeUnit($max);
21
        return $maxValue >= $value->getSize();
22
    }
23
24
    /**
25
     * uploadMaxSizeCheck
26
     *
27
     * @param UploadedFile $value
28
     * @param mixed $context
29
     * @return bool
30
     */
31
    public static function uploadMaxSizeCheck(UploadedFile $value, $context): bool
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
32
    {
33
        return $value->getError() != UPLOAD_ERR_INI_SIZE;
34
    }
35
36
    /**
37
     * Calculate file size by unit
38
     *
39
     * e.g.) 100KB -> 1024000
40
     *
41
     * @param int|string $size
42
     * @return int|bool
43
     */
44
    private static function calcFileSizeUnit($size)
45
    {
46
        $units = ['K', 'M', 'G', 'T'];
47
        $byte = 1024;
48
49
        if (is_numeric($size) || is_int($size)) {
50
            return $size;
51
        } else if (is_string($size) && preg_match('/^([0-9]+(?:\.[0-9]+)?)(' . implode('|', $units) . ')B?$/i', $size, $matches)) {
52
            return $matches[1] * pow($byte, array_search($matches[2], $units) + 1);
53
        }
54
        return false;
55
    }
56
57
    /**
58
     * checkExtension
59
     * 拡張子のチェック
60
     *
61
     * @param UploadedFile $value
62
     * @param array $extensions
63
     * @return bool
64
     */
65
    public static function checkExtension(UploadedFile $value, array $extensions = ['gif', 'jpeg', 'png', 'jpg']): bool
66
    {
67
        $check = $value->getClientFilename();
68
        if (is_null($check)) {
69
            return true;
70
        }
71
72
        $checkExtension = strtolower(pathinfo($check, PATHINFO_EXTENSION));
73
        foreach ($extensions as $extension) {
74
            if ($checkExtension === strtolower($extension)) {
75
                return true;
76
            }
77
        }
78
79
        return false;
80
    }
81
82
    /* ドラッグアンドドロップアップロード専用  @Todo: 後*/
83
    /**
84
     * extensionDd
85
     * 拡張子のチェック
86
     *
87
     * @param string $value
88
     * @param array $extensions
89
     * @param string $filenameField
90
     * @param mixed $context
91
     * @return bool
92
     */
93
    public static function extensionDd(string $value, array $extensions, string $filenameField, $context): bool
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
94
    {
95
        // チェックに必要なフィールドがない
96
        if (!array_key_exists($filenameField, $context['data'])) {
97
            return false;
98
        }
99
        $filename = $context['data'][$filenameField];
100
        $extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
101
        foreach ($extensions as $value) {
102
            if ($extension === strtolower($value)) {
103
                return true;
104
            }
105
        }
106
107
        return false;
108
    }
109
110
}
111