|
2 | 2 |
|
3 | 3 | namespace hiqdev\php\billing\product; |
4 | 4 |
|
| 5 | +use hiqdev\php\billing\product\behavior\InvalidBehaviorException; |
5 | 6 | use hiqdev\php\billing\product\invoice\RepresentationInterface; |
| 7 | +use hiqdev\php\billing\product\price\PriceTypeDefinition; |
6 | 8 | use hiqdev\php\billing\product\quantity\QuantityFormatterNotFoundException; |
7 | 9 | use hiqdev\php\billing\product\quantity\FractionQuantityData; |
8 | 10 | use hiqdev\php\billing\product\behavior\BehaviorInterface; |
@@ -82,22 +84,44 @@ private function convertStringTypeToType(string $type): TypeInterface |
82 | 84 | * @param string $behaviorClassWrapper |
83 | 85 | * @return BehaviorInterface |
84 | 86 | * @throws BehaviorNotFoundException |
| 87 | + * @throws InvalidBehaviorException |
85 | 88 | */ |
86 | 89 | public function getBehavior(string $type, string $behaviorClassWrapper): BehaviorInterface |
87 | 90 | { |
88 | | - $type = $this->convertStringTypeToType($type); |
| 91 | + if (!class_exists($behaviorClassWrapper)) { |
| 92 | + throw new InvalidBehaviorException( |
| 93 | + sprintf('Behavior class "%s" does not exist', $behaviorClassWrapper) |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + $billingType = $this->convertStringTypeToType($type); |
89 | 98 |
|
90 | 99 | foreach ($this->priceTypes() as $priceTypeDefinition) { |
91 | | - if ($priceTypeDefinition->hasType($type)) { |
92 | | - foreach ($priceTypeDefinition->withBehaviors() as $behavior) { |
93 | | - if ($behavior instanceof $behaviorClassWrapper) { |
94 | | - return $behavior; |
95 | | - } |
| 100 | + if ($priceTypeDefinition->hasType($billingType)) { |
| 101 | + $behavior = $this->findBehaviorInPriceType($priceTypeDefinition, $behaviorClassWrapper); |
| 102 | + |
| 103 | + if ($behavior) { |
| 104 | + return $behavior; |
96 | 105 | } |
97 | 106 | } |
98 | 107 | } |
99 | 108 |
|
100 | | - throw new BehaviorNotFoundException('Behaviour was not found'); |
| 109 | + throw new BehaviorNotFoundException( |
| 110 | + sprintf('Behavior of class "%s" not found for type "%s"', $behaviorClassWrapper, $type), |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + private function findBehaviorInPriceType( |
| 115 | + PriceTypeDefinition $priceTypeDefinition, |
| 116 | + string $behaviorClassWrapper |
| 117 | + ): ?BehaviorInterface { |
| 118 | + foreach ($priceTypeDefinition->withBehaviors() as $behavior) { |
| 119 | + if ($behavior instanceof $behaviorClassWrapper) { |
| 120 | + return $behavior; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return null; |
101 | 125 | } |
102 | 126 |
|
103 | 127 | public function getBehaviors(string $behaviorClassWrapper): \Generator |
|
0 commit comments