Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

71 changes: 26 additions & 45 deletions src/product/Application/BillingRegistryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@
use hiqdev\php\billing\product\behavior\BehaviorNotFoundException;
use hiqdev\php\billing\product\behavior\InvalidBehaviorException;
use hiqdev\php\billing\product\BillingRegistryInterface;
use hiqdev\php\billing\product\Exception\AggregateNotFoundException;
use hiqdev\php\billing\product\Exception\PriceTypeDefinitionNotFoundException;
use hiqdev\php\billing\product\Exception\TariffTypeDefinitionNotFoundException;
use hiqdev\php\billing\product\invoice\InvalidRepresentationException;
use hiqdev\php\billing\product\invoice\RepresentationInterface;
use hiqdev\php\billing\product\price\PriceTypeDefinitionInterface;
use hiqdev\php\billing\product\quantity\FractionQuantityData;
use hiqdev\php\billing\product\quantity\QuantityFormatterInterface;
use hiqdev\php\billing\product\quantity\QuantityFormatterNotFoundException;
use hiqdev\php\billing\product\TariffTypeDefinitionInterface;
use hiqdev\php\billing\type\Type;
use hiqdev\php\billing\type\TypeInterface;
Expand All @@ -31,9 +28,14 @@ public function getRepresentationsByType(string $representationClass): array
throw new InvalidRepresentationException("Class '$representationClass' does not exist");
}

if (class_exists($representationClass) && !is_subclass_of($representationClass, RepresentationInterface::class)) {
if (class_exists($representationClass)
&& !is_subclass_of($representationClass, RepresentationInterface::class)
) {
throw new InvalidBehaviorException(
sprintf('Representation class "%s" does not implement RepresentationInterface', $representationClass)
sprintf(
'Representation class "%s" does not implement RepresentationInterface',
$representationClass,
)
);
}

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

public function getTariffDefinitionByName(string $tariffName): ?TariffTypeDefinitionInterface
public function getTariffTypeDefinitionByTariffName(string $tariffName): TariffTypeDefinitionInterface
{
foreach ($this->registry->getTariffTypeDefinitions() as $tariffTypeDefinition) {
if ($tariffName === $tariffTypeDefinition->tariffType()->name) {
if ($tariffTypeDefinition->tariffType()->equalsName($tariffName)) {
return $tariffTypeDefinition;
}
}
return null;
}

public function hasBehaviour(TariffTypeDefinitionInterface $tariffTypeDefinition, string $behaviorClassWrapper): bool
{
return $tariffTypeDefinition->hasBehavior($behaviorClassWrapper);
}

public function createQuantityFormatter(string $type, FractionQuantityData $data): QuantityFormatterInterface {
$type = $this->convertStringTypeToType($type);

foreach ($this->registry->priceTypes() as $priceTypeDefinition) {
if ($priceTypeDefinition->hasType($type)) {
return $priceTypeDefinition->createQuantityFormatter($data);
}
}

throw new QuantityFormatterNotFoundException('Quantity formatter not found');
}

private function convertStringTypeToType(string $type): TypeInterface
{
return Type::anyId($type);
throw new TariffTypeDefinitionNotFoundException('Tariff type definition was not found');
}

public function getBehavior(string $type, string $behaviorClassWrapper): BehaviorInterface
Expand Down Expand Up @@ -112,6 +93,11 @@ public function getBehavior(string $type, string $behaviorClassWrapper): Behavio
);
}

private function convertStringTypeToType(string $type): TypeInterface
{
return Type::anyId($type);
}

