Completed
Push — master ( 1035b8...a1620e )
by Albert
03:30
created

MimeType::test()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Albert221\Validation\Rule;
4
5
use InvalidArgumentException;
6
use Psr\Http\Message\UploadedFileInterface;
7
8
class MimeType implements RuleInterface
9
{
10
    use RuleTrait;
11
12
    protected $mimeType;
13
14 3
    public function __construct($mimeType)
15
    {
16 3
        $this->message = 'This field must be of specified MIME type.';
17
18 3
        $this->mimeType = $mimeType;
19 3
    }
20
21 3
    public function test($value)
22
    {
23 3
        if (! $value instanceof UploadedFileInterface) {
24 1
            throw new InvalidArgumentException(sprintf(
25 1
                'MimeType rule excepts field of type %s, %s given.',
26 1
                UploadedFileInterface::class,
27 1
                gettype($value)
28 1
            ));
29
        }
30
31 2
        return $this->mimeType == $value->getClientMediaType();
32
    }
33
}