Skip to content

Commit 42d8d67

Browse files
HP-2512 Cleaned up BillingRegistryService (#102)
* HP-2512 Cleaned up BillingRegistryService * HP-2512 removed invalid PHPUnit test * HP-2512 moved Representation class to php-billing package * HP-2512 fixed PHPUnit tests * HP-2512 removed PHPUnit test duplicate * HP-2512 made BillingRegistryServiceTest is more readable * HP-2512 tiny * HP-2512 removed BillingRegistryTariffServiceTest PHPUnit test because we don't have BillingRegistryTariffService class * HP-2512 removed the next interfaces because this does not make any sense and only confusing - BillingRegistryBehaviorServiceInterface - BillingRegistryTariffServiceInterface * HP-2512 tiny * HP-2512 created BillingRegistryService::getPriceTypeDefinitionByPriceTypeName() method * HP-2512 tiny * HP-2512 tiny
1 parent 277995a commit 42d8d67

11 files changed

+224
-343
lines changed

src/product/Application/BillingRegistryBehaviorServiceInterface.php

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/product/Application/BillingRegistryService.php

Lines changed: 26 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@
77
use hiqdev\php\billing\product\behavior\BehaviorNotFoundException;
88
use hiqdev\php\billing\product\behavior\InvalidBehaviorException;
99
use hiqdev\php\billing\product\BillingRegistryInterface;
10-
use hiqdev\php\billing\product\Exception\AggregateNotFoundException;
10+
use hiqdev\php\billing\product\Exception\PriceTypeDefinitionNotFoundException;
1111
use hiqdev\php\billing\product\Exception\TariffTypeDefinitionNotFoundException;
1212
use hiqdev\php\billing\product\invoice\InvalidRepresentationException;
1313
use hiqdev\php\billing\product\invoice\RepresentationInterface;
1414
use hiqdev\php\billing\product\price\PriceTypeDefinitionInterface;
15-
use hiqdev\php\billing\product\quantity\FractionQuantityData;
16-
use hiqdev\php\billing\product\quantity\QuantityFormatterInterface;
17-
use hiqdev\php\billing\product\quantity\QuantityFormatterNotFoundException;
1815
use hiqdev\php\billing\product\TariffTypeDefinitionInterface;
1916
use hiqdev\php\billing\type\Type;
2017
use hiqdev\php\billing\type\TypeInterface;
@@ -31,9 +28,14 @@ public function getRepresentationsByType(string $representationClass): array
3128
throw new InvalidRepresentationException("Class '$representationClass' does not exist");
3229
}
3330

34-
if (class_exists($representationClass) && !is_subclass_of($representationClass, RepresentationInterface::class)) {
31+
if (class_exists($representationClass)
32+
&& !is_subclass_of($representationClass, RepresentationInterface::class)
33+
) {
3534
throw new InvalidBehaviorException(
36-
sprintf('Representation class "%s" does not implement RepresentationInterface', $representationClass)
35+
sprintf(
36+
'Representation class "%s" does not implement RepresentationInterface',
37+
$representationClass,
38+
)
3739
);
3840
}
3941

@@ -49,36 +51,15 @@ public function getRepresentationsByType(string $representationClass): array
4951
return $representations;
5052
}
5153

