Factory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 37
ccs 10
cts 10
cp 1
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A getRule() 0 15 4
1
<?php
2
3
namespace kalanis\kw_rules\Rules\File;
4
5
6
use kalanis\kw_rules\Interfaces\IRuleFactory;
7
use kalanis\kw_rules\Interfaces\IRules;
8
use kalanis\kw_rules\Exceptions\RuleException;
9
use ReflectionClass;
10
use ReflectionException;
11
12
13
/**
14
 * Class Factory
15
 * @package kalanis\kw_rules\Rules\File
16
 * Factory for getting rules for files
17
 */
18
class Factory implements IRuleFactory
19
{
20
    /** @var array<string, class-string<AFileRule>> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, class-string<AFileRule>> at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array<string, class-string<AFileRule>>.
Loading history...
21
    protected array $map = [
22
        IRules::FILE_EXISTS             => FileExists::class,
23
        IRules::FILE_SENT               => FileSent::class,
24
        IRules::FILE_RECEIVED           => FileReceived::class,
25
        IRules::FILE_MAX_SIZE           => FileMaxSize::class,
26
        IRules::FILE_MIMETYPE_EQUALS    => FileMimeEquals::class,
27
        IRules::FILE_MIMETYPE_IN_LIST   => FileMimeList::class,
28
        IRules::IS_IMAGE                => ImageIs::class,
29
        IRules::IMAGE_DIMENSION_EQUALS  => ImageSizeEquals::class,
30
        IRules::IMAGE_DIMENSION_IN_LIST => ImageSizeList::class,
31
        IRules::IMAGE_MAX_DIMENSION     => ImageSizeMax::class,
32
        IRules::IMAGE_MIN_DIMENSION     => ImageSizeMin::class,
33
    ];
34
35
    /**
36
     * @param string $ruleName
37
     * @throws RuleException
38
     * @return AFileRule
39
     */
40 54
    public function getRule(string $ruleName): AFileRule
41
    {
42 54
        if (isset($this->map[$ruleName])) {
43 15
            $rule = $this->map[$ruleName];
44
            try {
45 15
                $ref = new ReflectionClass($rule);
46 14
                $class = $ref->newInstance();
47 14
                if ($class instanceof AFileRule) {
48 14
                    return $class;
49
                }
50 1
            } catch (ReflectionException $ex) {
51 1
                throw new RuleException($ex->getMessage(), $ex->getCode(), $ex);
52
            }
53
        }
54 41
        throw new RuleException(sprintf('Unknown rule %s', $ruleName));
55
    }
56
}
57