1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace hiqdev\php\billing\product\Application; |
4
|
|
|
|
5
|
|
|
use hiqdev\php\billing\product\AggregateInterface; |
6
|
|
|
use hiqdev\php\billing\product\behavior\BehaviorInterface; |
7
|
|
|
use hiqdev\php\billing\product\behavior\BehaviorNotFoundException; |
8
|
|
|
use hiqdev\php\billing\product\behavior\InvalidBehaviorException; |
9
|
|
|
use hiqdev\php\billing\product\BillingRegistryInterface; |
10
|
|
|
use hiqdev\php\billing\product\Exception\AggregateNotFoundException; |
11
|
|
|
use hiqdev\php\billing\product\Exception\TariffTypeDefinitionNotFoundException; |
12
|
|
|
use hiqdev\php\billing\product\invoice\InvalidRepresentationException; |
13
|
|
|
use hiqdev\php\billing\product\invoice\RepresentationInterface; |
14
|
|
|
use hiqdev\php\billing\product\price\PriceTypeDefinitionInterface; |
15
|
|
|
use hiqdev\php\billing\product\quantity\FractionQuantityData; |
16
|
|
|
use hiqdev\php\billing\product\quantity\QuantityFormatterInterface; |
17
|
|
|
use hiqdev\php\billing\product\quantity\QuantityFormatterNotFoundException; |
18
|
|
|
use hiqdev\php\billing\product\TariffTypeDefinitionInterface; |
19
|
|
|
use hiqdev\php\billing\type\Type; |
20
|
|
|
use hiqdev\php\billing\type\TypeInterface; |
|
|
|
|
21
|
|
|
|
22
|
|
|
final class BillingRegistryService implements BillingRegistryServiceInterface |
23
|
|
|
{ |
24
|
|
|
public function __construct(private readonly BillingRegistryInterface $registry) |
25
|
|
|
{ |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function getRepresentationsByType(string $representationClass): array |
29
|
|
|
{ |
30
|
|
|
if (!class_exists($representationClass) && !interface_exists($representationClass)) { |
31
|
|
|
throw new InvalidRepresentationException("Class '$representationClass' does not exist"); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if (class_exists($representationClass) |
35
|
|
|
&& !is_subclass_of($representationClass, RepresentationInterface::class) |
36
|
|
|
) { |
37
|
|
|
throw new InvalidBehaviorException( |
38
|
|
|
sprintf( |
39
|
|
|
'Representation class "%s" does not implement RepresentationInterface', |
40
|
|
|
$representationClass, |
41
|
|
|
) |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$representations = []; |
46
|
|
|
foreach ($this->registry->priceTypes() as $priceTypeDefinition) { |
47
|
|
|
foreach ($priceTypeDefinition->documentRepresentation() as $representation) { |
48
|
|
|
if ($representation instanceof $representationClass) { |
49
|
|
|
$representations[] = $representation; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $representations; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getTariffTypeDefinitionByName(string $tariffName): TariffTypeDefinitionInterface |
58
|
|
|
{ |
59
|
|
|
foreach ($this->registry->getTariffTypeDefinitions() as $tariffTypeDefinition) { |
60
|
|
|
if ($tariffTypeDefinition->tariffType()->equalsName($tariffName)) { |
61
|
|
|
return $tariffTypeDefinition; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
throw new TariffTypeDefinitionNotFoundException('Tariff type definition was not found'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function createQuantityFormatter(string $type, FractionQuantityData $data): QuantityFormatterInterface { |
69
|
|
|
$type = $this->convertStringTypeToType($type); |
70
|
|
|
|
71
|
|
|
foreach ($this->registry->priceTypes() as $priceTypeDefinition) { |
72
|
|
|
if ($priceTypeDefinition->hasType($type)) { |
73
|
|
|
return $priceTypeDefinition->createQuantityFormatter($data); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
throw new QuantityFormatterNotFoundException('Quantity formatter not found'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function convertStringTypeToType(string $type): TypeInterface |
81
|
|
|
{ |
82
|
|
|
return Type::anyId($type); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getBehavior(string $type, string $behaviorClassWrapper): BehaviorInterface |
86
|
|
|
{ |
87
|
|
|
if (!class_exists($behaviorClassWrapper)) { |
88
|
|
|
throw new InvalidBehaviorException( |
89
|
|
|
sprintf('Behavior class "%s" does not exist', $behaviorClassWrapper) |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (!is_subclass_of($behaviorClassWrapper, BehaviorInterface::class)) { |
94
|
|
|
throw new InvalidBehaviorException( |
95
|
|
|
sprintf('Behavior class "%s" does not implement BehaviorInterface', $behaviorClassWrapper) |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$billingType = $this->convertStringTypeToType($type); |
100
|
|
|
|
101
|
|
|
foreach ($this->registry->priceTypes() as $priceTypeDefinition) { |
102
|
|
|
if ($priceTypeDefinition->hasType($billingType)) { |
103
|
|
|
$behavior = $this->findBehaviorInPriceType($priceTypeDefinition, $behaviorClassWrapper); |
|
|
|
|
104
|
|
|
|
105
|
|
|
if ($behavior) { |
106
|
|
|
return $behavior; |
|
|
|
|
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
throw new BehaviorNotFoundException( |
112
|
|
|
sprintf('Behavior of class "%s" not found for type "%s"', $behaviorClassWrapper, $type), |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
private function findBehaviorInPriceType( |
117
|
|
|
PriceTypeDefinitionInterface $priceTypeDefinition, |
118
|
|
|
string $behaviorClassWrapper |
119
|
|
|
): ?BehaviorInterface { |
120
|
|
|
foreach ($priceTypeDefinition->withBehaviors() as $behavior) { |
121
|
|
|
if ($behavior instanceof $behaviorClassWrapper) { |
122
|
|
|
return $behavior; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return null; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function getBehaviors(string $behaviorClassWrapper): \Generator |
130
|
|
|
{ |
131
|
|
|
foreach ($this->registry->getTariffTypeDefinitions() as $tariffTypeDefinition) { |
132
|
|
|
foreach ($tariffTypeDefinition->withBehaviors() as $behavior) { |
133
|
|
|
if ($behavior instanceof $behaviorClassWrapper) { |
134
|
|
|
yield $behavior; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
foreach ($this->registry->priceTypes() as $priceTypeDefinition) { |
140
|
|
|
foreach ($priceTypeDefinition->withBehaviors() as $behavior) { |
141
|
|
|
if ($behavior instanceof $behaviorClassWrapper) { |
142
|
|
|
yield $behavior; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function getAggregate(string $type): AggregateInterface |
149
|
|
|
{ |
150
|
|
|
$type = $this->convertStringTypeToType($type); |
151
|
|
|
|
152
|
|
|
foreach ($this->registry->priceTypes() as $priceTypeDefinition) { |
153
|
|
|
if ($priceTypeDefinition->hasType($type)) { |
154
|
|
|
return $priceTypeDefinition->getAggregate(); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
throw new AggregateNotFoundException('Aggregate was not found'); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function findPriceTypeDefinitionsByBehavior(string $behaviorClassWrapper): \Generator |
162
|
|
|
{ |
163
|
|
|
foreach ($this->registry->priceTypes() as $priceTypeDefinition) { |
164
|
|
|
if ($priceTypeDefinition->hasBehavior($behaviorClassWrapper)) { |
165
|
|
|
yield $priceTypeDefinition; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths