Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HP-1751 Create configurations for billing types #530

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
145f4f0
HP-1751 Create configurations for billing types
VadymHrechukha Dec 17, 2024
153f37d
HP-1751 Create configurations for billing types
VadymHrechukha Dec 17, 2024
84f3be6
HP-1751 cleaned up ConsumptionConfigurator from duplicates and thus s…
VadymHrechukha Dec 20, 2024
35e187c
HP-1751 removed part of Consumption configuration because it was impl…
VadymHrechukha Dec 26, 2024
c848e29
HP-1751 overwrote ConsumptionConfigurator::getConfigurations() method…
VadymHrechukha Dec 27, 2024
547de81
HP-1751 Created HasDecorator trait and removed ServerResourceDecorato…
VadymHrechukha Dec 27, 2024
3c477b0
HP-1751 fixed autoload BillingRegistry in ConsumptionConfigurator
VadymHrechukha Dec 30, 2024
e39fa0a
HP-1751 use DecoratedInterface from Billing Registry
VadymHrechukha Dec 30, 2024
ac6d395
HP-1751 tiny
VadymHrechukha Dec 30, 2024
f1aa881
HP-1751 added annotation for ResourceDecoratorFactory::createFromReso…
VadymHrechukha Dec 31, 2024
f43333b
HP-1751 tiny
VadymHrechukha Dec 31, 2024
08f66cb
HP-1751 removed the last mention of ConsumptionConfigurator from conf…
VadymHrechukha Jan 1, 2025
9535ff5
HP-1751 added tariff into ConsumptionConfigurator because it can't be…
VadymHrechukha Jan 1, 2025
041a294
HP-1751 removed decorators because they are moved to Billing Registry
VadymHrechukha Jan 1, 2025
36e414d
HP-1751 overwrote ResourceHelper::convertAmount()
VadymHrechukha Jan 2, 2025
07c37be
HP-1751 tiny
VadymHrechukha Jan 2, 2025
1ff5462
HP-1751 Implemented BillingRegistry::getAggregate() method
VadymHrechukha Jan 2, 2025
5dc957e
HP-1751 removed old code
VadymHrechukha Jan 2, 2025
bfcdefc
HP-1751 add overuse to all types
VadymHrechukha Jan 2, 2025
1658f36
HP-1751 tiny
VadymHrechukha Jan 7, 2025
d71bbd3
HP-1751 optimized Consumption page
VadymHrechukha Jan 7, 2025
c882709
HP-1751 created ConsumptionConfiguratorData for easier understanding …
VadymHrechukha Jan 7, 2025
ba3b462
HP-1751 tiny
VadymHrechukha Jan 7, 2025
0582250
HP-1751 increasing speed of finance/consumption page
VadymHrechukha Jan 7, 2025
1467874
HP-1751 increasing speed of finance/consumption page
VadymHrechukha Jan 7, 2025
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"hiqdev/hipanel-module-client": "dev-master",
"tuck/sort": "^0.1.0",
"moneyphp/money": "*",
"hiqdev/php-units": "*"
"hiqdev/php-units": "*",
"advancedhosters/billing-registry": "dev-master"
},
"require-dev": {
"hiqdev/hidev-php": "dev-master",
Expand Down
84 changes: 3 additions & 81 deletions config/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,87 +141,6 @@
\hipanel\modules\finance\helpers\ConsumptionConfigurator::class => [
'class' => \hipanel\modules\finance\helpers\ConsumptionConfigurator::class,
'configurations' => [
'anycastcdn' => [
'label' => ['hipanel:finance', 'Anycast CDN'],
'columns' => [
'cdn_traf_plain',
'cdn_traf_ssl',
'cdn_traf',
'cdn_traf_max',
'cdn_traf95',
'cdn_traf95_max',
'cdn_cache',
'cdn_cache95',
],
'groups' => [],
'model' => Target::class,
'resourceModel' => TargetResource::class,
],
'videocdn' => [
'label' => ['hipanel:finance', 'VideoCDN'],
'columns' => [
'server_traf',
'server_traf_in',
'server_traf95',
'server_traf95_in',
'server_du',
'server_files',
'server_ssd',
'server_sata',
],
'groups' => [['server_traf', 'server_traf_in'], ['server_traf95', 'server_traf95_in']],
'model' => Target::class,
'resourceModel' => TargetResource::class,
],
'vps' => [
'label' => ['hipanel:finance', 'VPS'],
'columns' => ['vps_traf', 'vps_traf_in', 'vps_traf_max', 'cloud_ip_anycast', 'cloud_ip_public'],
'groups' => [['vps_traf', 'vps_traf_in', 'cloud_ip_anycast', 'cloud_ip_public']],
'model' => Target::class,
'resourceModel' => TargetResource::class,
],
'snapshot' => [
'label' => ['hipanel:finance', 'Snapshot'],
'columns' => ['snapshot_du'],
'groups' => [],
'model' => Target::class,
'resourceModel' => TargetResource::class,
],
'volume' => [
'label' => ['hipanel:finance', 'Volume'],
'columns' => ['volume_du'],
'groups' => [],
'model' => Target::class,
'resourceModel' => TargetResource::class,
],
'storage' => [
'label' => ['hipanel:finance', 'Storage'],
'columns' => ['storage_du', 'storage_du95'],
'groups' => [],
'model' => Target::class,
'resourceModel' => TargetResource::class,
],
'private_cloud' => [
'label' => ['hipanel:finance', 'Private cloud'],
'columns' => [],
'groups' => [],
'model' => Target::class,
'resourceModel' => TargetResource::class,
],
'private_cloud_backup' => [
'label' => ['hipanel:finance', 'Private cloud backup'],
'columns' => ['private_cloud_backup_du'],
'groups' => [],
'model' => Target::class,
'resourceModel' => TargetResource::class,
],
'load_balancer' => [
'label' => ['hipanel:finance', 'Load balancer'],
'columns' => ['lb_capacity_unit', 'lb_ha_capacity_unit'],
'groups' => [],
'model' => Target::class,
'resourceModel' => TargetResource::class,
],
'tariff' => [
'label' => ['hipanel:finance', 'Tariff resources'],
'columns' => ['server_traf95_max', 'server_traf95', 'server_traf95_in'],
Expand Down Expand Up @@ -290,6 +209,9 @@
},
\Money\MoneyParser::class => \Money\Parser\DecimalMoneyParser::class,
// \hipanel\modules\finance\helpers\ConsumptionConfigurator::class => \hipanel\modules\finance\helpers\ConsumptionConfigurator::class,
\hiqdev\php\billing\product\BillingRegistryInterface::class => static function () {
return \hiqdev\billing\registry\TariffConfiguration::buildRegistry();
},
],
],
];
39 changes: 26 additions & 13 deletions src/helpers/ConsumptionConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,25 @@
use hipanel\modules\finance\models\decorators\ResourceDecoratorInterface;
use hipanel\modules\finance\models\Target;
use hipanel\modules\finance\models\TargetResource;
use hiqdev\billing\registry\behavior\ConsumptionConfigurationBehaviour;
use hiqdev\php\billing\product\BillingRegistryInterface;
use yii\db\ActiveRecordInterface;
use Yii;

