Completed
Push — 2.0 ( 9e2b8d )
by Nicolas
03:40
created

MaxFolderSizeValidator::validateMaxSize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 3
1
<?php namespace Modules\Media\Validators;
2
3
use RecursiveDirectoryIterator;
4
use RecursiveIteratorIterator;
5
use Symfony\Component\HttpFoundation\File\UploadedFile;
6
7
class MaxFolderSizeValidator
8
{
9
    public function validateMaxSize($attribute, UploadedFile $value, $parameters)
0 ignored issues
show
Unused Code introduced by
The parameter $attribute 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...
Unused Code introduced by
The parameter $parameters 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...
10
    {
11
        $mediaPath = public_path(config('asgard.media.config.files-path'));
12
        $folderSize = $this->getDirSize($mediaPath);
13
14
        preg_match('/([0-9]+)/', $folderSize, $match);
15
16
        return ($match[0] + $value->getSize()) < config('asgard.media.config.max-total-size');
17
    }
18
19
    /**
20
    * Get the directory size
21
    * @param string $directory
22
    * @return int
23
    */
24
    public function getDirSize($directory)
25
    {
26
        $size = 0;
27
        foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file) {
28
            $size += $file->getSize();
29
        }
30
31
        return $size;
32
    }
33
}
34