ResourceDecoratorFactory::createFromResource()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace hipanel\modules\finance\models\decorators;
4
5
use yii\base\InvalidConfigException;
6
7
class ResourceDecoratorFactory
8
{
9
    protected static function typeMap(): array
10
    {
11
        return [];
12
    }
13
14
    /**
15
     * @param $resource
16
     * @return ResourceDecoratorInterface
17
     * @throws InvalidConfigException
18
     */
19
    public static function createFromResource($resource): ResourceDecoratorInterface
20
    {
21
        $type = $resource->model_type ?? $resource->type;
22
        $map = static::typeMap();
23
24
        if (!isset($map[$type])) {
25
            throw new InvalidConfigException('No representative decoration class found for type "' . $type . '"');
26
        }
27
28
        return new $map[$type]($resource);
29
    }
30
}
31