PHP code example of tourze / credit-resource-bundle

1. Go to this page and download the library: Download tourze/credit-resource-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tourze / credit-resource-bundle example snippets


// config/bundles.php
return [
    // ...
    CreditResourceBundle\CreditResourceBundle::class => ['all' => true],
];

use CreditResourceBundle\Entity\ResourcePrice;
use CreditResourceBundle\Enum\FeeCycle;

$resourcePrice = new ResourcePrice();
$resourcePrice->setTitle('VPN Usage');
$resourcePrice->setResource('App\Entity\VpnSession'); 
$resourcePrice->setCycle(FeeCycle::NEW_BY_HOUR);
$resourcePrice->setPrice('0.01');
$resourcePrice->setValid(true);

$entityManager->persist($resourcePrice);
$entityManager->flush();

// Simple fixed price per unit
$resourcePrice->setBillingStrategy(null); // Uses default fixed strategy
$resourcePrice->setPrice('1.00'); // $1 per unit

$resourcePrice->setBillingStrategy(TieredPriceStrategy::class);
$resourcePrice->setPriceRules([
    ['min' => 0, 'max' => 100, 'price' => '1.00'],     // First 100 units: $1 each
    ['min' => 100, 'max' => 1000, 'price' => '0.80'],  // Next 900 units: $0.80 each
    ['min' => 1000, 'max' => PHP_INT_MAX, 'price' => '0.50'] // 1000+: $0.50 each
]);

// Calculate new items added in the current hour
$resourcePrice->setCycle(FeeCycle::NEW_BY_HOUR);

// Calculate total items accumulated by end of day
$resourcePrice->setCycle(FeeCycle::TOTAL_BY_DAY);

// Set free quota (first 100 units free)
$resourcePrice->setFreeQuota(100);

// Set minimum charge (at least $5 even if usage is low)
$resourcePrice->setBottomPrice('5.00');

// Set maximum charge (never more than $100)
$resourcePrice->setTopPrice('100.00');

use CreditResourceBundle\Interface\ResourceUsageProviderInterface;
use Symfony\Component\Security\Core\User\UserInterface;

class CustomUsageProvider implements ResourceUsageProviderInterface
{
    public function supports(string $resourceType): bool
    {
        return $resourceType === 'custom_resource';
    }
    
    public function getUsage(
        UserInterface $user,
        string $resourceType,
        \DateTimeInterface $start,
        \DateTimeInterface $end
    ): int {
        // Custom usage calculation logic
        return $this->calculateCustomUsage($user, $start, $end);
    }
    
    public function getUsageDetails(
        UserInterface $user,
        string $resourceType,
        \DateTimeInterface $start,
        \DateTimeInterface $end
    ): array {
        // Return detailed usage information
        return [];
    }
    
    public function getPriority(): int
    {
        return 0;
    }
}

use CreditResourceBundle\Interface\BillingStrategyInterface;
use CreditResourceBundle\Entity\ResourcePrice;

class VolumeDiscountStrategy implements BillingStrategyInterface
{
    public function calculate(ResourcePrice $price, int $usage, array $context = []): string
    {
        $basePrice = bcmul($price->getPrice(), (string) $usage, 5);
        
        // Apply volume discount
        if ($usage > 1000) {
            $discount = bcmul($basePrice, '0.10', 5); // 10% discount
            return bcsub($basePrice, $discount, 5);
        }
        
        return $basePrice;
    }
    
    public function supports(ResourcePrice $price): bool
    {
        return true;
    }
    
    public function getName(): string
    {
        return 'volume_discount';
    }
    
    public function getDescription(): string
    {
        return 'Volume discount strategy with 10% discount for usage over 1000 units';
    }
    
    public function validateConfiguration(ResourcePrice $price): array
    {
        return [];
    }
    
    public function getPriority(): int
    {
        return 0;
    }
}
bash
php bin/console doctrine:migrations:migrate
bash
# Manual billing execution
php bin/console billing:create-resource-bill

# Or configure as cron job (runs at 1st minute of every hour)
# 1 * * * * php /path/to/project/bin/console billing:create-resource-bill