Skip to content

Commit 79b6e2c

Browse files
Merge pull request #104 from VadymHrechukha/HP-2496_extend_tarifftypedefinition_with_getbehavior_method
HP-2496: Extend TariffTypeDefinition with findBehaviorByClass method
2 parents c650bfe + 9f10707 commit 79b6e2c

File tree

8 files changed

+108
-85
lines changed

8 files changed

+108
-85
lines changed

src/product/TariffTypeDefinition.php

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,14 @@
22

33
namespace hiqdev\php\billing\product;
44

5-
use Google\Service\TPU;
65
use hiqdev\php\billing\product\behavior\BehaviorCollectionInterface;
7-
use hiqdev\php\billing\product\behavior\BehaviorInterface;
8-
use hiqdev\php\billing\product\behavior\BehaviorTariffTypeCollection;
96
use hiqdev\php\billing\product\behavior\TariffTypeBehaviorRegistry;
107
use hiqdev\php\billing\product\Domain\Model\TariffTypeInterface;
118
use hiqdev\php\billing\product\Exception\ProductNotDefinedException;
12-
use hiqdev\php\billing\product\price\PriceTypeDefinition;
139
use hiqdev\php\billing\product\price\PriceTypeDefinitionCollection;
14-
use hiqdev\php\billing\product\price\PriceTypeDefinitionCollectionInterface;
1510
use hiqdev\php\billing\product\price\PriceTypeDefinitionFactory;
16-
use hiqdev\php\billing\product\price\PriceTypeDefinitionInterface;
1711
use hiqdev\php\billing\product\trait\HasLock;
12+
use LogicException;
1813

1914
/**
2015
* @template TPriceTypeDefinitionCollection of PriceTypeDefinitionCollection
@@ -89,24 +84,6 @@ public function withPrices()
8984
return $this->prices;
9085
}
9186

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-
11087
/**
11188
* @return BehaviorCollectionInterface<TariffTypeDefinition>
11289
* @psalm-suppress ImplementedReturnTypeMismatch
@@ -117,15 +94,15 @@ public function withBehaviors()
11794
{
11895
$this->ensureNotLocked();
11996

120-
return $this->tariffTypeBehaviorRegistry->getBehaviors();
97+
return $this->tariffTypeBehaviorRegistry->withBehaviors();
12198
}
12299

123100
public function hasBehavior(string $behaviorClassName): bool
124101
{
125102
return $this->tariffTypeBehaviorRegistry->hasBehavior($behaviorClassName);
126103
}
127104

128-
public function findBehaviorByClass(string $class): ?BehaviorInterface
105+
public function findBehaviorByClass(string $class)
129106
{
130107
return $this->tariffTypeBehaviorRegistry->findBehaviorByClass($class);
131108
}
@@ -136,7 +113,7 @@ public function end(): TariffTypeDefinitionInterface
136113

137114
// Validate prices configuration is complete
138115
if ($this->prices->count() === 0) {
139-
throw new \LogicException('At least one price type must be defined');
116+
throw new LogicException('At least one price type must be defined');
140117
}
141118

142119
return $this;

src/product/TariffTypeDefinitionInterface.php

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

3939
public function end();
40-
41-
public function findPricesByTypeName(string $typeName): ?array;
4240
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace hiqdev\php\billing\product\behavior;
6+
7+
use hiqdev\php\billing\product\trait\HasLockInterface;
8+
9+
/**
10+
* @psalm-suppress MissingTemplateParam
11+
* @psalm-suppress InvalidTemplateParam
12+
*/
13+
abstract class BehaviorRegistry implements HasLockInterface, HasBehaviorsInterface
14+
{
15+
public function hasBehavior(string $behaviorClassName): bool
16+
{
17+
foreach ($this->withBehaviors() as $behavior) {
18+
if ($behavior instanceof $behaviorClassName) {
19+
return true;
20+
}
21+
}
22+
23+
return false;
24+
}
25+
26+
public function findBehaviorByClass(string $class)
27+
{
28+
foreach ($this->withBehaviors() as $behavior) {
29+
if ($behavior instanceof $class) {
30+
return $behavior;
31+
}
32+
}
33+
34+
return null;
35+
}
36+
37+
public function lock(): void
38+
{
39+
$this->withBehaviors()->lock();
40+
}
41+
}

src/product/behavior/HasBehaviorsInterface.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace hiqdev\php\billing\product\behavior;
44