52-
public function getTariffDefinitionByName(string $tariffName): ?TariffTypeDefinitionInterface
54+
public function getTariffTypeDefinitionByTariffName(string $tariffName): TariffTypeDefinitionInterface
5355
{
5456
foreach ($this->registry->getTariffTypeDefinitions() as $tariffTypeDefinition) {
55-
if ($tariffName === $tariffTypeDefinition->tariffType()->name) {
57+
if ($tariffTypeDefinition->tariffType()->equalsName($tariffName)) {
5658
return $tariffTypeDefinition;
5759
}
5860
}
59-
return null;
60-
}
61-
62-
public function hasBehaviour(TariffTypeDefinitionInterface $tariffTypeDefinition, string $behaviorClassWrapper): bool
63-
{
64-
return $tariffTypeDefinition->hasBehavior($behaviorClassWrapper);
65-
}
6661

67-
public function createQuantityFormatter(string $type, FractionQuantityData $data): QuantityFormatterInterface {
68-
$type = $this->convertStringTypeToType($type);
69-
70-
foreach ($this->registry->priceTypes() as $priceTypeDefinition) {
71-
if ($priceTypeDefinition->hasType($type)) {
72-
return $priceTypeDefinition->createQuantityFormatter($data);
73-
}
74-
}
75-
76-
throw new QuantityFormatterNotFoundException('Quantity formatter not found');
77-
}
78-
79-
private function convertStringTypeToType(string $type): TypeInterface
80-
{
81-
return Type::anyId($type);
62+
throw new TariffTypeDefinitionNotFoundException('Tariff type definition was not found');
8263
}
8364

8465
public function getBehavior(string $type, string $behaviorClassWrapper): BehaviorInterface
@@ -112,6 +93,11 @@ public function getBehavior(string $type, string $behaviorClassWrapper): Behavio
11293
);
11394
}
11495

