22
33namespace hiqdev \php \billing \product ;
44
5- use hiqdev \php \billing \product \behavior \InvalidBehaviorException ;
6- use hiqdev \php \billing \product \Exception \AggregateNotFoundException ;
7- use hiqdev \php \billing \product \invoice \InvalidRepresentationException ;
8- use hiqdev \php \billing \product \invoice \RepresentationInterface ;
9- use hiqdev \php \billing \product \price \PriceTypeDefinition ;
105use hiqdev \php \billing \product \quantity \QuantityFormatterInterface ;
11- use hiqdev \php \billing \product \quantity \QuantityFormatterNotFoundException ;
126use hiqdev \php \billing \product \quantity \FractionQuantityData ;
137use hiqdev \php \billing \product \behavior \BehaviorInterface ;
14- use hiqdev \php \billing \product \behavior \BehaviorNotFoundException ;
158use hiqdev \php \billing \product \trait \HasLock ;
16- use hiqdev \php \billing \type \Type ;
17- use hiqdev \php \billing \type \TypeInterface ;
189
1910class BillingRegistry implements BillingRegistryInterface
2011{
2112 use HasLock;
2213
2314 /** @var TariffTypeDefinitionInterface[] */
2415 private array $ tariffTypeDefinitions = [];
16+
2517 private bool $ locked = false ;
2618
19+ private BillingRegistryService $ service ;
20+
21+ public function __construct ()
22+ {
23+ $ this ->service = new BillingRegistryService ($ this );
24+ }
25+
2726 public function addTariffType (TariffTypeDefinitionInterface $ tariffTypeDefinition ): void
2827 {
2928 $ this ->ensureNotLocked ();
3029
3130 $ this ->tariffTypeDefinitions [] = $ tariffTypeDefinition ;
3231 }
3332
33+ public function getTariffTypeDefinitions (): array
34+ {
35+ return $ this ->tariffTypeDefinitions ;
36+ }
37+
3438 public function priceTypes (): \Generator
3539 {
36- foreach ($ this ->tariffTypeDefinitions as $ tariffTypeDefinition ) {
40+ foreach ($ this ->getTariffTypeDefinitions () as $ tariffTypeDefinition ) {
3741 foreach ($ tariffTypeDefinition ->withPrices () as $ priceTypeDefinition ) {
3842 yield $ priceTypeDefinition ;
3943 }
@@ -42,129 +46,42 @@ public function priceTypes(): \Generator
4246
4347 public function getRepresentationsByType (string $ representationClass ): array
4448 {
45- if (!class_exists ($ representationClass )) {
46- throw new InvalidRepresentationException ("Class ' $ representationClass' does not exist " );
47- }
48-
49- if (!is_subclass_of ($ representationClass , RepresentationInterface::class)) {
50- throw new InvalidBehaviorException (
51- sprintf ('Representation class "%s" does not implement RepresentationInterface ' , $ representationClass )
52- );
53- }
54-
55- $ representations = [];
56- foreach ($ this ->priceTypes () as $ priceTypeDefinition ) {
57- foreach ($ priceTypeDefinition ->documentRepresentation () as $ representation ) {
58- if ($ representation instanceof $ representationClass ) {
59- $ representations [] = $ representation ;
60- }
61- }
62- }
63-
64- return $ representations ;
49+ return $ this ->service ->getRepresentationsByType ($ representationClass );
6550 }
6651
6752 public function createQuantityFormatter (
6853 string $ type ,
6954 FractionQuantityData $ data ,
7055 ): QuantityFormatterInterface {
71- $ type = $ this ->convertStringTypeToType ($ type );
72-
73- foreach ($ this ->priceTypes () as $ priceTypeDefinition ) {
74- if ($ priceTypeDefinition ->hasType ($ type )) {
75- return $ priceTypeDefinition ->createQuantityFormatter ($ data );
76- }
77- }
78-
79- throw new QuantityFormatterNotFoundException ('Quantity formatter not found ' );
80- }
81-
82- private function convertStringTypeToType (string $ type ): TypeInterface
83- {
84- return Type::anyId ($ type );
56+ return $ this ->service ->createQuantityFormatter ($ type , $ data );
8557 }
8658
8759 public function getBehavior (string $ type , string $ behaviorClassWrapper ): BehaviorInterface
8860 {
89- if (!class_exists ($ behaviorClassWrapper )) {
90- throw new InvalidBehaviorException (
91- sprintf ('Behavior class "%s" does not exist ' , $ behaviorClassWrapper )
92- );
93- }
94-
95- if (!is_subclass_of ($ behaviorClassWrapper , BehaviorInterface::class)) {
96- throw new InvalidBehaviorException (
97- sprintf ('Behavior class "%s" does not implement BehaviorInterface ' , $ behaviorClassWrapper )
98- );
99- }
100-
101- $ billingType = $ this ->convertStringTypeToType ($ type );
102-
103- foreach ($ this ->priceTypes () as $ priceTypeDefinition ) {
104- if ($ priceTypeDefinition ->hasType ($ billingType )) {
105- $ behavior = $ this ->findBehaviorInPriceType ($ priceTypeDefinition , $ behaviorClassWrapper );
106-
107- if ($ behavior ) {
108- return $ behavior ;
109- }
110- }
111- }
112-
113- throw new BehaviorNotFoundException (
114- sprintf ('Behavior of class "%s" not found for type "%s" ' , $ behaviorClassWrapper , $ type ),
115- );
116- }
117-
118- private function findBehaviorInPriceType (
119- PriceTypeDefinition $ priceTypeDefinition ,
120- string $ behaviorClassWrapper
121- ): ?BehaviorInterface {
122- foreach ($ priceTypeDefinition ->withBehaviors () as $ behavior ) {
123- if ($ behavior instanceof $ behaviorClassWrapper ) {
124- return $ behavior ;
125- }
126- }
127-
128- return null ;
61+ return $ this ->service ->getBehavior ($ type , $ behaviorClassWrapper );
12962 }
13063
64+ /**
65+ * @inerhitDoc
66+ */
13167 public function getBehaviors (string $ behaviorClassWrapper ): \Generator
13268 {
133- foreach ($ this ->tariffTypeDefinitions as $ tariffTypeDefinition ) {
134- foreach ($ tariffTypeDefinition ->withBehaviors () as $ behavior ) {
135- if ($ behavior instanceof $ behaviorClassWrapper ) {
136- yield $ behavior ;
137- }
138- }
139- }
140-
141- foreach ($ this ->priceTypes () as $ priceTypeDefinition ) {
142- foreach ($ priceTypeDefinition ->withBehaviors () as $ behavior ) {
143- if ($ behavior instanceof $ behaviorClassWrapper ) {
144- yield $ behavior ;
145- }
146- }
147- }
69+ return $ this ->service ->getBehaviors ($ behaviorClassWrapper );
14870 }
14971
15072 public function getAggregate (string $ type ): AggregateInterface
15173 {
152- $ type = $ this ->convertStringTypeToType ($ type );
153-
154- foreach ($ this ->priceTypes () as $ priceTypeDefinition ) {
155- if ($ priceTypeDefinition ->hasType ($ type )) {
156- return $ priceTypeDefinition ->getAggregate ();
157- }
158- }
74+ return $ this ->service ->getAggregate ($ type );
75+ }
15976
160- throw new AggregateNotFoundException ('Aggregate was not found ' );
77+ public function findTariffTypeDefinitionByBehavior (BehaviorInterface $ behavior ): TariffTypeDefinitionInterface
78+ {
79+ return $ this ->service ->findTariffTypeDefinitionByBehavior ($ behavior );
16180 }
16281
163- public function getTariffTypeDefinitions ( ): \Generator
82+ public function findPriceTypeDefinitionsByBehavior ( string $ behaviorClassWrapper ): \Generator
16483 {
165- foreach ($ this ->tariffTypeDefinitions as $ tariffTypeDefinition ) {
166- yield $ tariffTypeDefinition ;
167- }
84+ return $ this ->service ->findPriceTypeDefinitionsByBehavior ($ behaviorClassWrapper );
16885 }
16986
17087 protected function afterLock (): void
0 commit comments