Completed
Push — master ( e35889...138bca )
by Dmitry
11:39
created

PricePresenterFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 1
cp 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace hipanel\modules\finance\grid\presenters\price;
4
5
use hipanel\modules\finance\models\CertificatePrice;
6
use hipanel\modules\finance\models\DomainServicePrice;
7
use hipanel\modules\finance\models\DomainZonePrice;
8
use hipanel\modules\finance\models\TemplatePrice;
9
use hipanel\modules\finance\models\Price;
10
use yii\base\InvalidConfigException;
11
use yii\di\Container;
12
13
/**
14
 * Class PricePresenterFactory
15
 *
16
 * @author Dmytro Naumenko <[email protected]>
17
 */
18
class PricePresenterFactory
19
{
20
    /**
21
     * @var array map of model class to its presenter
22
     */
23
    protected $map = [
24
        Price::class => PricePresenter::class,
25
        TemplatePrice::class => TemplatePricePresenter::class,
26
        CertificatePrice::class => CertificatePricePresenter::class,
27
        '*' => PricePresenter::class,
28
    ];
29
30
    /**
31
     * @var array
32
     */
33
    protected $cache = [];
34
    /**
35
     * @var Container
36
     */
37
    private $container;
38
39
    public function __construct(Container $container)
40
    {
41
        $this->container = $container;
42
    }
43
44
    /**
45
     * @param string $name
46
     * @return PricePresenter
47
     * @throws InvalidConfigException
48
     */
49
    public function build($name)
50
    {
51
        $className = $this->map[$name] ?? $this->map['*'];
52
53
        if (!isset($this->cache[$name])) {
54
            $this->cache[$name] = $this->container->get($className);
55
        }
56
57
        return $this->cache[$name];
58
    }
59
60
    /**
61
     * @return array
62
     */
63
    public function getMap(): array
64
    {
65
        return $this->map;
66
    }
67
68
}
69