Skip to content

Commit b6aada3

Browse files
HP-1751 created TariffTypeBehaviorRegistry
1 parent 3aab1ee commit b6aada3

File tree

3 files changed

+77
-10
lines changed

3 files changed

+77
-10
lines changed

src/product/TariffTypeDefinition.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace hiqdev\php\billing\product;
44

55
use hiqdev\php\billing\product\behavior\BehaviorTariffTypeCollection;
6+
use hiqdev\php\billing\product\behavior\TariffTypeBehaviorRegistry;
67
use hiqdev\php\billing\product\Domain\Model\TariffTypeInterface;
78
use hiqdev\php\billing\product\Exception\ProductNotDefinedException;
89
use hiqdev\php\billing\product\Exception\TariffTypeLockedException;
@@ -17,12 +18,12 @@ class TariffTypeDefinition implements TariffTypeDefinitionInterface
1718

1819
private PriceTypeDefinitionCollection $prices;
1920

20-
private BehaviorTariffTypeCollection $behaviorCollection;
21+
private TariffTypeBehaviorRegistry $tariffTypeBehaviorRegistry;
2122

2223
public function __construct(private readonly TariffTypeInterface $tariffType)
2324
{
2425
$this->prices = new PriceTypeDefinitionCollection($this, new PriceTypeDefinitionFactory());
25-
$this->behaviorCollection = new BehaviorTariffTypeCollection($this, $tariffType);
26+
$this->tariffTypeBehaviorRegistry = new TariffTypeBehaviorRegistry($this, $tariffType);
2627
}
2728

2829
public function tariffType(): TariffTypeInterface
@@ -78,18 +79,12 @@ public function withBehaviors(): BehaviorTariffTypeCollection
7879
{
7980
$this->ensureNotLocked();
8081

81-
return $this->behaviorCollection;
82+
return $this->tariffTypeBehaviorRegistry->withBehaviors();
8283
}
8384

8485
public function hasBehavior(string $behaviorClassName): bool
8586
{
86-
foreach ($this->behaviorCollection as $behavior) {
87-
if ($behavior instanceof $behaviorClassName) {
88-
return true;
89-
}
90-
}
91-
92-
return false;
87+
return $this->tariffTypeBehaviorRegistry->hasBehavior($behaviorClassName);
9388
}
9489

9590
public function end(): TariffTypeDefinitionInterface
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace hiqdev\php\billing\product\behavior;
4+
5+
use hiqdev\php\billing\product\Domain\Model\TariffTypeInterface;
6+
use hiqdev\php\billing\product\TariffTypeDefinitionInterface;
7+
8+
final class TariffTypeBehaviorRegistry
9+
{
10+
private BehaviorTariffTypeCollection $behaviorCollection;
11+
12+
public function __construct(TariffTypeDefinitionInterface $tariffTypeDefinition, TariffTypeInterface $tariffType)
13+
{
14+
$this->behaviorCollection = new BehaviorTariffTypeCollection($tariffTypeDefinition, $tariffType);
15+
}
16+
17+
public function withBehaviors(): BehaviorTariffTypeCollection
18+
{
19+
return $this->behaviorCollection;
20+
}
21+
22+
public function hasBehavior(string $behaviorClassName): bool
23+
{
24+
foreach ($this->behaviorCollection as $behavior) {
25+
if ($behavior instanceof $behaviorClassName) {
26+
return true;
27+
}
28+
}
29+
30+
return false;
31+
}
32+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace hiqdev\php\billing\tests\unit\product\behavior;
4+
5+
use hiqdev\php\billing\product\behavior\TariffTypeBehaviorRegistry;
6+
use hiqdev\php\billing\product\TariffTypeDefinitionInterface;
7+
use hiqdev\php\billing\product\Domain\Model\TariffTypeInterface;
8+
use hiqdev\php\billing\product\behavior\BehaviorTariffTypeCollection;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class TariffTypeBehaviorRegistryTest extends TestCase
12+
{
13+
private TariffTypeBehaviorRegistry $manager;
14+
15+
protected function setUp(): void
16+
{
17+
$tariffTypeDefinition = $this->createMock(TariffTypeDefinitionInterface::class);
18+
$tariffType = $this->createMock(TariffTypeInterface::class);
19+
$this->manager = new TariffTypeBehaviorRegistry($tariffTypeDefinition, $tariffType);
20+
}
21+
22+
public function testWithBehaviorsReturnsBehaviorCollection(): void
23+
{
24+
$this->assertInstanceOf(BehaviorTariffTypeCollection::class, $this->manager->withBehaviors());
25+
}
26+
27+
public function testHasBehaviorReturnsFalseWhenBehaviorNotPresent(): void
28+
{
29+
$this->assertFalse($this->manager->hasBehavior(TestBehavior::class));
30+
}
31+
32+
public function testHasBehaviorReturnsTrueWhenBehaviorPresent(): void
33+
{
34+
$behavior = $this->createMock(TestBehavior::class);
35+
$behaviorCollection = $this->manager->withBehaviors();
36+
$behaviorCollection->attach($behavior);
37+
38+
$this->assertTrue($this->manager->hasBehavior(TestBehavior::class));
39+
}
40+
}

0 commit comments

Comments
 (0)