final class ConsumptionConfigurator
{
public array $configurations = [];

public function __construct(private readonly BillingRegistryInterface $billingRegistry)
{
}

public function getColumns(string $class): array
{
return $this->getConfigurationByClass($class)['columns'];
}

public function getGroups(string $class): array
private function getGroups(string $class): array
{
$groups = [];
$columns = $this->getColumns($class);
Expand Down Expand Up @@ -97,7 +103,7 @@ public function getAllPossibleColumnsWithLabels(): array
return $result;
}

public function getDecorator(string $class, string $type): ResourceDecoratorInterface
private function getDecorator(string $class, string $type): ResourceDecoratorInterface
{
$config = $this->getConfigurationByClass($class);
$config['resourceModel']->type = $type;
Expand Down Expand Up @@ -138,29 +144,36 @@ public function getFirstAvailableClass(): string
* @param string $class
* @return array{label: string, columns: array, group: array, model: ActiveRecordInterface, resourceModel: ActiveRecordInterface}
*/
private function getConfigurationByClass(string $class): array
public function getConfigurationByClass(string $class): array
{
$fallback = [
'label' => ['hipanel:finance', $class],
'columns' => [],
'groups' => [],
'model' => Target::class,
'resourceModel' => TargetResource::class,
'model' => $this->createObject(Target::class),
'resourceModel' => $this->createObject(TargetResource::class),
];

return $this->getConfigurations()[$class] ?? $fallback;
}

public function getConfigurations(): array
{
return array_map(function (array $config): array {
[$dictionary, $label] = $config['label'];
$config['label'] = Yii::t($dictionary, $label);
$config['model'] = $this->createObject($config['model']);
$config['resourceModel'] = $this->createObject($config['resourceModel']);

return $config;
}, $this->configurations);
$configurations = [];
/** @var ConsumptionConfigurationBehaviour $behavior */
foreach ($this->billingRegistry->getBehaviors(ConsumptionConfigurationBehaviour::class) as $behavior) {
$tariffType = $behavior->getTariffType();

$configurations[$tariffType->name()] = [
'label' => $behavior->getLabel(),
'columns' => $behavior->columns,
'groups' => $behavior->groups,
'model' => $this->createObject($behavior->getModel() ?? Target::class),
'resourceModel' => $this->createObject($behavior->getResourceModel() ?? TargetResource::class),
];
}

return $configurations;
}

private function createObject(string $className, array $params = []): object
Expand Down
6 changes: 5 additions & 1 deletion src/logic/bill/QuantityFormatterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ final class QuantityFormatterFactory implements QuantityFormatterFactoryInterfac
'overuse,snapshot_du' => [FractionQuantityFormatter::class, FractionUnit::SIZE],
];

public function __construct(private readonly IntlFormatter $intlFormatter, private readonly BillTypesProvider $billTypesProvider)
public function __construct(
private readonly IntlFormatter $intlFormatter,
private readonly BillTypesProvider $billTypesProvider,
private readonly BillingRegistryInterface $BillingRegistry,
)
Comment on lines +67 to +71
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

⚠️ Potential issue

After reviewing the complete class implementation, I can now generate the final response:

Property naming issue confirmed and unused dependency detected

