PriceModelFactory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 59
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 16 5
A getMap() 0 4 1
A instantiate() 0 4 1
1
<?php
2
/**
3
 * Finance module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-finance
6
 * @package   hipanel-module-finance
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\models\factories;
12
13
use hipanel\helpers\StringHelper;
14
use hipanel\modules\finance\models\CertificatePrice;
15
use hipanel\modules\finance\models\DomainServicePrice;
16
use hipanel\modules\finance\models\DomainZonePrice;
17
use hipanel\modules\finance\models\OveruseServerPrice;
18
use hipanel\modules\finance\models\Price;
19
use hipanel\modules\finance\models\RatePrice;
20
use hipanel\modules\finance\models\TemplatePrice;
21
use yii\base\InvalidConfigException;
22
use yii\base\Model;
23
24
/**
25
 * Class PriceModelFactory builds objects, that must be children of [[Price]] class
26
 * using short class name. See definitions in $map property.
27
 *
28
 * @author Dmytro Naumenko <[email protected]>
29
 */
30
class PriceModelFactory
31
{
32
    /**
33
     * @var array
34
     */
35
    protected $map = [
36
        'Price' => Price::class,
37
        'RatePrice' => RatePrice::class,
38
        'TemplatePrice' => TemplatePrice::class,
39
        'RateTemplatePrice' => TemplatePrice::class,
40
        'OveruseServerPrice' => OveruseServerPrice::class,
41
        'SinglePrice' => [
42
            'domain,*' => DomainZonePrice::class,
43
            'feature,*' => DomainServicePrice::class,
44
            'overuse,server_traf95_max' => OveruseServerPrice::class,
45
            '*' => Price::class,
46
        ],
47
        'CertificatePrice' => CertificatePrice::class,
48
        'EnumPrice' => CertificatePrice::class,
49
        'DomainZonePrice' => DomainZonePrice::class,
50
        'DomainServicePrice' => DomainServicePrice::class,
51
    ];
52
53
    /**
54
     * @param string $className
55
     * @param string|null $priceType
56
     * @throws InvalidConfigException
57
     * @return Price
58
     */
59
    public function build(string $className, ?string $priceType = null)
60
    {
61
        if (!isset($this->map[$className])) {
62
            throw new InvalidConfigException('Can not create model for class ' . $className);
63
        }
64
65
        if (is_array($this->map[$className])) {
66
            foreach ($this->map[$className] as $type => $class) {
67
                if (StringHelper::matchWildcard($type, (string) $priceType)) {
68
                    return new $class();
69
                }
70
            }
71
        }
72
73
        return new $this->map[$className]();
74
    }
75
76
    /**
77
     * @return array
78
     */
79
    public function getMap(): array
80
    {
81
        return $this->map;
82
    }
83
84
    public function instantiate(string $className): Model
85
    {
86
        return new $className();
87
    }
88
}
89