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
2 changes: 1 addition & 1 deletion app/Data/Billing/CreditPackDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function __construct(
public string $key,
public string $label,
public int $credits,
public string $stripePriceId,
public ?string $stripePriceId,
public string $description,
) {}

Expand Down
51 changes: 51 additions & 0 deletions app/Services/Billing/CreditPackRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace App\Services\Billing;

use App\Data\Billing\CreditPackDto;
use Illuminate\Support\Collection;

final class CreditPackRepository
{
/** @return Collection<int, CreditPackDto> */
public function all(): Collection
{
return collect($this->packsConfig())
->map(fn (array $config, string $key) => CreditPackDto::fromConfig($key, $config))
->values();
}

public function find(string $key): ?CreditPackDto
{
$config = $this->packsConfig();

if (! array_key_exists($key, $config)) {
return null;
}

return CreditPackDto::fromConfig($key, $config[$key]);
}

public function findOrFail(string $key): CreditPackDto
{
$pack = $this->find($key);

if ($pack === null) {
throw new \InvalidArgumentException("Unknown credit pack key: {$key}");
}

return $pack;
}

public function findByStripePriceId(string $stripePriceId): ?CreditPackDto
{
return $this->all()->first(fn (CreditPackDto $pack) => $pack->stripePriceId === $stripePriceId);
}

private function packsConfig(): array
{
return config('billing.credit_packs', []);
}
}
37 changes: 37 additions & 0 deletions tests/Unit/Billing/CreditPackRepositoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

use App\Data\Billing\CreditPackDto;
use App\Services\Billing\CreditPackRepository;

it('returns all credit packs', function () {
$packs = app(CreditPackRepository::class)->all();

expect($packs)->not->toBeEmpty();
expect($packs->first())->toBeInstanceOf(CreditPackDto::class);
});

it('finds credit pack by key', function () {
$pack = app(CreditPackRepository::class)->findOrFail('small');

expect($pack->key)->toBe('small');
});

it('throws for unknown credit pack key', function () {
app(CreditPackRepository::class)->findOrFail('unknown');
})->throws(InvalidArgumentException::class);

it('finds credit pack by stripe price id', function () {
config()->set('billing.credit_packs.small.stripe_price_id', 'price_small');

$pack = app(CreditPackRepository::class)->findByStripePriceId('price_small');

expect($pack?->key)->toBe('small');
});

it('returns null for unknown stripe price id', function () {
$pack = app(CreditPackRepository::class)->findByStripePriceId('price_nonexistent');

expect($pack)->toBeNull();
});