private function findBehaviorInPriceType(
PriceTypeDefinitionInterface $priceTypeDefinition,
string $behaviorClassWrapper
Expand Down Expand Up @@ -146,28 +132,23 @@ public function getBehaviors(string $behaviorClassWrapper): \Generator

public function getAggregate(string $type): AggregateInterface
{
$type = $this->convertStringTypeToType($type);

foreach ($this->registry->priceTypes() as $priceTypeDefinition) {
if ($priceTypeDefinition->hasType($type)) {
return $priceTypeDefinition->getAggregate();
}
}

throw new AggregateNotFoundException('Aggregate was not found');
return $this->getPriceTypeDefinitionByPriceTypeName($type)->getAggregate();
}

public function findTariffTypeDefinitionByBehavior(BehaviorInterface $behavior): TariffTypeDefinitionInterface
public function getPriceTypeDefinitionByPriceTypeName(string $typeName): PriceTypeDefinitionInterface
{
$tariffType = $behavior->getTariffType();
$type = $this->convertStringTypeToType($typeName);

foreach ($this->registry->getTariffTypeDefinitions() as $tariffTypeDefinition) {
if ($tariffTypeDefinition->belongToTariffType($tariffType)) {
return $tariffTypeDefinition;
foreach ($this->registry->priceTypes() as $priceTypeDefinition) {
if ($priceTypeDefinition->hasType($type)) {
return $priceTypeDefinition;
}
}

throw new TariffTypeDefinitionNotFoundException('Tariff type definition was not found');
throw new PriceTypeDefinitionNotFoundException(sprintf(
'PriceTypeDefinition was not found for %s type',
$typeName,
));
}

public function findPriceTypeDefinitionsByBehavior(string $behaviorClassWrapper): \Generator
Expand Down
52 changes: 47 additions & 5 deletions src/product/Application/BillingRegistryServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,64 @@
use hiqdev\php\billing\product\behavior\BehaviorInterface;
use hiqdev\php\billing\product\behavior\BehaviorNotFoundException;
use hiqdev\php\billing\product\behavior\InvalidBehaviorException;
use hiqdev\php\billing\product\Exception\PriceTypeDefinitionNotFoundException;
use hiqdev\php\billing\product\Exception\TariffTypeDefinitionNotFoundException;
use hiqdev\php\billing\product\invoice\RepresentationInterface;
use hiqdev\php\billing\product\price\PriceTypeDefinitionInterface;
use hiqdev\php\billing\product\quantity\FractionQuantityData;
use hiqdev\php\billing\product\quantity\QuantityFormatterInterface;
use hiqdev\php\billing\product\TariffTypeDefinitionInterface;

interface BillingRegistryServiceInterface extends BillingRegistryTariffServiseInterface, BillingRegistryBehaviorServiceInterface
interface BillingRegistryServiceInterface
{
/**
* @param string $representationClass
* @return RepresentationInterface[]
*/
public function getRepresentationsByType(string $representationClass): array;

public function createQuantityFormatter(string $type, FractionQuantityData $data): QuantityFormatterInterface;

/**
* @deprecated - please use getPriceTypeDefinitionByPriceTypeName() method instead
* @param string $type
* @return AggregateInterface
*/
public function getAggregate(string $type): AggregateInterface;

/**
* @param string $typeName
* @return PriceTypeDefinitionInterface
* @throws PriceTypeDefinitionNotFoundException
*/
public function getPriceTypeDefinitionByPriceTypeName(string $typeName): PriceTypeDefinitionInterface;

/**
* @param string $tariffName
* @return TariffTypeDefinitionInterface
* @throws TariffTypeDefinitionNotFoundException
*/
public function getTariffTypeDefinitionByTariffName(string $tariffName): TariffTypeDefinitionInterface;

/**
* @param string $type - full type like 'overuse,lb_capacity_unit'
* @param string $behaviorClassWrapper
* @return BehaviorInterface
* @throws BehaviorNotFoundException
* @throws InvalidBehaviorException
*/
public function getBehavior(string $type, string $behaviorClassWrapper): BehaviorInterface;


/**
* Find all behaviors attached to any TariffType or PriceType by specified Behavior class.
*
* @param string $behaviorClassWrapper
* @return Generator<BehaviorInterface>
*/
public function getBehaviors(string $behaviorClassWrapper): Generator;

/**
* Find all PriceTypeDefinition in registry by specified Behavior class.
*
* @param string $behaviorClassWrapper
* @return Generator<PriceTypeDefinitionInterface>
*/
public function findPriceTypeDefinitionsByBehavior(string $behaviorClassWrapper): Generator;
}
18 changes: 0 additions & 18 deletions src/product/Application/BillingRegistryTariffServiseInterface.php

This file was deleted.

10 changes: 10 additions & 0 deletions src/product/Exception/PriceTypeDefinitionNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace hiqdev\php\billing\product\Exception;

use hiqdev\php\billing\Exception\RuntimeException;

class PriceTypeDefinitionNotFoundException extends RuntimeException
{

}
39 changes: 39 additions & 0 deletions src/product/invoice/Representation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php declare(strict_types=1);

namespace hiqdev\php\billing\product\invoice;

use hiqdev\php\billing\type\TypeInterface;

/**
* This class is made abstract intentionally.
* Because you can attach multiple representations, and thus you should distinguish them somehow.
* So, please implement you own representation.
*/
abstract class Representation implements RepresentationInterface
{
private TypeInterface $type;

public function __construct(private readonly string $sql)
{
if (trim($this->sql) === '') {
throw new InvalidRepresentationException('Representation SQL cannot be empty.');
}
}

public function getSql(): string
{
return $this->sql;
}

public function setType(TypeInterface $type): RepresentationInterface
{
$this->type = $type;

return $this;
}

public function getType(): TypeInterface
{
return $this->type;
}
}
Loading