96+
private function convertStringTypeToType(string $type): TypeInterface
97+
{
98+
return Type::anyId($type);
99+
}
100+
115101
private function findBehaviorInPriceType(
116102
PriceTypeDefinitionInterface $priceTypeDefinition,
117103
string $behaviorClassWrapper
@@ -146,28 +132,23 @@ public function getBehaviors(string $behaviorClassWrapper): \Generator
146132

147133
public function getAggregate(string $type): AggregateInterface
148134
{
149-
$type = $this->convertStringTypeToType($type);
150-
151-
foreach ($this->registry->priceTypes() as $priceTypeDefinition) {
152-
if ($priceTypeDefinition->hasType($type)) {
153-
return $priceTypeDefinition->getAggregate();
154-
}
155-
}
156-
157-
throw new AggregateNotFoundException('Aggregate was not found');
135+
return $this->getPriceTypeDefinitionByPriceTypeName($type)->getAggregate();
158136
}
159137

160-
public function findTariffTypeDefinitionByBehavior(BehaviorInterface $behavior): TariffTypeDefinitionInterface
138+
public function getPriceTypeDefinitionByPriceTypeName(string $typeName): PriceTypeDefinitionInterface
161139
{
162-
$tariffType = $behavior->getTariffType();
140+
$type = $this->convertStringTypeToType($typeName);
163141

164-
foreach ($this->registry->getTariffTypeDefinitions() as $tariffTypeDefinition) {
165-
if ($tariffTypeDefinition->belongToTariffType($tariffType)) {
166-
return $tariffTypeDefinition;
142+
foreach ($this->registry->priceTypes() as $priceTypeDefinition) {
143+
if ($priceTypeDefinition->hasType($type)) {
144+
return $priceTypeDefinition;
167145
}
168146
}
169147

170-
throw new TariffTypeDefinitionNotFoundException('Tariff type definition was not found');
148+
throw new PriceTypeDefinitionNotFoundException(sprintf(
149+
'PriceTypeDefinition was not found for %s type',
150+
$typeName,
151+
));
171152
}
172153

173154
public function findPriceTypeDefinitionsByBehavior(string $behaviorClassWrapper): \Generator

src/product/Application/BillingRegistryServiceInterface.php

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,64 @@
77
use hiqdev\php\billing\product\behavior\BehaviorInterface;
88
use hiqdev\php\billing\product\behavior\BehaviorNotFoundException;
99
use hiqdev\php\billing\product\behavior\InvalidBehaviorException;
10+
use hiqdev\php\billing\product\Exception\PriceTypeDefinitionNotFoundException;
11+
use hiqdev\php\billing\product\Exception\TariffTypeDefinitionNotFoundException;
1012
use hiqdev\php\billing\product\invoice\RepresentationInterface;
1113
use hiqdev\php\billing\product\price\PriceTypeDefinitionInterface;
12-
use hiqdev\php\billing\product\quantity\FractionQuantityData;
13-
use hiqdev\php\billing\product\quantity\QuantityFormatterInterface;
1414
use hiqdev\php\billing\product\TariffTypeDefinitionInterface;
1515

16-
interface BillingRegistryServiceInterface extends BillingRegistryTariffServiseInterface, BillingRegistryBehaviorServiceInterface
16+
interface BillingRegistryServiceInterface
1717
{
1818
/**
1919
* @param string $representationClass
2020
* @return RepresentationInterface[]
2121
*/
2222
public function getRepresentationsByType(string $representationClass): array;
2323

24-
public function createQuantityFormatter(string $type, FractionQuantityData $data): QuantityFormatterInterface;
25-
24+
/**
25+
* @deprecated - please use getPriceTypeDefinitionByPriceTypeName() method instead
26+
* @param string $type
27+
* @return AggregateInterface
28+
*/
2629
public function getAggregate(string $type): AggregateInterface;
2730

31+
/**
32+
* @param string $typeName
33+
* @return PriceTypeDefinitionInterface
34+
* @throws PriceTypeDefinitionNotFoundException
35+
*/
36+
public function getPriceTypeDefinitionByPriceTypeName(string $typeName): PriceTypeDefinitionInterface;
37+
38+
/**
39+
* @param string $tariffName
40+
* @return TariffTypeDefinitionInterface
41+
* @throws TariffTypeDefinitionNotFoundException
42+
*/
43+
public function getTariffTypeDefinitionByTariffName(string $tariffName): TariffTypeDefinitionInterface;
44+
45+
/**
46+
* @param string $type - full type like 'overuse,lb_capacity_unit'
47+
* @param string $behaviorClassWrapper
48+
* @return BehaviorInterface
49+
* @throws BehaviorNotFoundException
50+
* @throws InvalidBehaviorException
51+
*/
52+
public function getBehavior(string $type, string $behaviorClassWrapper): BehaviorInterface;
53+
54+
55+
/**
56+
* Find all behaviors attached to any TariffType or PriceType by specified Behavior class.
57+
*
58+
* @param string $behaviorClassWrapper
59+
* @return Generator<BehaviorInterface>
60+
*/
61+
public function getBehaviors(string $behaviorClassWrapper): Generator;
62+
63+
/**
64+
* Find all PriceTypeDefinition in registry by specified Behavior class.
65+
*
66+
* @param string $behaviorClassWrapper
67+
* @return Generator<PriceTypeDefinitionInterface>
68+
*/
69+
public function findPriceTypeDefinitionsByBehavior(string $behaviorClassWrapper): Generator;
2870
}

src/product/Application/BillingRegistryTariffServiseInterface.php

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace hiqdev\php\billing\product\Exception;
4+
5+
use hiqdev\php\billing\Exception\RuntimeException;
6+
7+
class PriceTypeDefinitionNotFoundException extends RuntimeException
8+
{
9+
10+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace hiqdev\php\billing\product\invoice;
4+
5+
use hiqdev\php\billing\type\TypeInterface;
6+
7+
/**
8+
* This class is made abstract intentionally.
9+
* Because you can attach multiple representations, and thus you should distinguish them somehow.
10+
* So, please implement you own representation.
11+
*/
12+
abstract class Representation implements RepresentationInterface
13+
{
14+
private TypeInterface $type;
15+
16+
public function __construct(private readonly string $sql)
17+
{
18+
if (trim($this->sql) === '') {
19+
throw new InvalidRepresentationException('Representation SQL cannot be empty.');
20+
}
21+
}
22+
23+
public function getSql(): string
24+
{
25+
return $this->sql;
26+
}
27+
28+
public function setType(TypeInterface $type): RepresentationInterface
29+
{
30+
$this->type = $type;
31+
32+
return $this;
33+
}
34+
35+
public function getType(): TypeInterface
36+
{
37+
return $this->type;
38+
}
39+
}

0 commit comments

Comments
 (0)