TargetFactory   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Test Coverage

Coverage 52.38%

Importance

Changes 9
Bugs 0 Features 5
Metric Value
wmc 8
eloc 64
c 9
b 0
f 5
dl 0
loc 115
ccs 11
cts 21
cp 0.5238
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getClassForType() 0 73 2
A shortenType() 0 3 1
A getEntityClassName() 0 3 1
A create() 0 10 2
A parseType() 0 7 2
1
<?php
2
/**
3
 * API for Billing
4
 *
5
 * @link      https://github.com/hiqdev/billing-hiapi
6
 * @package   billing-hiapi
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\billing\hiapi\target;
12
13
use hiqdev\billing\hiapi\target\certificate\CertificateTarget;
14
use hiqdev\billing\hiapi\target\client\ClientTarget;
15
use hiqdev\billing\hiapi\target\device\PixCdnTarget;
16
use hiqdev\billing\hiapi\target\device\ServerTarget;
17
use hiqdev\billing\hiapi\target\device\SwitchTarget;
18
use hiqdev\billing\hiapi\target\device\VideoCdnTarget;
19
use hiqdev\billing\hiapi\target\domain\DomainZoneTarget;
20
use hiqdev\billing\hiapi\target\domain\DomainTarget;
21
use hiqdev\billing\hiapi\target\modelGroup\ModelGroupTarget;
22
use hiqdev\billing\hiapi\target\part\PartTarget;
23
use hiqdev\billing\hiapi\target\ref\RefTarget;
24
use hiqdev\billing\hiapi\target\tariff\PcdnTariffTarget;
25
use hiqdev\billing\hiapi\target\tariff\ServerTariffTarget;
26
use hiqdev\billing\hiapi\target\tariff\CertificateTariffTarget;
27
use hiqdev\billing\hiapi\target\tariff\DomainTariffTarget;
28
use hiqdev\billing\hiapi\target\tariff\SwitchTariffTarget;
29
use hiqdev\billing\hiapi\target\tariff\TariffTarget;
30
use hiqdev\billing\hiapi\target\tariff\TemplateTariffTarget;
31
use hiqdev\billing\hiapi\target\tariff\VcdnTariffTarget;
32
use hiqdev\php\billing\target\Target;
33
use hiqdev\php\billing\target\TargetCreationDto;
34
use hiqdev\php\billing\target\TargetFactoryInterface;
35
use hiqdev\php\units\exceptions\InvalidConfigException;
36
37
class TargetFactory implements TargetFactoryInterface
38
{
39
    public function getEntityClassName(): string
40
    {
41
        return Target::class;
42
    }
43
44
    /**
45
     * @return Target|null
46
     */
47
    public function create(TargetCreationDto $dto): ?Target
48
    {
49
        if (!isset($dto->type)) {
50
            $class = Target::class;
51
        } else {
52
            $class = $this->getClassForType($dto->type);
53
            $dto->type = $this->shortenType($dto->type);
54
        }
55
56
        return new $class($dto->id, $dto->type, $dto->name);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61 6
     */
62
    public function shortenType(string $type): string
63 6
    {
64
        return $this->parseType($type)[0];
65
    }
66 6
67
    protected function parseType(string $type): array
68 6
    {
69
        if (strpos($type, '.') !== false) {
70
            return explode('.', $type, 2);
71
        }
72 6
73
        return [$type, '*'];
74
    }
75
76
    /**
77
     * {@inheritdoc}
78 5
     */
79
    public function getClassForType(string $type): string
80
    {
81 5
        $map = [
82
            'device' => [
83
                'cdn' => VideoCdnTarget::class,
84
                'cloudservice' => VideoCdnTarget::class,
85
                'cdnpix' => PixCdnTarget::class,
86
                'net' => SwitchTarget::class,
87
                'rack' => SwitchTarget::class,
88
                'cable_organizer' => SwitchTarget::class,
89
                'console' => SwitchTarget::class,
90
                'ipmi' => SwitchTarget::class,
91
                'pdu' => SwitchTarget::class,
92
                '*' => ServerTarget::class,
93
            ],
94
            'serverConfig' => [
95
                '*' => Target::class,
96
            ],
97
            'part' => [
98
                '*' => PartTarget::class,
99
            ],
100
            'server' => [
101
                '*' => ServerTarget::class,
102
            ],
103
            'certificate' => [
104
                '*' => CertificateTarget::class,
105
            ],
106
            'ref' => [
107
                '*' => RefTarget::class,
108
            ],
109
            'tariff' => [
110
                'vcdn' => VcdnTariffTarget::class,
111
                'pcdn' => PcdnTariffTarget::class,
112
                'server' => ServerTariffTarget::class,
113
                'template' => TemplateTariffTarget::class,
114
                'certificate' => CertificateTariffTarget::class,
115
                'domain' => DomainTariffTarget::class,
116
                'switch' => SwitchTariffTarget::class,
117
                '*' => TariffTarget::class,
118
            ],
119
            'client' => [
120
                '*' => ClientTarget::class,
121
            ],
122
            'account' => [
123
                '*' => ServerTarget::class,
124
            ],
125
            'model_group' => [
126
                '*' => ModelGroupTarget::class,
127
            ],
128
            'type' => [
129
                '*' => Target::class,
130
            ],
131
            '-1' => [
132
                '*' => Target::class,
133
            ],
134
            'feature' => [
135
                '*' => Target::class,
136
            ],
137
            'domain' => [
138
                '*' => DomainTarget::class,
139
            ],
140
            'zone' => [
141
                '*' => DomainZoneTarget::class,
142
            ],
143
        ];
144 5
145 5
        [$type, $subtype] = $this->parseType($type);
146 5
        $class = $map[$type][$subtype] ?? $map[$type]['*'] ?? null;
147
        if ($class === null) {
148
            throw new InvalidConfigException("No class for type '$type'");
149
        }
150 5
151
        return $class;
152
    }
153
}
154