Skip to content

Commit a9e3868

Browse files
HP-2419 extended functional of BillingRegistry for easier use
1 parent 9220708 commit a9e3868

13 files changed

+279
-129
lines changed

src/product/BillingRegistry.php

Lines changed: 28 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,42 @@
22

33
namespace hiqdev\php\billing\product;
44

5-
use hiqdev\php\billing\product\behavior\InvalidBehaviorException;
6-
use hiqdev\php\billing\product\Exception\AggregateNotFoundException;
7-
use hiqdev\php\billing\product\invoice\InvalidRepresentationException;
8-
use hiqdev\php\billing\product\invoice\RepresentationInterface;
9-
use hiqdev\php\billing\product\price\PriceTypeDefinition;
105
use hiqdev\php\billing\product\quantity\QuantityFormatterInterface;
11-
use hiqdev\php\billing\product\quantity\QuantityFormatterNotFoundException;
126
use hiqdev\php\billing\product\quantity\FractionQuantityData;
137
use hiqdev\php\billing\product\behavior\BehaviorInterface;
14-
use hiqdev\php\billing\product\behavior\BehaviorNotFoundException;
158
use hiqdev\php\billing\product\trait\HasLock;
16-
use hiqdev\php\billing\type\Type;
17-
use hiqdev\php\billing\type\TypeInterface;
189

