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

PricePresenterFactory::build()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
namespace hipanel\modules\finance\grid\presenters\price;
4
5
use hipanel\modules\finance\models\ModelGroupPrice;
6
use hipanel\modules\finance\models\Price;
7
use yii\base\InvalidConfigException;
8
9
/**
10
 * Class PricePresenterFactory
11
 *
12
 * @author Dmytro Naumenko <[email protected]>
13
 */
14
class PricePresenterFactory
15
{
16
    /**
17
     * @var array map of model class to its presenter
18
     */
19
    protected $map = [
20
        Price::class => PricePresenter::class,
21
        ModelGroupPrice::class => ModelGroupPricePresenter::class,
22
    ];
23
24
    /**
25
     * @var array
26
     */
27
    protected $cache = [];
28
29
    /**
30
     * @param string $name
31
     * @return PricePresenter
32
     * @throws InvalidConfigException
33
     */
34
    public function build($name)
35
    {
36
        if (!isset($this->map[$name])) {
37
            throw new InvalidConfigException('Can not create presenter for class ' . $name);
38
        }
39
40
        if (!isset($this->cache[$name])) {
41
            $this->cache[$name] = new $this->map[$name]();
42
        }
43
44
        return $this->cache[$name];
45
    }
46
47
    /**
48
     * @return array
49
     */
50
    public function getMap(): array
51
    {
52
        return $this->map;
53
    }
54
55
}
56