5-
use hiqdev\php\billing\product\invoice\RepresentationCollection;
65
use hiqdev\php\billing\product\price\PriceTypeDefinitionInterface;
76
use hiqdev\php\billing\product\TariffTypeDefinitionInterface;
87

@@ -19,5 +18,10 @@ public function withBehaviors();
1918

2019
public function hasBehavior(string $behaviorClassName): bool;
2120

22-
public function findBehaviorByClass(string $class): ?BehaviorInterface;
21+
/**
22+
* @template TBehavior of object
23+
* @param class-string<TBehavior> $class
24+
* @return TBehavior|null
25+
*/
26+
public function findBehaviorByClass(string $class);
2327
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace hiqdev\php\billing\product\behavior;
6+
7+
use hiqdev\php\billing\product\Domain\Model\TariffTypeInterface;
8+
use hiqdev\php\billing\product\price\PriceTypeDefinitionInterface;
9+
10+
/**
11+
* @template-covariant T of PriceTypeDefinitionInterface
12+
*/
13+
final class PriceTypeBehaviorRegistry extends BehaviorRegistry
14+
{
15+
/**
16+
* @var BehaviorPriceTypeDefinitionCollection<T>
17+
*/
18+
private BehaviorPriceTypeDefinitionCollection $behaviorCollection;
19+
20+
/**
21+
* @psalm-param T $priceTypeDefinition
22+
*/
23+
public function __construct(PriceTypeDefinitionInterface $priceTypeDefinition, TariffTypeInterface $tariffType)
24+
{
25+
$this->behaviorCollection = new BehaviorPriceTypeDefinitionCollection($priceTypeDefinition, $tariffType);
26+
}
27+
28+
/**
29+
* @return BehaviorPriceTypeDefinitionCollection<T>
30+
*/
31+
public function withBehaviors(): BehaviorPriceTypeDefinitionCollection
32+
{
33+
return $this->behaviorCollection;
34+
}
35+
36+
protected function getBehaviorCollection(): BehaviorCollectionInterface
37+
{
38+
return $this->behaviorCollection;
39+
}
40+
}

src/product/behavior/TariffTypeBehaviorRegistry.php

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*
2727
* @template-covariant T of TariffTypeDefinitionInterface
2828
*/
29-
final class TariffTypeBehaviorRegistry implements HasLockInterface
29+
final class TariffTypeBehaviorRegistry extends BehaviorRegistry
3030
{
3131
/**
3232
* @var BehaviorTariffTypeCollection<T>
@@ -44,35 +44,13 @@ public function __construct(TariffTypeDefinitionInterface $tariffTypeDefinition,
4444
/**
4545
* @return BehaviorTariffTypeCollection<T>
4646
*/
47-
public function getBehaviors(): BehaviorTariffTypeCollection
47+
public function withBehaviors(): BehaviorTariffTypeCollection
4848
{
4949
return $this->behaviorCollection;
5050
}
5151

52-
public function hasBehavior(string $behaviorClassName): bool
52+
protected function getBehaviorCollection(): BehaviorCollectionInterface
5353
{
54-
foreach ($this->behaviorCollection as $behavior) {
55-
if ($behavior instanceof $behaviorClassName) {
56-
return true;
57-
}
58-
}
59-
60-
return false;
61-
}
62-
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-
74-
public function lock(): void
75-
{
76-
$this->behaviorCollection->lock();
54+
return $this->behaviorCollection;
7755
}
7856
}

src/product/price/PriceTypeDefinition.php

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
namespace hiqdev\php\billing\product\price;
55

66
use hiqdev\php\billing\product\AggregateInterface;
7-
use hiqdev\php\billing\product\behavior\BehaviorCollectionInterface;
8-
use hiqdev\php\billing\product\behavior\BehaviorInterface;
97
use hiqdev\php\billing\product\behavior\HasBehaviorsInterface;
8+
use hiqdev\php\billing\product\behavior\PriceTypeBehaviorRegistry;
109
use hiqdev\php\billing\product\Exception\AggregateNotDefinedException;
1110
use hiqdev\php\billing\product\behavior\BehaviorPriceTypeDefinitionCollection;
1211
use hiqdev\php\billing\product\invoice\RepresentationCollection;
@@ -22,6 +21,7 @@
2221
use hiqdev\php\billing\product\TariffTypeDefinitionInterface;
2322
use hiqdev\php\billing\product\trait\HasLock;
2423
use hiqdev\php\billing\type\TypeInterface;
24+
use function class_exists;
2525

