Skip to content

Commit

Permalink
CachingRepository decorator clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
incredimike committed Aug 3, 2023
1 parent 5eef4bb commit 2a32fba
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions app/Repositories/CachingBudgetRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace App\Repositories;

use App\Interfaces\BudgetRepositoryInterface;
use Cache;

class CachingBudgetRepository implements BudgetRepositoryInterface
{
protected BudgetRepository $repository;
protected string $serviceName = 'Cache';

public function __construct(BudgetRepository $repository)
{
$this->repository = $repository;
}

private function makeCacheKey(string $id): string
{
return sprintf(
'%s.%s.%s',
strtolower($this->repository->getServiceName()),
$this->repository->getBudgetId(),
$id
);
}

public function fetchAccounts(): array
{
return Cache::remember($this->makeCacheKey('accounts'), $minutes = 10, function () {
return $this->repository->fetchAccounts();
});
}

public function fetchBudgets(bool $include_accounts = false): array
{
return Cache::remember($this->makeCacheKey('budgets'), $minutes = 10, function () {
return $this->repository->fetchBudgets();
});
}

public function fetchCategories(): array
{
return Cache::remember($this->makeCacheKey('categories'), $minutes = 10, function () {
return $this->repository->fetchBudgets();
});
}

public function fetchPayees(): array
{
return Cache::remember($this->makeCacheKey('payees'), $minutes = 10, function () {
return $this->repository->fetchPayees();
});
}

public function fetchTransactions(string $sinceDate): array
{
return Cache::remember($this->makeCacheKey('transactions'), $minutes = 10, function () {
return $this->repository->fetchPayees();
});
}

public function getBudgetId(): string
{
return $this->budgetId;
}

public function fetchTransaction(string $id): array
{
$url = sprintf(
$this->serviceUrl . '/budgets/%s/transactions/%s',
$this->budgetId,
$id
);
return $this->fetchJson($url, 'data.transaction');
}

public function getServiceName(): string
{
return $this->serviceName . ' <- ' . $this->repository->getServiceName();
}

public function setBudgetId(string $budgetId): void
{
$this->budgetId = $budgetId;
}
}

0 comments on commit 2a32fba

Please sign in to comment.