  1. The property $BillingRegistry violates PSR naming conventions and should be renamed to $billingRegistry
  2. The BillingRegistryInterface dependency is injected but never used in any of the class methods: create(), forBill(), forCharge(), forConsumption(), createByType(), or fixType()

This appears to be an incomplete implementation. Either:

  • Remove the unused dependency if it's not needed
  • Or implement the intended functionality that requires this dependency
🔗 Analysis chain

Property naming and unused dependency concerns

  1. The property name $BillingRegistry should follow PSR naming conventions and be in camelCase: $billingRegistry
  2. The injected BillingRegistryInterface is never used in any of the class methods

Apply this diff to fix the property naming:

    public function __construct(
        private readonly IntlFormatter $intlFormatter,
        private readonly BillTypesProvider $billTypesProvider,
-       private readonly BillingRegistryInterface $BillingRegistry,
+       private readonly BillingRegistryInterface $billingRegistry,
    )

Let's verify the usage of BillingRegistryInterface in this class:


Let me gather more context about this class to ensure a comprehensive verification.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if BillingRegistryInterface is used in the class methods
rg -U "billingRegistry|BillingRegistry" src/logic/bill/QuantityFormatterFactory.php

Length of output: 153


Script:

#!/bin/bash
# Get the full content of the class to understand its methods and usage
cat src/logic/bill/QuantityFormatterFactory.php

Length of output: 5817

{
}

Expand Down
13 changes: 2 additions & 11 deletions src/models/ClientResource.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
<?php
<?php declare(strict_types=1);

namespace hipanel\modules\finance\models;

use hipanel\modules\finance\models\decorators\client\ClientResourceDecoratorFactory;
use hipanel\modules\finance\models\decorators\DecoratedInterface;
use hipanel\modules\finance\models\decorators\ResourceDecoratorInterface;

class ClientResource extends Resource implements DecoratedInterface
{
public function decorator(): ResourceDecoratorInterface
{
if (empty($this->decorator)) {
$this->decorator = ClientResourceDecoratorFactory::createFromResource($this);
}

return $this->decorator;
}
use HasDecorator;
}
20 changes: 20 additions & 0 deletions src/models/HasDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types=1);

