Skip to content

Commit c650bfe

Browse files
ValeriyShnurovoyDrahma
andauthored
HP-2496/Added methods findBehaviorByClass, findPricesByTypeName and u… (#103)
* HP-2496/Added methods findBehaviorByClass, findPricesByTypeName and unit tests for them * HP-2496/Removed not used namespaces * HP-2496/Fixed after review --------- Co-authored-by: Drahma <[email protected]>
1 parent 354dcdc commit c650bfe

File tree

5 files changed

+52
-0
lines changed

5 files changed

+52
-0
lines changed

src/product/TariffTypeDefinition.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Google\Service\TPU;
66
use hiqdev\php\billing\product\behavior\BehaviorCollectionInterface;
7+
use hiqdev\php\billing\product\behavior\BehaviorInterface;
78
use hiqdev\php\billing\product\behavior\BehaviorTariffTypeCollection;
89
use hiqdev\php\billing\product\behavior\TariffTypeBehaviorRegistry;
910
use hiqdev\php\billing\product\Domain\Model\TariffTypeInterface;
@@ -12,6 +13,7 @@
1213
use hiqdev\php\billing\product\price\PriceTypeDefinitionCollection;
1314
use hiqdev\php\billing\product\price\PriceTypeDefinitionCollectionInterface;
1415
use hiqdev\php\billing\product\price\PriceTypeDefinitionFactory;
16+
use hiqdev\php\billing\product\price\PriceTypeDefinitionInterface;
1517
use hiqdev\php\billing\product\trait\HasLock;
1618

1719
/**
@@ -87,6 +89,24 @@ public function withPrices()
8789
return $this->prices;
8890
}
8991

92+
public function findPricesByTypeName(string $typeName): ?array
93+
{
94+
$prices = null;
95+
$this->ensureNotLocked();
96+
97+
foreach ($this->prices as $price) {
98+
if ($this->matchesPriceType($price, $typeName)) {
99+
$prices[] = $price;
100+
}
101+
}
102+
return $prices;
103+
}
104+
105+
private function matchesPriceType(PriceTypeDefinitionInterface $price, string $typeName): bool
106+
{
107+
return str_ends_with($price->type()->getName(), ",$typeName");
108+
}
109+
90110
/**
91111
* @return BehaviorCollectionInterface<TariffTypeDefinition>
92112
* @psalm-suppress ImplementedReturnTypeMismatch
@@ -105,6 +125,11 @@ public function hasBehavior(string $behaviorClassName): bool
105125
return $this->tariffTypeBehaviorRegistry->hasBehavior($behaviorClassName);
106126
}
107127

128+
public function findBehaviorByClass(string $class): ?BehaviorInterface
129+
{
130+
return $this->tariffTypeBehaviorRegistry->findBehaviorByClass($class);
131+
}
132+
108133
public function end(): TariffTypeDefinitionInterface
109134
{
110135
$this->ensureProductExists();

src/product/TariffTypeDefinitionInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,6 @@ public function setPricesSuggester(string $suggesterClass): static;
3737
public function withPrices();
3838

3939
public function end();
40+
41+
public function findPricesByTypeName(string $typeName): ?array;
4042
}

src/product/behavior/HasBehaviorsInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ interface HasBehaviorsInterface
1818
public function withBehaviors();
1919

2020
public function hasBehavior(string $behaviorClassName): bool;
21+
22+
public function findBehaviorByClass(string $class): ?BehaviorInterface;
2123
}

src/product/behavior/TariffTypeBehaviorRegistry.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ public function hasBehavior(string $behaviorClassName): bool
6060
return false;
6161
}
6262

63+
public function findBehaviorByClass(string $class): ?BehaviorInterface
64+
{
65+
foreach ($this->getBehaviors() as $behavior) {
66+
if ($behavior instanceof $class) {
67+
return $behavior;
68+
}
69+
}
70+
71+
return null;
72+
}
73+
6374
public function lock(): void
6475
{
6576
$this->behaviorCollection->lock();

src/product/price/PriceTypeDefinition.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use hiqdev\php\billing\product\AggregateInterface;
77
use hiqdev\php\billing\product\behavior\BehaviorCollectionInterface;
8+
use hiqdev\php\billing\product\behavior\BehaviorInterface;
89
use hiqdev\php\billing\product\behavior\HasBehaviorsInterface;
910
use hiqdev\php\billing\product\Exception\AggregateNotDefinedException;
1011
use hiqdev\php\billing\product\behavior\BehaviorPriceTypeDefinitionCollection;
@@ -194,6 +195,17 @@ public function hasBehavior(string $behaviorClassName): bool
194195
return false;
195196
}
196197

198+
public function findBehaviorByClass(string $class): ?BehaviorInterface
199+
{
200+
foreach ($this->behaviorCollection as $behavior) {
201+
if ($behavior instanceof $class) {
202+
return $behavior;
203+
}
204+
}
205+
206+
return null;
207+
}
208+
197209
/**
198210
* @inerhitDoc
199211
*/

0 commit comments

Comments
 (0)