Move calculations and database interactions out of DashboardController and create DashboardRepository
MODIFY
app/Http/Controllers/DashboardController.php
app/Http/Controllers/DashboardController.php
@@ -9,53 +9,40 @@ use Illuminate\Http\Request;
use App\Models\Earning;
use App\Models\Recurring;
use App\Models\Spending;
+use App\Repositories\DashboardRepository;
use Auth;
use DB;
class DashboardController extends Controller {
+ private $dashboardRepository;
+ private $tagRepository;
+
+ public function __construct(DashboardRepository $dashboardRepository, TagRepository $tagRepository)
+ {
+ $this->dashboardRepository = $dashboardRepository;
+ $this->tagRepository = $tagRepository;
+ }
+
public function __invoke() {
$space_id = session('space')->id;
$currentYear = date('Y');
$currentMonth = date('m');
$daysInMonth = cal_days_in_month(CAL_GREGORIAN, $currentMonth, $currentYear);
- $balance = session('space')->monthlyBalance($currentYear, $currentMonth);
- $recurrings = session('space')->monthlyRecurrings($currentYear, $currentMonth);
- $leftToSpend = max($balance - $recurrings, 0);
-
- $totalSpent = session('space')->spendings()->whereRaw('YEAR(happened_on) = ? AND MONTH(happened_on) = ?', [$currentYear, $currentMonth])->sum('amount');
-
- $tagRepository = new TagRepository();
- $mostExpensiveTags = $tagRepository->getMostExpensiveTags($space_id, 3, $currentYear, $currentMonth);
-
- $balanceTick = 0;
- $dailyBalance = [];
- for ($i = 1; $i <= $daysInMonth; $i ++) {
- $balanceTick -= session('space')
- ->spendings()
- ->where('happened_on', $currentYear . '-' . $currentMonth . '-' . $i)
- ->sum('amount');
-
- $balanceTick += session('space')
- ->earnings()
- ->where('happened_on', $currentYear . '-' . $currentMonth . '-' . $i)
- ->sum('amount');
-
- $dailyBalance[$i] = Helper::formatNumber($balanceTick / 100);
- }
+ $mostExpensiveTags = $this->tagRepository->getMostExpensiveTags($space_id, 3, $currentYear, $currentMonth);
return view('dashboard', [
'month' => date('n'),
- 'balance' => $balance,
- 'recurrings' => $recurrings,
- 'leftToSpend' => $leftToSpend,
+ 'balance' => $this->dashboardRepository->getBalance($currentYear, $currentMonth),
+ 'recurrings' => $this->dashboardRepository->getRecurrings($currentYear, $currentMonth),
+ 'leftToSpend' => $this->dashboardRepository->getLeftToSpend($currentYear, $currentMonth),
- 'totalSpent' => $totalSpent,
+ 'totalSpent' => $this->dashboardRepository->getTotalAmountSpent($currentYear, $currentMonth),
'mostExpensiveTags' => $mostExpensiveTags,
'daysInMonth' => $daysInMonth,
- 'dailyBalance' => $dailyBalance
+ 'dailyBalance' => $this->dashboardRepository->getDailyBalance($currentYear, $currentMonth)
]);
}
}

ADD
app/Repositories/DashboardRepository.php
+<?php
+
+namespace App\Repositories;
+
+use App\Helper;
+
+class DashboardRepository
+{
+ public function getBalance(string $year, string $month)
+ {
+ return session('space')->monthlyBalance($year, $month);
+ }
+
+ public function getRecurrings(string $year, string $month)
+ {
+ return session('space')->monthlyRecurrings($year, $month);
+ }
+
+ public function getLeftToSpend(string $year, string $month)
+ {
+ $balance = $this->getBalance($year, $month);
+ $sumRecurrings = $this->getRecurrings($year, $month);
+
+ return max($balance - $sumRecurrings, 0);
+ }
+
+ // TODO MOVE TO SPENDINGREPOSITORY IN FUTURE
+ public function getTotalAmountSpent(string $year, string $month)
+ {
+ return session('space')
+ ->spendings()
+ ->whereRaw('YEAR(happened_on) = ? AND MONTH(happened_on) = ?', [$year, $month])
+ ->sum('amount');
+ }
+
+ public function getDailyBalance(string $year, string $month): array
+ {
+ $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
+
+ $balanceTick = 0;
+ $dailyBalance = [];
+
+ for ($i = 1; $i <= $daysInMonth; $i ++) {
+ $balanceTick -= session('space')
+ ->spendings()
+ ->where('happened_on', $year . '-' . $month . '-' . $i)
+ ->sum('amount');
+
+ $balanceTick += session('space')
+ ->earnings()
+ ->where('happened_on', $year . '-' . $month . '-' . $i)
+ ->sum('amount');
+
+ $dailyBalance[$i] = Helper::formatNumber($balanceTick / 100);
+ }
+
+ return $dailyBalance;
+ }
+}

Move calculations and database interactions out of DashboardController and create DashboardRepository