FileMaxSize::fileSizeString2size()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 17
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 20
ccs 18
cts 18
cp 1
crap 4
rs 9.7
1
<?php
2
3
namespace kalanis\kw_rules\Rules\File;
4
5
6
use kalanis\kw_rules\Interfaces\IValidateFile;
7
use kalanis\kw_rules\Exceptions\RuleException;
8
9
10
/**
11
 * Class FileMaxSize
12
 * @package kalanis\kw_rules\Rules\File
13
 * Check if input file has correct size
14
 */
15
class FileMaxSize extends AFileRule
16
{
17 5
    public function checkValue($againstValue)
18
    {
19 5
        return $this->fileSizeString2size($againstValue);
20
    }
21
22 5
    public function validate(IValidateFile $entry): void
23
    {
24 5
        if ($entry->getSize() > $this->againstValue) {
25 2
            throw new RuleException($this->errorText);
26
        }
27 3
    }
28
29 5
    protected function fileSizeString2size(string $string): int
30
    {
31 5
        $size = intval($string);
32 5
        $posK = stripos($string, 'k');
33 5
        $posM = stripos($string, 'm');
34 5
        $posG = stripos($string, 'g');
35 5
        if (false !== $posK) {
36 2
            list($value, ) = (array) explode(strval(substr($string, $posK, 1)), $string);
37 2
            $value = floatval($value);
38 2
            $size = intval($value * 1024);
39 3
        } elseif (false !== $posM) {
40 1
            list($value, ) = (array) explode(strval(substr($string, $posM, 1)), $string);
41 1
            $value = floatval($value);
42 1
            $size = intval($value * 1024 * 1024);
43 2
        } elseif (false !== $posG) {
44 1
            list($value, ) = (array) explode(strval(substr($string, $posG, 1)), $string);
45 1
            $value = floatval($value);
46 1
            $size = intval($value * 1024 * 1024 * 1024);
47
        }
48 5
        return $size;
49
    }
50
}
51