-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CachingRepository decorator clean-up
- Loading branch information
1 parent
5eef4bb
commit 2a32fba
Showing
1 changed file
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |