diff --git a/src/product/TariffTypeDefinition.php b/src/product/TariffTypeDefinition.php index 789c6b7d..730517eb 100644 --- a/src/product/TariffTypeDefinition.php +++ b/src/product/TariffTypeDefinition.php @@ -2,19 +2,14 @@ namespace hiqdev\php\billing\product; -use Google\Service\TPU; use hiqdev\php\billing\product\behavior\BehaviorCollectionInterface; -use hiqdev\php\billing\product\behavior\BehaviorInterface; -use hiqdev\php\billing\product\behavior\BehaviorTariffTypeCollection; use hiqdev\php\billing\product\behavior\TariffTypeBehaviorRegistry; use hiqdev\php\billing\product\Domain\Model\TariffTypeInterface; use hiqdev\php\billing\product\Exception\ProductNotDefinedException; -use hiqdev\php\billing\product\price\PriceTypeDefinition; use hiqdev\php\billing\product\price\PriceTypeDefinitionCollection; -use hiqdev\php\billing\product\price\PriceTypeDefinitionCollectionInterface; use hiqdev\php\billing\product\price\PriceTypeDefinitionFactory; -use hiqdev\php\billing\product\price\PriceTypeDefinitionInterface; use hiqdev\php\billing\product\trait\HasLock; +use LogicException; /** * @template TPriceTypeDefinitionCollection of PriceTypeDefinitionCollection @@ -89,24 +84,6 @@ public function withPrices() return $this->prices; } - public function findPricesByTypeName(string $typeName): ?array - { - $prices = null; - $this->ensureNotLocked(); - - foreach ($this->prices as $price) { - if ($this->matchesPriceType($price, $typeName)) { - $prices[] = $price; - } - } - return $prices; - } - - private function matchesPriceType(PriceTypeDefinitionInterface $price, string $typeName): bool - { - return str_ends_with($price->type()->getName(), ",$typeName"); - } - /** * @return BehaviorCollectionInterface * @psalm-suppress ImplementedReturnTypeMismatch @@ -117,7 +94,7 @@ public function withBehaviors() { $this->ensureNotLocked(); - return $this->tariffTypeBehaviorRegistry->getBehaviors(); + return $this->tariffTypeBehaviorRegistry->withBehaviors(); } public function hasBehavior(string $behaviorClassName): bool @@ -125,7 +102,7 @@ public function hasBehavior(string $behaviorClassName): bool return $this->tariffTypeBehaviorRegistry->hasBehavior($behaviorClassName); } - public function findBehaviorByClass(string $class): ?BehaviorInterface + public function findBehaviorByClass(string $class) { return $this->tariffTypeBehaviorRegistry->findBehaviorByClass($class); } @@ -136,7 +113,7 @@ public function end(): TariffTypeDefinitionInterface // Validate prices configuration is complete if ($this->prices->count() === 0) { - throw new \LogicException('At least one price type must be defined'); + throw new LogicException('At least one price type must be defined'); } return $this; diff --git a/src/product/TariffTypeDefinitionInterface.php b/src/product/TariffTypeDefinitionInterface.php index b39bd1fa..f6e7a847 100644 --- a/src/product/TariffTypeDefinitionInterface.php +++ b/src/product/TariffTypeDefinitionInterface.php @@ -37,6 +37,4 @@ public function setPricesSuggester(string $suggesterClass): static; public function withPrices(); public function end(); - - public function findPricesByTypeName(string $typeName): ?array; } diff --git a/src/product/behavior/BehaviorRegistry.php b/src/product/behavior/BehaviorRegistry.php new file mode 100644 index 00000000..dfd73b1a --- /dev/null +++ b/src/product/behavior/BehaviorRegistry.php @@ -0,0 +1,41 @@ +withBehaviors() as $behavior) { + if ($behavior instanceof $behaviorClassName) { + return true; + } + } + + return false; + } + + public function findBehaviorByClass(string $class) + { + foreach ($this->withBehaviors() as $behavior) { + if ($behavior instanceof $class) { + return $behavior; + } + } + + return null; + } + + public function lock(): void + { + $this->withBehaviors()->lock(); + } +} diff --git a/src/product/behavior/HasBehaviorsInterface.php b/src/product/behavior/HasBehaviorsInterface.php index a71a3661..204cf893 100644 --- a/src/product/behavior/HasBehaviorsInterface.php +++ b/src/product/behavior/HasBehaviorsInterface.php @@ -2,7 +2,6 @@ namespace hiqdev\php\billing\product\behavior; -use hiqdev\php\billing\product\invoice\RepresentationCollection; use hiqdev\php\billing\product\price\PriceTypeDefinitionInterface; use hiqdev\php\billing\product\TariffTypeDefinitionInterface; @@ -19,5 +18,10 @@ public function withBehaviors(); public function hasBehavior(string $behaviorClassName): bool; - public function findBehaviorByClass(string $class): ?BehaviorInterface; + /** + * @template TBehavior of object + * @param class-string $class + * @return TBehavior|null + */ + public function findBehaviorByClass(string $class); } diff --git a/src/product/behavior/PriceTypeBehaviorRegistry.php b/src/product/behavior/PriceTypeBehaviorRegistry.php new file mode 100644 index 00000000..cc09de2b --- /dev/null +++ b/src/product/behavior/PriceTypeBehaviorRegistry.php @@ -0,0 +1,40 @@ + + */ + private BehaviorPriceTypeDefinitionCollection $behaviorCollection; + + /** + * @psalm-param T $priceTypeDefinition + */ + public function __construct(PriceTypeDefinitionInterface $priceTypeDefinition, TariffTypeInterface $tariffType) + { + $this->behaviorCollection = new BehaviorPriceTypeDefinitionCollection($priceTypeDefinition, $tariffType); + } + + /** + * @return BehaviorPriceTypeDefinitionCollection + */ + public function withBehaviors(): BehaviorPriceTypeDefinitionCollection + { + return $this->behaviorCollection; + } + + protected function getBehaviorCollection(): BehaviorCollectionInterface + { + return $this->behaviorCollection; + } +} diff --git a/src/product/behavior/TariffTypeBehaviorRegistry.php b/src/product/behavior/TariffTypeBehaviorRegistry.php index f2095db9..1644bb16 100644 --- a/src/product/behavior/TariffTypeBehaviorRegistry.php +++ b/src/product/behavior/TariffTypeBehaviorRegistry.php @@ -26,7 +26,7 @@ * * @template-covariant T of TariffTypeDefinitionInterface */ -final class TariffTypeBehaviorRegistry implements HasLockInterface +final class TariffTypeBehaviorRegistry extends BehaviorRegistry { /** * @var BehaviorTariffTypeCollection @@ -44,35 +44,13 @@ public function __construct(TariffTypeDefinitionInterface $tariffTypeDefinition, /** * @return BehaviorTariffTypeCollection */ - public function getBehaviors(): BehaviorTariffTypeCollection + public function withBehaviors(): BehaviorTariffTypeCollection { return $this->behaviorCollection; } - public function hasBehavior(string $behaviorClassName): bool + protected function getBehaviorCollection(): BehaviorCollectionInterface { - foreach ($this->behaviorCollection as $behavior) { - if ($behavior instanceof $behaviorClassName) { - return true; - } - } - - return false; - } - - public function findBehaviorByClass(string $class): ?BehaviorInterface - { - foreach ($this->getBehaviors() as $behavior) { - if ($behavior instanceof $class) { - return $behavior; - } - } - - return null; - } - - public function lock(): void - { - $this->behaviorCollection->lock(); + return $this->behaviorCollection; } } diff --git a/src/product/price/PriceTypeDefinition.php b/src/product/price/PriceTypeDefinition.php index 1c2a2770..1e3b4aa4 100644 --- a/src/product/price/PriceTypeDefinition.php +++ b/src/product/price/PriceTypeDefinition.php @@ -4,9 +4,8 @@ namespace hiqdev\php\billing\product\price; use hiqdev\php\billing\product\AggregateInterface; -use hiqdev\php\billing\product\behavior\BehaviorCollectionInterface; -use hiqdev\php\billing\product\behavior\BehaviorInterface; use hiqdev\php\billing\product\behavior\HasBehaviorsInterface; +use hiqdev\php\billing\product\behavior\PriceTypeBehaviorRegistry; use hiqdev\php\billing\product\Exception\AggregateNotDefinedException; use hiqdev\php\billing\product\behavior\BehaviorPriceTypeDefinitionCollection; use hiqdev\php\billing\product\invoice\RepresentationCollection; @@ -22,6 +21,7 @@ use hiqdev\php\billing\product\TariffTypeDefinitionInterface; use hiqdev\php\billing\product\trait\HasLock; use hiqdev\php\billing\type\TypeInterface; +use function class_exists; /** * @template TParentCollection @@ -46,16 +46,13 @@ class PriceTypeDefinition implements PriceTypeDefinitionInterface */ private RepresentationCollection $representationCollection; - /** - * @var BehaviorPriceTypeDefinitionCollection - */ - private BehaviorPriceTypeDefinitionCollection $behaviorCollection; - private ?AggregateInterface $aggregate = null; /** @psalm-var TParentCollection */ private readonly PriceTypeDefinitionCollectionInterface $parent; + private readonly PriceTypeBehaviorRegistry $behaviorRegistry; + /** * @param TParentCollection $parent */ @@ -66,7 +63,7 @@ public function __construct( ) { $this->parent = $parent; $this->representationCollection = new RepresentationCollection($this); - $this->behaviorCollection = new BehaviorPriceTypeDefinitionCollection($this, $tariffType); + $this->behaviorRegistry = new PriceTypeBehaviorRegistry($this, $tariffType); $this->init(); } @@ -107,7 +104,7 @@ public function quantityFormatter(string $formatterClass, $fractionUnit = null): { $this->ensureNotLocked(); - if (!\class_exists($formatterClass)) { + if (!class_exists($formatterClass)) { throw new InvalidQuantityFormatterException("Formatter class $formatterClass does not exist"); } @@ -181,29 +178,17 @@ public function withBehaviors() { $this->ensureNotLocked(); - return $this->behaviorCollection; + return $this->behaviorRegistry->withBehaviors(); } public function hasBehavior(string $behaviorClassName): bool { - foreach ($this->behaviorCollection as $behavior) { - if ($behavior instanceof $behaviorClassName) { - return true; - } - } - - return false; + return $this->behaviorRegistry->hasBehavior($behaviorClassName); } - public function findBehaviorByClass(string $class): ?BehaviorInterface + public function findBehaviorByClass(string $class) { - foreach ($this->behaviorCollection as $behavior) { - if ($behavior instanceof $class) { - return $behavior; - } - } - - return null; + return $this->behaviorRegistry->findBehaviorByClass($class); } /** @@ -242,7 +227,7 @@ public function getQuantityFormatterDefinition(): ?QuantityFormatterDefinition protected function afterLock(): void { $this->representationCollection->lock(); - $this->behaviorCollection->lock(); + $this->behaviorRegistry->lock(); } public function getTariffTypeDefinition(): TariffTypeDefinitionInterface diff --git a/tests/unit/product/behavior/TariffTypeBehaviorRegistryTest.php b/tests/unit/product/behavior/TariffTypeBehaviorRegistryTest.php index b5c11406..f861388c 100644 --- a/tests/unit/product/behavior/TariffTypeBehaviorRegistryTest.php +++ b/tests/unit/product/behavior/TariffTypeBehaviorRegistryTest.php @@ -21,7 +21,7 @@ protected function setUp(): void public function testWithBehaviorsReturnsBehaviorCollection(): void { - $this->assertInstanceOf(BehaviorTariffTypeCollection::class, $this->manager->getBehaviors()); + $this->assertInstanceOf(BehaviorTariffTypeCollection::class, $this->manager->withBehaviors()); } public function testHasBehaviorReturnsFalseWhenBehaviorNotPresent(): void @@ -32,7 +32,7 @@ public function testHasBehaviorReturnsFalseWhenBehaviorNotPresent(): void public function testHasBehaviorReturnsTrueWhenBehaviorPresent(): void { $behavior = $this->createMock(TestBehavior::class); - $behaviorCollection = $this->manager->getBehaviors(); + $behaviorCollection = $this->manager->withBehaviors(); $behaviorCollection->attach($behavior); $this->assertTrue($this->manager->hasBehavior(TestBehavior::class));