Completed
Push — master ( f7bdb9...8d2b6e )
by Andrii
10:42
created

ResourceDecoratorFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 24
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A typeMap() 0 4 1
A createFromResource() 0 11 2
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