Init totalization strategy

This commit is contained in:
2025-09-06 21:09:38 +02:00
parent af28014dc9
commit b5bb718a0a
4 changed files with 99 additions and 2 deletions
+8 -2
View File
@@ -15,7 +15,9 @@ services:
App\Gateway\GatewayInterface:
tags: ['app.gateway.gateway']
App\Money\Currency\ExchangeInterface:
tags: ['app.lib.economy.currency.exchange']
tags: ['app.money.currency.exchange']
App\Money\Totalization\TotalizerInterface:
tags: ['app.money.totalization.totalizer']
App\Matchfunding\Formula\FormulaInterface:
tags: ['app.matchfunding.formula']
App\Matchfunding\Rule\RuleInterface:
@@ -96,7 +98,11 @@ services:
App\Money\Currency\ExchangeLocator:
arguments:
- !tagged 'app.lib.economy.currency.exchange'
- !tagged 'app.money.currency.exchange'
App\Money\Totalization\TotalizerLocator:
arguments:
- !tagged 'app.money.totalization.totalizer'
App\Mapping\AutoMapper:
arguments:
+32
View File
@@ -0,0 +1,32 @@
<?php
namespace App\Money\Totalization;
use App\Money\MoneyInterface;
class TotalizedMoney implements MoneyInterface
{
public function __construct(
private int $amount,
private string $currency,
private int $length,
) {}
public function getAmount(): int
{
return $this->amount;
}
public function getCurrency(): string
{
return $this->currency;
}
/**
* @return int the total number of items that were totalized to produce the Money
*/
public function getLength(): int
{
return $this->length;
}
}
@@ -0,0 +1,16 @@
<?php
namespace App\Money\Totalization;
interface TotalizerInterface
{
/**
* @return string the resource that can be totalized by this Totalizer
*/
public static function getSupportedResource(): string;
/**
* @param array $filters an array of filters to be applied to the resource Collection
*/
public function totalize(array $filters): TotalizedMoney;
}
@@ -0,0 +1,43 @@
<?php
namespace App\Money\Totalization;
class TotalizerLocator
{
/** @var array<string, TotalizerInterface> */
private array $totalizers;
public function __construct(
iterable $interfaces,
) {
/** @var TotalizerInterface[] */
$totalizers = \iterator_to_array($interfaces);
foreach ($totalizers as $totalizer) {
$resource = $totalizer::getSupportedResource();
if (\array_key_exists($resource, $this->totalizers)) {
throw new \Exception(\sprintf(
"Duplicated TotalizerInterface. The resource '%s' is already supported by %s",
$resource,
$totalizers[$resource]::class
));
}
$this->totalizers[$resource] = $totalizer;
}
}
public function get(string $resource): ?TotalizerInterface
{
return $this->totalizers[$resource] ?? null;
}
/**
* @return array<string, TotalizerInterface>
*/
public function getAll(): array
{
return $this->totalizers;
}
}