Completed
Push — master ( 958dbc...2d3a59 )
by Dmitry
05:33
created

PriceModelFactory::build()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace hipanel\modules\finance\models\factories;
4
5
use hipanel\modules\finance\models\ModelGroupPrice;
6
use hipanel\modules\finance\models\Price;
7
use yii\base\InvalidConfigException;
8
9
/**
10
 * Class PriceModelFactory builds objects, that must be children of [[Price]] class
11
 * using short class name. See definitions in $map property.
12
 *
13
 * @author Dmytro Naumenko <[email protected]>
14
 */
15
class PriceModelFactory
16
{
17
    /**
18
     * @var array
19
     */
20
    protected $map = [
21
        'Price' => Price::class,
22
        'ModelGroupPrice' => ModelGroupPrice::class,
23
        'SinglePrice' => Price::class,
24
    ];
25
26
    /**
27
     * @param string $name
28
     * @return Price
29
     * @throws InvalidConfigException
30
     */
31
    public function build($name)
32
    {
33
        if (!isset($this->map[$name])) {
34
            throw new InvalidConfigException('Can not create model for class ' . $name);
35
        }
36
37
        return new $this->map[$name]();
38
    }
39
40
    /**
41
     * @return array
42
     */
43
    public function getMap(): array
44
    {
45
        return $this->map;
46
    }
47
}
48