2626
/**
2727
* @template TParentCollection
@@ -46,16 +46,13 @@ class PriceTypeDefinition implements PriceTypeDefinitionInterface
4646
*/
4747
private RepresentationCollection $representationCollection;
4848

49-
/**
50-
* @var BehaviorPriceTypeDefinitionCollection<PriceTypeDefinition>
51-
*/
52-
private BehaviorPriceTypeDefinitionCollection $behaviorCollection;
53-
5449
private ?AggregateInterface $aggregate = null;
5550

5651
/** @psalm-var TParentCollection */
5752
private readonly PriceTypeDefinitionCollectionInterface $parent;
5853

54+
private readonly PriceTypeBehaviorRegistry $behaviorRegistry;
55+
5956
/**
6057
* @param TParentCollection $parent
6158
*/
@@ -66,7 +63,7 @@ public function __construct(
6663
) {
6764
$this->parent = $parent;
6865
$this->representationCollection = new RepresentationCollection($this);
69-
$this->behaviorCollection = new BehaviorPriceTypeDefinitionCollection($this, $tariffType);
66+
$this->behaviorRegistry = new PriceTypeBehaviorRegistry($this, $tariffType);
7067

7168
$this->init();
7269
}
@@ -107,7 +104,7 @@ public function quantityFormatter(string $formatterClass, $fractionUnit = null):
107104
{
108105
$this->ensureNotLocked();
109106

110-
if (!\class_exists($formatterClass)) {
107+
if (!class_exists($formatterClass)) {
111108
throw new InvalidQuantityFormatterException("Formatter class $formatterClass does not exist");
112109
}
113110

@@ -181,29 +178,17 @@ public function withBehaviors()
181178
{
182179
$this->ensureNotLocked();
183180

184-
return $this->behaviorCollection;
181+
return $this->behaviorRegistry->withBehaviors();
185182
}
186183

187184
public function hasBehavior(string $behaviorClassName): bool
188185
{
189-
foreach ($this->behaviorCollection as $behavior) {
190-
if ($behavior instanceof $behaviorClassName) {
191-
return true;
192-
}
193-
}
194-
195-
return false;
186+
return $this->behaviorRegistry->hasBehavior($behaviorClassName);
196187
}
197188

198-
public function findBehaviorByClass(string $class): ?BehaviorInterface
189+
public function findBehaviorByClass(string $class)
199190
{
200-
foreach ($this->behaviorCollection as $behavior) {
201-
if ($behavior instanceof $class) {
202-
return $behavior;
203-
}
204-
}
205-
206-
return null;
191+
return $this->behaviorRegistry->findBehaviorByClass($class);
207192
}
208193

209194
/**
@@ -242,7 +227,7 @@ public function getQuantityFormatterDefinition(): ?QuantityFormatterDefinition
242227
protected function afterLock(): void
243228
{
244229
$this->representationCollection->lock();
245-
$this->behaviorCollection->lock();
230+
$this->behaviorRegistry->lock();
246231
}
247232

248233
public function getTariffTypeDefinition(): TariffTypeDefinitionInterface

tests/unit/product/behavior/TariffTypeBehaviorRegistryTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ protected function setUp(): void
2121

2222
public function testWithBehaviorsReturnsBehaviorCollection(): void
2323
{
24-
$this->assertInstanceOf(BehaviorTariffTypeCollection::class, $this->manager->getBehaviors());
24+
$this->assertInstanceOf(BehaviorTariffTypeCollection::class, $this->manager->withBehaviors());
2525
}
2626

2727
public function testHasBehaviorReturnsFalseWhenBehaviorNotPresent(): void
@@ -32,7 +32,7 @@ public function testHasBehaviorReturnsFalseWhenBehaviorNotPresent(): void
3232
public function testHasBehaviorReturnsTrueWhenBehaviorPresent(): void
3333
{
3434
$behavior = $this->createMock(TestBehavior::class);
35-
$behaviorCollection = $this->manager->getBehaviors();
35+
$behaviorCollection = $this->manager->withBehaviors();
3636
$behaviorCollection->attach($behavior);
3737

3838
$this->assertTrue($this->manager->hasBehavior(TestBehavior::class));

0 commit comments

Comments
 (0)