FilesCountValidator   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 96
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 12 4
A countFileObjects() 0 4 1
A getFileObjects() 0 13 2
A getUploadDir() 0 11 3
1
<?php
2
3
namespace yiicod\fileupload\validators;
4
5
use yii\base\BaseObject;
6
use yiicod\fileupload\components\common\UploadedFile;
7
use yiicod\fileupload\components\traits\ServerVariableTrait;
8
9
/**
10
 * Class FilesCountValidator
11
 *
12
 * @author Virchenko Maksim <[email protected]>
13
 *
14
 * @package yiicod\fileupload\validators
15
 */
16
class FilesCountValidator extends BaseObject implements ValidatorInterface
17
{
18
    use ServerVariableTrait;
19
20
    /**
21
     * Error message
22
     *
23
     * @var string
24
     */
25
    public $message = 'Maximum number of files exceeded';
26
27
    /**
28
     * Minimum file size
29
     *
30
     * @var int
31
     */
32
    public $maxCountOfFiles;
33
34
    /**
35
     * File upload path
36
     *
37
     * @var string
38
     */
39
    public $uploadDir;
40
41
    /**
42
     * Validate file
43
     *
44
     * @param UploadedFile $file
45
     *
46
     * @return bool
47
     */
48
    public function validate(UploadedFile &$file): bool
49
    {
50
        if (is_int($this->maxCountOfFiles) &&
51
            ($this->countFileObjects() >= $this->maxCountOfFiles) &&
52
            !is_file($this->getUploadDir($file->name))) {
53
            $file->error = $this->message;
54
55
            return false;
56
        }
57
58
        return true;
59
    }
60
61
    /**
62
     * Count file objects
63
     *
64
     * @return int
65
     */
66
    protected function countFileObjects(): int
67
    {
68
        return count($this->getFileObjects('is_valid_file_object'));
69
    }
70
71
    /**
72
     * Get file objects
73
     *
74
     * @param string $iterationMethod
75
     *
76
     * @return array
77
     */
78
    protected function getFileObjects(string $iterationMethod = 'get_file_object'): array
79
    {
80
        $uploadDir = $this->getUploadDir();
81
82
        if (!is_dir($uploadDir)) {
83
            return [];
84
        }
85
86
        return array_values(array_filter(array_map(
87
            [$this, $iterationMethod],
88
            scandir($uploadDir)
89
        )));
90
    }
91
92
    /**
93
     * Get upload path
94
     *
95
     * @param null|string $fileName
96
     * @param null|string $version
97
     *
98
     * @return string
99
     */
100
    protected function getUploadDir(?string $fileName = null, ?string $version = null)
101
    {
102
        $fileName = $fileName ? $fileName : '';
103
        if (empty($version)) {
104
            $versionPath = '';
105
        } else {
106
            $versionPath = $version . '/';
107
        }
108
109
        return $this->uploadDir . $versionPath . $fileName;
110
    }
111
}
112