99
1010class TariffTypeDefinition implements TariffTypeDefinitionInterface
1111{
12+ private bool $ locked = false ;
13+
1214 private ?ProductInterface $ product = null ;
1315
1416 private PriceTypeDefinitionCollection $ prices ;
@@ -28,33 +30,52 @@ public function tariffType(): TariffTypeInterface
2830
2931 public function ofProduct (ProductInterface $ product ): TariffTypeDefinitionInterface
3032 {
33+ $ this ->ensureNotLocked ();
3134 $ this ->product = $ product ;
3235
3336 return $ this ;
3437 }
3538
39+ private function ensureNotLocked (): void
40+ {
41+ if ($ this ->locked ) {
42+ throw new \LogicException ('Modifications are not allowed after calling end(). ' );
43+ }
44+ }
45+
3646 public function getProduct (): ProductInterface
47+ {
48+ $ this ->ensureProductExists ();
49+
50+ return $ this ->product ;
51+ }
52+
53+ private function ensureProductExists (): void
3754 {
3855 if ($ this ->product === null ) {
3956 throw new ProductNotDefinedException ('Product is not set. Call the ofProduct() method first. ' );
4057 }
41-
42- return $ this ->product ;
4358 }
4459
4560 public function setPricesSuggester (string $ suggesterClass ): TariffTypeDefinitionInterface
4661 {
62+ $ this ->ensureNotLocked ();
63+
4764 // Validate or store the suggester class
4865 return $ this ;
4966 }
5067
5168 public function withPrices (): PriceTypeDefinitionCollection
5269 {
70+ $ this ->ensureNotLocked ();
71+
5372 return $ this ->prices ;
5473 }
5574
5675 public function withBehaviors (): BehaviorTariffTypeCollection
5776 {
77+ $ this ->ensureNotLocked ();
78+
5879 return $ this ->behaviorCollection ;
5980 }
6081
@@ -71,7 +92,16 @@ public function hasBehavior(string $behaviorClassName): bool
7192
7293 public function end (): TariffTypeDefinitionInterface
7394 {
74- // Validate the TariffType and lock its state
95+ $ this ->ensureProductExists ();
96+
97+ // Validate prices configuration is complete
98+ if ($ this ->prices ->count () === 0 ) {
99+ throw new \LogicException ('At least one price type must be defined ' );
100+ }
101+
102+ // Lock the state to prevent further modifications
103+ $ this ->locked = true ;
104+
75105 return $ this ;
76106 }
77107}
0 commit comments