1910
class BillingRegistry implements BillingRegistryInterface
2011
{
2112
use HasLock;
2213

2314
/** @var TariffTypeDefinitionInterface[] */
2415
private array $tariffTypeDefinitions = [];
16+
2517
private bool $locked = false;
2618

19+
private BillingRegistryService $service;
20+
21+
public function __construct()
22+
{
23+
$this->service = new BillingRegistryService($this);
24+
}
25+
2726
public function addTariffType(TariffTypeDefinitionInterface $tariffTypeDefinition): void
2827
{
2928
$this->ensureNotLocked();
3029

3130
$this->tariffTypeDefinitions[] = $tariffTypeDefinition;
3231
}
3332

33+
public function getTariffTypeDefinitions(): array
34+
{
35+
return $this->tariffTypeDefinitions;
36+
}
37+
3438
public function priceTypes(): \Generator
3539
{
36-
foreach ($this->tariffTypeDefinitions as $tariffTypeDefinition) {
40+
foreach ($this->getTariffTypeDefinitions() as $tariffTypeDefinition) {
3741
foreach ($tariffTypeDefinition->withPrices() as $priceTypeDefinition) {
3842
yield $priceTypeDefinition;
3943
}
@@ -42,129 +46,42 @@ public function priceTypes(): \Generator
4246

4347
public function getRepresentationsByType(string $representationClass): array
4448
{
45-
if (!class_exists($representationClass)) {
46-
throw new InvalidRepresentationException("Class '$representationClass' does not exist");
47-
}
48-
49-
if (!is_subclass_of($representationClass, RepresentationInterface::class)) {
50-
throw new InvalidBehaviorException(
51-
sprintf('Representation class "%s" does not implement RepresentationInterface', $representationClass)
52-
);
53-
}
54-
55-
$representations = [];
56-
foreach ($this->priceTypes() as $priceTypeDefinition) {
57-
foreach ($priceTypeDefinition->documentRepresentation() as $representation) {
58-
if ($representation instanceof $representationClass) {
59-
$representations[] = $representation;
60-
}
61-
}
62-
}
63-
64-
return $representations;
49+
return $this->service->getRepresentationsByType($representationClass);
6550
}
6651

6752
public function createQuantityFormatter(
6853
string $type,
6954
FractionQuantityData $data,
7055
): QuantityFormatterInterface {
71-
$type = $this->convertStringTypeToType($type);
72-
73-
foreach ($this->priceTypes() as $priceTypeDefinition) {
74-
if ($priceTypeDefinition->hasType($type)) {
75-
return $priceTypeDefinition->createQuantityFormatter($data);
76-
}
77-
}
78-
79-
throw new QuantityFormatterNotFoundException('Quantity formatter not found');
80-
}
81-
82-
private function convertStringTypeToType(string $type): TypeInterface
83-
{
84-
return Type::anyId($type);
56+
return $this->service->createQuantityFormatter($type, $data);
8557
}
8658

8759
public function getBehavior(string $type, string $behaviorClassWrapper): BehaviorInterface
8860
{
89-
if (!class_exists($behaviorClassWrapper)) {
90-
throw new InvalidBehaviorException(
91-
sprintf('Behavior class "%s" does not exist', $behaviorClassWrapper)
92-
);
93-
}
94-
95-
if (!is_subclass_of($behaviorClassWrapper, BehaviorInterface::class)) {
96-
throw new InvalidBehaviorException(
97-
sprintf('Behavior class "%s" does not implement BehaviorInterface', $behaviorClassWrapper)
98-
);
99-
}
100-
101-
$billingType = $this->convertStringTypeToType($type);
102-
103-
foreach ($this->priceTypes() as $priceTypeDefinition) {
104-
if ($priceTypeDefinition->hasType($billingType)) {
105-
$behavior = $this->findBehaviorInPriceType($priceTypeDefinition, $behaviorClassWrapper);
106-
107-
if ($behavior) {
108-
return $behavior;
109-
}
110-
}
111-
}
112-
113-
throw new BehaviorNotFoundException(
114-
sprintf('Behavior of class "%s" not found for type "%s"', $behaviorClassWrapper, $type),
115-
);
116-
}
117-
118-
private function findBehaviorInPriceType(
119-
PriceTypeDefinition $priceTypeDefinition,
120-
string $behaviorClassWrapper
121-
): ?BehaviorInterface {
122-
foreach ($priceTypeDefinition->withBehaviors() as $behavior) {
123-
if ($behavior instanceof $behaviorClassWrapper) {
124-
return $behavior;
125-
}
126-
}
127-
128-
return null;
61+
return $this->service->getBehavior($type, $behaviorClassWrapper);
12962
}
13063

64+
/**
65+
* @inerhitDoc
66+
*/
13167
public function getBehaviors(string $behaviorClassWrapper): \Generator
13268
{
133-
foreach ($this->tariffTypeDefinitions as $tariffTypeDefinition) {
134-
foreach ($tariffTypeDefinition->withBehaviors() as $behavior) {
135-
if ($behavior instanceof $behaviorClassWrapper) {
136-
yield $behavior;
137-
}
138-
}
139-
}
140-
141-
foreach ($this->priceTypes() as $priceTypeDefinition) {
142-
foreach ($priceTypeDefinition->withBehaviors() as $behavior) {
143-
if ($behavior instanceof $behaviorClassWrapper) {
144-
yield $behavior;
145-
}
146-
}
147-
}
69+
return $this->service->getBehaviors($behaviorClassWrapper);
14870
}
14971

15072
public function getAggregate(string $type): AggregateInterface
15173
{
152-
$type = $this->convertStringTypeToType($type);
153-
154-
foreach ($this->priceTypes() as $priceTypeDefinition) {
155-
if ($priceTypeDefinition->hasType($type)) {
156-
return $priceTypeDefinition->getAggregate();
157-
}
158-
}
74+
return $this->service->getAggregate($type);
75+
}
15976

160-
throw new AggregateNotFoundException('Aggregate was not found');
77+
public function findTariffTypeDefinitionByBehavior(BehaviorInterface $behavior): TariffTypeDefinitionInterface
78+
{
79+
return $this->service->findTariffTypeDefinitionByBehavior($behavior);
16180
}
16281

163-
public function getTariffTypeDefinitions(): \Generator
82+
public function findPriceTypeDefinitionsByBehavior(string $behaviorClassWrapper): \Generator
16483
{
165-
foreach ($this->tariffTypeDefinitions as $tariffTypeDefinition) {
166-
yield $tariffTypeDefinition;
167-
}
84+
return $this->service->findPriceTypeDefinitionsByBehavior($behaviorClassWrapper);
16885
}
16986

17087
protected function afterLock(): void

src/product/BillingRegistryInterface.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
interface BillingRegistryInterface extends HasLockInterface
1616
{
1717
/**
18-
* @return Generator
19-
* @psalm-return Generator<PriceTypeDefinitionInterface>
18+
* @return Generator<PriceTypeDefinitionInterface>
2019
*/
2120
public function priceTypes(): Generator;
2221

22+
public function getTariffTypeDefinitions(): array;
23+
2324
public function addTariffType(TariffTypeDefinitionInterface $tariffTypeDefinition): void;
2425

2526
/**
@@ -40,17 +41,22 @@ public function createQuantityFormatter(string $type, FractionQuantityData $data
4041
public function getBehavior(string $type, string $behaviorClassWrapper): BehaviorInterface;
4142

4243
/**
44+
* Find all behaviors attached to any TariffType or PriceType by specified Behavior class.
45+
*
4346
* @param string $behaviorClassWrapper
44-
* @return Generator
45-
* @psalm-return Generator<BehaviorInterface>
47+
* @return Generator<BehaviorInterface>
4648
*/
4749
public function getBehaviors(string $behaviorClassWrapper): Generator;
4850

49-
public function getAggregate(string $type): AggregateInterface;
51+
public function findTariffTypeDefinitionByBehavior(BehaviorInterface $behavior): TariffTypeDefinitionInterface;
5052

5153
/**
52-
* @return Generator
53-
* @psalm-return Generator<TariffTypeDefinitionInterface>
54+
* Find all PriceTypeDefinition in registry by specified Behavior class.
55+
*
56+
* @param string $behaviorClassWrapper
57+
* @return \Generator<PriceTypeDefinitionInterface>
5458
*/
55-
public function getTariffTypeDefinitions(): Generator;
59+
public function findPriceTypeDefinitionsByBehavior(string $behaviorClassWrapper): \Generator;
60+
61+
public function getAggregate(string $type): AggregateInterface;
5662
}

0 commit comments

Comments
 (0)