Skip to content

Commit 27bec10

Browse files
HP-2419 implemented Service for BillingRegistry and allow client code to communicate only through it
1 parent 167436f commit 27bec10

File tree

4 files changed

+63
-105
lines changed

4 files changed

+63
-105
lines changed

src/product/BillingRegistryService.php renamed to src/product/Application/BillingRegistryService.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
<?php declare(strict_types=1);
22

3-
namespace hiqdev\php\billing\product;
3+
namespace hiqdev\php\billing\product\Application;
44

5+
use hiqdev\php\billing\product\AggregateInterface;
56
use hiqdev\php\billing\product\behavior\BehaviorInterface;
67
use hiqdev\php\billing\product\behavior\BehaviorNotFoundException;
78
use hiqdev\php\billing\product\behavior\InvalidBehaviorException;
9+
use hiqdev\php\billing\product\BillingRegistryInterface;
810
use hiqdev\php\billing\product\Exception\AggregateNotFoundException;
911
use hiqdev\php\billing\product\Exception\TariffTypeDefinitionNotFoundException;
1012
use hiqdev\php\billing\product\invoice\InvalidRepresentationException;
1113
use hiqdev\php\billing\product\invoice\RepresentationInterface;
12-
use hiqdev\php\billing\product\price\PriceTypeDefinition;
14+
use hiqdev\php\billing\product\price\PriceTypeDefinitionInterface;
1315
use hiqdev\php\billing\product\quantity\FractionQuantityData;
1416
use hiqdev\php\billing\product\quantity\QuantityFormatterInterface;
1517
use hiqdev\php\billing\product\quantity\QuantityFormatterNotFoundException;
18+
use hiqdev\php\billing\product\TariffTypeDefinitionInterface;
1619
use hiqdev\php\billing\type\Type;
1720
use hiqdev\php\billing\type\TypeInterface;
1821

19-
class BillingRegistryService
22+
final class BillingRegistryService implements BillingRegistryServiceInterface
2023
{
21-
public function __construct(private readonly BillingRegistry $registry)
24+
public function __construct(private readonly BillingRegistryInterface $registry)
2225
{
2326
}
2427

@@ -46,10 +49,7 @@ public function getRepresentationsByType(string $representationClass): array
4649
return $representations;
4750
}
4851

