|
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\device\ServerTarget; |
|
15
|
|
|
use hiqdev\billing\hiapi\target\modelGroup\ModelGroupTarget; |
|
16
|
|
|
use hiqdev\billing\hiapi\target\part\PartTarget; |
|
17
|
|
|
use hiqdev\billing\hiapi\target\ref\RefTarget; |
|
18
|
|
|
use hiqdev\billing\hiapi\target\tariff\ServerTariffTarget; |
|
19
|
|
|
use hiqdev\billing\hiapi\target\tariff\TariffTarget; |
|
20
|
|
|
use hiqdev\billing\hiapi\target\tariff\TemplateTariffTarget; |
|
21
|
|
|
use hiqdev\php\billing\target\Target; |
|
22
|
|
|
use hiqdev\php\billing\target\TargetCreationDto; |
|
23
|
|
|
use hiqdev\php\billing\target\TargetFactoryInterface; |
|
24
|
|
|
use hiqdev\php\units\exceptions\InvalidConfigException; |
|
25
|
|
|
|
|
26
|
|
|
class TargetFactory implements TargetFactoryInterface |
|
27
|
|
|
{ |
|
28
|
|
|
public function getEntityClassName(): string |
|
29
|
|
|
{ |
|
30
|
|
|
return Target::class; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @return Target|null |
|
35
|
|
|
*/ |
|
36
|
|
|
public function create(TargetCreationDto $dto): ?Target |
|
37
|
|
|
{ |
|
38
|
|
|
if (!isset($dto->type)) { |
|
39
|
|
|
$class = Target::class; |
|
40
|
|
|
} else { |
|
41
|
|
|
$class = $this->getClassForType($dto->type); |
|
42
|
|
|
$dto->type = $this->shortenType($dto->type); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return new $class($dto->id, $dto->type, $dto->name); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
1 |
|
public function shortenType(string $type): string |
|
52
|
|
|
{ |
|
53
|
1 |
|
return $this->parseType($type)[0]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
protected function parseType(string $type): array |
|
57
|
|
|
{ |
|
58
|
1 |
|
if (strpos($type, '.') !== false) { |
|
59
|
|
|
return explode('.', $type, 2); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
return [$type, '*']; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getClassForType(string $type): string |
|
69
|
|
|
{ |
|
70
|
|
|
$map = [ |
|
71
|
|
|
'device' => [ |
|
72
|
|
|
'*' => ServerTarget::class, |
|
73
|
|
|
], |
|
74
|
|
|
'part' => [ |
|
75
|
|
|
'*' => PartTarget::class, |
|
76
|
|
|
], |
|
77
|
|
|
'server' => [ |
|
78
|
|
|
'*' => ServerTarget::class, |
|
79
|
|
|
], |
|
80
|
|
|
'certificate' => [ |
|
81
|
|
|
'*' => CertificateTarget::class, |
|
82
|
|
|
], |
|
83
|
|
|
'ref' => [ |
|
84
|
|
|
'*' => RefTarget::class, |
|
85
|
|
|
], |
|
86
|
|
|
'tariff' => [ |
|
87
|
|
|
'server' => ServerTariffTarget::class, |
|
88
|
|
|
'template' => TemplateTariffTarget::class, |
|
89
|
|
|
'*' => TariffTarget::class, |
|
90
|
|
|
], |
|
91
|
|
|
'client' => [ |
|
92
|
|
|
'*' => ServerTarget::class, |
|
93
|
|
|
], |
|
94
|
|
|
'account' => [ |
|
95
|
|
|
'*' => ServerTarget::class, |
|
96
|
|
|
], |
|
97
|
|
|
'model_group' => [ |
|
98
|
|
|
'*' => ModelGroupTarget::class, |
|
99
|
|
|
], |
|
100
|
|
|
'-1' => [ |
|
101
|
|
|
'*' => Target::class, |
|
102
|
|
|
], |
|
103
|
|
|
]; |
|
104
|
|
|
|
|
105
|
|
|
[$type, $subtype] = $this->parseType($type); |
|
|
|
|
|
|
106
|
|
|
$class = $map[$type][$subtype] ?? $map[$type]['*'] ?? null; |
|
107
|
|
|
if ($class === null) { |
|
108
|
|
|
throw new InvalidConfigException('No class for type ' . $type); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $class; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.