namespace hipanel\modules\finance\models;

use hipanel\modules\finance\models\decorators\ResourceDecoratorFactory;
use hipanel\modules\finance\models\decorators\ResourceDecoratorInterface;

trait HasDecorator
{
private ResourceDecoratorInterface $decorator;

public function decorator(): ResourceDecoratorInterface
{
if (empty($this->decorator)) {
$this->decorator = ResourceDecoratorFactory::createFromResource($this);
}

return $this->decorator;
}
}
15 changes: 2 additions & 13 deletions src/models/ServerResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@

use hipanel\base\ModelTrait;
use hipanel\modules\finance\models\decorators\DecoratedInterface;
use hipanel\modules\finance\models\decorators\ResourceDecoratorInterface;
use hipanel\modules\finance\models\decorators\server\AbstractServerResourceDecorator;
use hipanel\modules\finance\models\decorators\server\ServerResourceDecoratorFactory;
use Yii;
use yii\base\InvalidConfigException;

/**
* Class ServerResource.
Expand All @@ -29,6 +25,8 @@ class ServerResource extends Resource implements DecoratedInterface
{
use ModelTrait;

use HasDecorator;

public static function tableName()
{
return 'resource';
Expand Down Expand Up @@ -116,15 +114,6 @@ public function getMinimumQuantity()
return isset($types[$this->type]) ? $types[$this->type] : 0.01;
}

public function decorator(): ResourceDecoratorInterface
{
if (empty($this->decorator)) {
$this->decorator = ServerResourceDecoratorFactory::createFromResource($this);
}

return $this->decorator;
}

public function realObjectId()
{
if (!$this->isPeriodic()) {
Expand Down
13 changes: 2 additions & 11 deletions src/models/TargetResource.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
<?php
<?php declare(strict_types=1);

namespace hipanel\modules\finance\models;

use hipanel\modules\finance\models\decorators\DecoratedInterface;
use hipanel\modules\finance\models\decorators\ResourceDecoratorInterface;
use hipanel\modules\finance\models\decorators\target\TargetResourceDecoratorFactory;

class TargetResource extends Resource implements DecoratedInterface
{
public function decorator(): ResourceDecoratorInterface
{
if (empty($this->decorator)) {
$this->decorator = TargetResourceDecoratorFactory::createFromResource($this);
}

return $this->decorator;
}
use HasDecorator;
}
29 changes: 19 additions & 10 deletions src/models/decorators/ResourceDecoratorFactory.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<?php
<?php declare(strict_types=1);

namespace hipanel\modules\finance\models\decorators;

use hiqdev\billing\registry\behavior\ResourceDecoratorBehavior;
use hiqdev\billing\registry\ResourceDecorator\ResourceDecoratorData;
use hiqdev\billing\registry\TariffConfiguration;
use hiqdev\php\billing\product\BehaviorNotFoundException;
use yii\base\InvalidConfigException;

class ResourceDecoratorFactory
{
protected static function typeMap(): array
{
return [];
}

/**
* @param $resource
* @return ResourceDecoratorInterface
Expand All @@ -19,12 +18,22 @@ protected static function typeMap(): array
public static function createFromResource($resource): ResourceDecoratorInterface
{
$type = $resource->model_type ?? $resource->type;
$map = static::typeMap();

if (!isset($map[$type])) {
$registry = TariffConfiguration::buildRegistry();

try {
/** @var ResourceDecoratorBehavior $behavior */
$behavior = $registry->getBehavior($type, ResourceDecoratorBehavior::class);

return $behavior->createDecorator(new ResourceDecoratorData(
$resource->quantity,
$resource->price,
$resource->unit,
$resource->currency,
$resource->partno,
));
} catch (BehaviorNotFoundException) {
throw new InvalidConfigException('No representative decoration class found for type "' . $type . '"');
}

return new $map[$type]($resource);
}
}
34 changes: 0 additions & 34 deletions src/models/decorators/client/ClientResourceDecoratorFactory.php

This file was deleted.

Loading