49-
public function createQuantityFormatter(
50-
string $type,
51-
FractionQuantityData $data,
52-
): QuantityFormatterInterface {
52+
public function createQuantityFormatter(string $type, FractionQuantityData $data): QuantityFormatterInterface {
5353
$type = $this->convertStringTypeToType($type);
5454

5555
foreach ($this->registry->priceTypes() as $priceTypeDefinition) {
@@ -98,7 +98,7 @@ public function getBehavior(string $type, string $behaviorClassWrapper): Behavio
9898
}
9999

100100
private function findBehaviorInPriceType(
101-
PriceTypeDefinition $priceTypeDefinition,
101+
PriceTypeDefinitionInterface $priceTypeDefinition,
102102
string $behaviorClassWrapper
103103
): ?BehaviorInterface {
104104
foreach ($priceTypeDefinition->withBehaviors() as $behavior) {
@@ -110,9 +110,6 @@ private function findBehaviorInPriceType(
110110
return null;
111111
}
112112

113-
/**
114-
* @inerhitDoc
115-
*/
116113
public function getBehaviors(string $behaviorClassWrapper): \Generator
117114
{
118115
foreach ($this->registry->getTariffTypeDefinitions() as $tariffTypeDefinition) {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace hiqdev\php\billing\product\Application;
4+
5+
use Generator;
6+
use hiqdev\php\billing\product\AggregateInterface;
7+
use hiqdev\php\billing\product\behavior\BehaviorInterface;
8+
use hiqdev\php\billing\product\behavior\BehaviorNotFoundException;
9+
use hiqdev\php\billing\product\behavior\InvalidBehaviorException;
10+
use hiqdev\php\billing\product\invoice\RepresentationInterface;
11+
use hiqdev\php\billing\product\price\PriceTypeDefinitionInterface;
12+
use hiqdev\php\billing\product\quantity\FractionQuantityData;
13+
use hiqdev\php\billing\product\quantity\QuantityFormatterInterface;
14+
use hiqdev\php\billing\product\TariffTypeDefinitionInterface;
15+
16+
interface BillingRegistryServiceInterface
17+
{
18+
/**
19+
* @param string $representationClass
20+
* @return RepresentationInterface[]
21+
*/
22+
public function getRepresentationsByType(string $representationClass): array;
23+
24+
public function createQuantityFormatter(string $type, FractionQuantityData $data): QuantityFormatterInterface;
25+
26+
/**
27+
* @param string $type - full type like 'overuse,lb_capacity_unit'
28+
* @param string $behaviorClassWrapper
29+
* @return BehaviorInterface
30+
* @throws BehaviorNotFoundException
31+
* @throws InvalidBehaviorException
32+
*/
33+
public function getBehavior(string $type, string $behaviorClassWrapper): BehaviorInterface;
34+
35+
/**
36+
* Find all behaviors attached to any TariffType or PriceType by specified Behavior class.
37+
*
38+
* @param string $behaviorClassWrapper
39+
* @return Generator<BehaviorInterface>
40+
*/
41+
public function getBehaviors(string $behaviorClassWrapper): Generator;
42+
43+
public function getAggregate(string $type): AggregateInterface;
44+
45+
public function findTariffTypeDefinitionByBehavior(BehaviorInterface $behavior): TariffTypeDefinitionInterface;
46+
47+
/**
48+
* Find all PriceTypeDefinition in registry by specified Behavior class.
49+
*
50+
* @param string $behaviorClassWrapper
51+
* @return Generator<PriceTypeDefinitionInterface>
52+
*/
53+
public function findPriceTypeDefinitionsByBehavior(string $behaviorClassWrapper): Generator;
54+
}

src/product/BillingRegistry.php

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

33
namespace hiqdev\php\billing\product;
44

5-
use hiqdev\php\billing\product\quantity\QuantityFormatterInterface;
6-
use hiqdev\php\billing\product\quantity\FractionQuantityData;
7-
use hiqdev\php\billing\product\behavior\BehaviorInterface;
85
use hiqdev\php\billing\product\trait\HasLock;
96

107
class BillingRegistry implements BillingRegistryInterface
@@ -14,13 +11,6 @@ class BillingRegistry implements BillingRegistryInterface
1411
/** @var TariffTypeDefinitionInterface[] */
1512
private array $tariffTypeDefinitions = [];
1613

17-
private BillingRegistryService $service;
18-
19-
public function __construct()
20-
{
21-
$this->service = new BillingRegistryService($this);
22-
}
23-
2414
public function addTariffType(TariffTypeDefinitionInterface $tariffTypeDefinition): void
2515
{
2616
$this->ensureNotLocked();
@@ -42,46 +32,6 @@ public function priceTypes(): \Generator
4232
}
4333
}
4434

45-
public function getRepresentationsByType(string $representationClass): array
46-
{
47-
return $this->service->getRepresentationsByType($representationClass);
48-
}
49-
50-
public function createQuantityFormatter(
51-
string $type,
52-
FractionQuantityData $data,
53-
): QuantityFormatterInterface {
54-
return $this->service->createQuantityFormatter($type, $data);
55-
}
56-
57-
public function getBehavior(string $type, string $behaviorClassWrapper): BehaviorInterface
58-
{
59-
return $this->service->getBehavior($type, $behaviorClassWrapper);
60-
}
61-
62-
/**
63-
* @inerhitDoc
64-
*/
65-
public function getBehaviors(string $behaviorClassWrapper): \Generator
66-
{
67-
return $this->service->getBehaviors($behaviorClassWrapper);
68-
}
69-
70-
public function getAggregate(string $type): AggregateInterface
71-
{
72-
return $this->service->getAggregate($type);
73-
}
74-
75-
public function findTariffTypeDefinitionByBehavior(BehaviorInterface $behavior): TariffTypeDefinitionInterface
76-
{
77-
return $this->service->findTariffTypeDefinitionByBehavior($behavior);
78-
}
79-
80-
public function findPriceTypeDefinitionsByBehavior(string $behaviorClassWrapper): \Generator
81-
{
82-
return $this->service->findPriceTypeDefinitionsByBehavior($behaviorClassWrapper);
83-
}
84-
8535
protected function afterLock(): void
8636
{
8737
$this->lockItems($this->tariffTypeDefinitions);

src/product/BillingRegistryInterface.php

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

55
use Generator;
6-
use hiqdev\php\billing\product\behavior\BehaviorInterface;
7-
use hiqdev\php\billing\product\behavior\BehaviorNotFoundException;
8-
use hiqdev\php\billing\product\behavior\InvalidBehaviorException;
9-
use hiqdev\php\billing\product\invoice\RepresentationInterface;
106
use hiqdev\php\billing\product\price\PriceTypeDefinitionInterface;
11-
use hiqdev\php\billing\product\quantity\FractionQuantityData;
12-
use hiqdev\php\billing\product\quantity\QuantityFormatterInterface;
137
use hiqdev\php\billing\product\trait\HasLockInterface;
148

159
interface BillingRegistryInterface extends HasLockInterface
@@ -22,41 +16,4 @@ public function priceTypes(): Generator;
2216
public function getTariffTypeDefinitions(): array;
2317

2418
public function addTariffType(TariffTypeDefinitionInterface $tariffTypeDefinition): void;
25-
26-
/**
27-
* @param string $representationClass
28-
* @return RepresentationInterface[]
29-
*/
30-
public function getRepresentationsByType(string $representationClass): array;
31-
32-
public function createQuantityFormatter(string $type, FractionQuantityData $data): QuantityFormatterInterface;
33-
34-
/**
35-
* @param string $type - full type like 'overuse,lb_capacity_unit'
36-
* @param string $behaviorClassWrapper
37-
* @return BehaviorInterface
38-
* @throws BehaviorNotFoundException
39-
* @throws InvalidBehaviorException
40-
*/
41-
public function getBehavior(string $type, string $behaviorClassWrapper): BehaviorInterface;
42-
43-
/**
44-
* Find all behaviors attached to any TariffType or PriceType by specified Behavior class.
45-
*
46-
* @param string $behaviorClassWrapper
47-
* @return Generator<BehaviorInterface>
48-
*/
49-
public function getBehaviors(string $behaviorClassWrapper): Generator;
50-
51-
public function findTariffTypeDefinitionByBehavior(BehaviorInterface $behavior): TariffTypeDefinitionInterface;
52-
53-
/**
54-
* Find all PriceTypeDefinition in registry by specified Behavior class.
55-
*
56-
* @param string $behaviorClassWrapper
57-
* @return \Generator<PriceTypeDefinitionInterface>
58-
*/
59-
public function findPriceTypeDefinitionsByBehavior(string $behaviorClassWrapper): \Generator;
60-
61-
public function getAggregate(string $type): AggregateInterface;
6219
}

0 commit comments

Comments
 (0)