From 2a32fbac61d4a94b09c10bda4fe941a9fd71f322 Mon Sep 17 00:00:00 2001 From: Mike Walker Date: Thu, 3 Aug 2023 01:08:00 -0700 Subject: [PATCH] CachingRepository decorator clean-up --- app/Repositories/CachingBudgetRepository.php | 87 ++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 app/Repositories/CachingBudgetRepository.php diff --git a/app/Repositories/CachingBudgetRepository.php b/app/Repositories/CachingBudgetRepository.php new file mode 100644 index 0000000..c2b3926 --- /dev/null +++ b/app/Repositories/CachingBudgetRepository.php @@ -0,0 +1,87 @@ +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; + } +}