FileMimeList::checkRule()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
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