Skip to content

Commit 5343fbe

Browse files
HP-2419 extended functional of BillingRegistry for easier use (#99)
* HP-2419 extended functional of BillingRegistry for easier use * HP-2419 extended functional of BillingRegistry for easier use * HP-2419 tiny * HP-2419 tiny * HP-2419 tiny * HP-2419 tiny * HP-2419 tiny * HP-2419 tiny * HP-2419 tiny * HP-2419 fixing Psalm error * HP-2419 removed dependency between php-billing and BillingRegistry packages * HP-2419 created TrafCollectorInterface * HP-2419 fixing PHPUnit tests * HP-2419 fixing PHPUnit tests * HP-2419 implemented Service for BillingRegistry and allow client code to communicate only through it * HP-2419 Created PHPUnit test for BillingRegistryService * HP-2419 fixing scrutinizer * HP-2419 fixing scrutinizer * HP-2419 fixing scrutinizer * HP-2419 fixing scrutinizer * HP-2419 fixing scrutinizer * HP-2419 fixing scrutinizer * HP-2419 fixing scrutinizer * HP-2419 exclude vendor and tests from scrutinizer
1 parent 69d9a9e commit 5343fbe

23 files changed

+539
-340
lines changed

.scrutinizer.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
build:
2+
environment:
3+
php:
4+
version: 8.3.3
5+
nodes:
6+
analysis:
7+
tests:
8+
override: [ php-scrutinizer-run ]
9+
10+
tools:
11+
php_code_coverage:
12+
enabled: true
13+
external_code_coverage:
14+
timeout: 600
15+
php_code_sniffer: true
16+
17+
filter:
18+
excluded_paths:
19+
- tests/*
20+
- vendor/*
21+
22+
checks:
23+
php:
24+
code_rating: true
25+
duplication: true

.travis.yml

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
language: php
22
php:
3-
- 7.1
4-
- 7.2
5-
- 7.3
63
- 7.4
7-
dist: trusty
4+
- 8.1
5+
- 8.2
6+
- 8.3
7+
dist: focal
8+
sudo: false
89
cache:
910
directories:
1011
- $HOME/.composer/cache
1112
before_install:
12-
- 'composer self-update'
13+
- 'composer self-update --2'
1314
- 'composer --version'
1415
- 'composer install --no-interaction'
1516
- './vendor/bin/hidev --version'
1617
- './vendor/bin/hidev travis/before-install'
17-
sudo: false
1818
install:
1919
- './vendor/bin/hidev travis/install'
2020
script:
@@ -23,6 +23,3 @@ after_script:
2323
- './vendor/bin/hidev travis/after-script'
2424
matrix:
2525
fast_finish: true
26-
allow_failures:
27-
-
28-
php: 7.1
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace hiqdev\php\billing\product\Application;
4+
5+
use hiqdev\php\billing\product\AggregateInterface;
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\BillingRegistryInterface;
10+
use hiqdev\php\billing\product\Exception\AggregateNotFoundException;
11+
use hiqdev\php\billing\product\Exception\TariffTypeDefinitionNotFoundException;
12+
use hiqdev\php\billing\product\invoice\InvalidRepresentationException;
13+
use hiqdev\php\billing\product\invoice\RepresentationInterface;
14+
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;
18+
use hiqdev\php\billing\product\TariffTypeDefinitionInterface;
19+
use hiqdev\php\billing\type\Type;
20+
use hiqdev\php\billing\type\TypeInterface;
21+
22+
final class BillingRegistryService implements BillingRegistryServiceInterface
23+
{
24+
public function __construct(private readonly BillingRegistryInterface $registry)
25+
{
26+
}
27+
28+
public function getRepresentationsByType(string $representationClass): array
29+
{
30+
if (!class_exists($representationClass)) {
31+
throw new InvalidRepresentationException("Class '$representationClass' does not exist");
32+
}
33+
34+
if (!is_subclass_of($representationClass, RepresentationInterface::class)) {
35+
throw new InvalidBehaviorException(
36+
sprintf('Representation class "%s" does not implement RepresentationInterface', $representationClass)
37+
);
38+
}
39+
40+
$representations = [];
41+
foreach ($this->registry->priceTypes() as $priceTypeDefinition) {
42+
foreach ($priceTypeDefinition->documentRepresentation() as $representation) {
43+
if ($representation instanceof $representationClass) {
44+
$representations[] = $representation;
45+
}
46+
}
47+
}
48+
49+
return $representations;
50+
}
51+
52+
public function createQuantityFormatter(string $type, FractionQuantityData $data): QuantityFormatterInterface {
53+
$type = $this->convertStringTypeToType($type);
54+
55+
foreach ($this->registry->priceTypes() as $priceTypeDefinition) {
56+
if ($priceTypeDefinition->hasType($type)) {
57+
return $priceTypeDefinition->createQuantityFormatter($data);
58+
}
59+
}
60+
61+
throw new QuantityFormatterNotFoundException('Quantity formatter not found');
62+
}
63+
64+
private function convertStringTypeToType(string $type): TypeInterface
65+
{
66+
return Type::anyId($type);
67+
}
68+
69+
public function getBehavior(string $type, string $behaviorClassWrapper): BehaviorInterface
70+
{
71+
if (!class_exists($behaviorClassWrapper)) {
72+
throw new InvalidBehaviorException(
73+
sprintf('Behavior class "%s" does not exist', $behaviorClassWrapper)
74+
);
75+
}
76+
77+
if (!is_subclass_of($behaviorClassWrapper, BehaviorInterface::class)) {
78+
throw new InvalidBehaviorException(
79+
sprintf('Behavior class "%s" does not implement BehaviorInterface', $behaviorClassWrapper)
80+
);
81+
}
82+
83+
$billingType = $this->convertStringTypeToType($type);
84+
85+
foreach ($this->registry->priceTypes() as $priceTypeDefinition) {
86+
if ($priceTypeDefinition->hasType($billingType)) {
87+
$behavior = $this->findBehaviorInPriceType($priceTypeDefinition, $behaviorClassWrapper);
88+
89+
if ($behavior) {
90+
return $behavior;
91+
}
92+
}
93+
}
94+
95+
throw new BehaviorNotFoundException(
96+
sprintf('Behavior of class "%s" not found for type "%s"', $behaviorClassWrapper, $type),
97+
);
98+
}
99+
100+
private function findBehaviorInPriceType(
101+
PriceTypeDefinitionInterface $priceTypeDefinition,
102+
string $behaviorClassWrapper
103+
): ?BehaviorInterface {
104+
foreach ($priceTypeDefinition->withBehaviors() as $behavior) {
105+
if ($behavior instanceof $behaviorClassWrapper) {
106+
return $behavior;
107+
}
108+
}
109+
110+
return null;
111+
}
112+
113+
public function getBehaviors(string $behaviorClassWrapper): \Generator
114+
{
115+
foreach ($this->registry->getTariffTypeDefinitions() as $tariffTypeDefinition) {
116+
foreach ($tariffTypeDefinition->withBehaviors() as $behavior) {
117+
if ($behavior instanceof $behaviorClassWrapper) {
118+
yield $behavior;
119+
}
120+
}
121+
}
122+
123+
foreach ($this->registry->priceTypes() as $priceTypeDefinition) {
124+
foreach ($priceTypeDefinition->withBehaviors() as $behavior) {
125+
if ($behavior instanceof $behaviorClassWrapper) {
126+
yield $behavior;
127+
}
128+
}
129+
}
130+
}
131+
132+
public function getAggregate(string $type): AggregateInterface
133+
{
134+
$type = $this->convertStringTypeToType($type);
135+
136+
foreach ($this->registry->priceTypes() as $priceTypeDefinition) {
137+
if ($priceTypeDefinition->hasType($type)) {
138+
return $priceTypeDefinition->getAggregate();
139+
}
140+
}
141+
142+
throw new AggregateNotFoundException('Aggregate was not found');
143+
}
144+
145+
public function findTariffTypeDefinitionByBehavior(BehaviorInterface $behavior): TariffTypeDefinitionInterface
146+
{
147+
$tariffType = $behavior->getTariffType();
148+
149+
foreach ($this->registry->getTariffTypeDefinitions() as $tariffTypeDefinition) {
150+
if ($tariffTypeDefinition->belongToTariffType($tariffType)) {
151+
return $tariffTypeDefinition;
152+
}
153+
}
154+
155+
throw new TariffTypeDefinitionNotFoundException('Tariff type definition was not found');
156+
}
157+
158+
public function findPriceTypeDefinitionsByBehavior(string $behaviorClassWrapper): \Generator
159+
{
160+
foreach ($this->registry->priceTypes() as $priceTypeDefinition) {
161+
if ($priceTypeDefinition->hasBehavior($behaviorClassWrapper)) {
162+
yield $priceTypeDefinition;
163+
}
164+
}
165+
}
166+
}
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+
}

0 commit comments

Comments
 (0)