FileMimeList   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 29
ccs 12
cts 12
cp 1
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 12 4
A checkRule() 0 6 2
1
<?php
2
3
namespace kalanis\kw_rules\Rules\File;
4
5
6
use finfo;
7
use kalanis\kw_rules\Interfaces\IValidateFile;
8
use kalanis\kw_rules\Exceptions\RuleException;
9
use kalanis\kw_rules\Rules\TCheckArrayString;
10
11
12
/**
13
 * Class FileMimeList
14
 * @package kalanis\kw_rules\Rules\File
15
 * Check if input file has correct mime type
16
 */
17
class FileMimeList extends AFileRule
18
{
19
    use TCheckArrayString;
20
21 2
    public function validate(IValidateFile $entry): void
22
    {
23 2
        $filename = $entry->getTempName();
24 2
        $finfo = new finfo(FILEINFO_MIME_TYPE);
25 2
        if (!empty($filename)) {
26 2
            foreach ($this->againstValue as $argumentValue) {
27 2
                if ($finfo->file($filename) == $argumentValue) {
28 1
                    return;
29
                }
30
            }
31
        }
32 1
        throw new RuleException($this->errorText);
33
    }
34
35
    /**
36
     * @param mixed|null $singleRule
37
     * @throws RuleException
38
     * @return string
39
     */
40 4
    protected function checkRule($singleRule): string
41
    {
42 4
        if (!is_string($singleRule)) {
43 2
            throw new RuleException('Input for check is not a string.');
44
        }
45 2
        return $singleRule;
46
    }
47
}
48