Make first step towards using repository design by creating TransactionRepository
MODIFY
app/Http/Controllers/TransactionController.php
app/Http/Controllers/TransactionController.php
namespace App\Http\Controllers;
-use Carbon\Carbon;
+use App\Repositories\TransactionRepository;
use Illuminate\Http\Request;
class TransactionController extends Controller {
- public function index(Request $request) {
- $filterBy = $request->get('filterBy') ? explode('-', $request->get('filterBy')) : null;
-
- $yearMonths = [];
-
- // Populate yearMonths with earnings
- foreach (session('space')->earnings as $earning) {
- $shouldAdd = false;
-
- if (!$filterBy) {
- $shouldAdd = true;
- }
-
- if ($shouldAdd) {
- $i = Carbon::parse($earning->happened_on)->format('Y-m');
-
- if (!isset($yearMonths[$i])) {
- $yearMonths[$i] = [];
- }
-
- $yearMonths[$i][] = $earning;
- }
- }
-
- // Populate yearMonths with spendings
- foreach (session('space')->spendings as $spending) {
- $shouldAdd = true;
-
- // Filter
- if ($filterBy[0] == 'tag') {
- if (!$spending->tag || $spending->tag->id != $filterBy[1]) {
- $shouldAdd = false;
- }
- }
-
- if ($shouldAdd) {
- $i = Carbon::parse($spending->happened_on)->format('Y-m');
-
- if (!isset($yearMonths[$i])) {
- $yearMonths[$i] = [];
+ public function __construct(TransactionRepository $transactionRepository) {
+ $this->repository = $transactionRepository;
}
- $yearMonths[$i][] = $spending;
- }
- }
+ public function index(Request $request) {
+ $filterBy = null;
- // Sort transactions
- foreach ($yearMonths as &$yearMonth) {
- usort($yearMonth, function ($a, $b) {
- return $a->happened_on < $b->happened_on;
- });
+ if ($request->get('filterBy')) {
+ $filterBy = explode('-', $request->get('filterBy'));
}
- // Sort yearMonths
- krsort($yearMonths);
-
- $tags = session('space')->tags;
-
- return view('transactions.index', compact('yearMonths', 'tags'));
+ return view('transactions.index', [
+ 'yearMonths' => $this->repository->getTransactionsByYearMonth($filterBy),
+ 'tags' => session('space')->tags
+ ]);
}
public function create() {

ADD
app/Repositories/TransactionRepository.php
+<?php
+
+namespace App\Repositories;
+
+use Carbon\Carbon;
+
+class TransactionRepository {
+ public function getTransactionsByYearMonth($filterBy = null) {
+ $yearMonths = [];
+
+ // Populate yearMonths with earnings
+ foreach (session('space')->earnings as $earning) {
+ $shouldAdd = false;
+
+ if (!$filterBy) {
+ $shouldAdd = true;
+ }
+
+ if ($shouldAdd) {
+ $i = Carbon::parse($earning->happened_on)->format('Y-m');
+
+ if (!isset($yearMonths[$i])) {
+ $yearMonths[$i] = [];
+ }
+
+ $yearMonths[$i][] = $earning;
+ }
+ }
+
+ // Populate yearMonths with spendings
+ foreach (session('space')->spendings as $spending) {
+ $shouldAdd = true;
+
+ // Filter
+ if ($filterBy[0] == 'tag') {
+ if (!$spending->tag || $spending->tag->id != $filterBy[1]) {
+ $shouldAdd = false;
+ }
+ }
+
+ if ($shouldAdd) {
+ $i = Carbon::parse($spending->happened_on)->format('Y-m');
+
+ if (!isset($yearMonths[$i])) {
+ $yearMonths[$i] = [];
+ }
+
+ $yearMonths[$i][] = $spending;
+ }
+ }
+
+ // Sort transactions
+ foreach ($yearMonths as &$yearMonth) {
+ usort($yearMonth, function ($a, $b) {
+ return $a->happened_on < $b->happened_on;
+ });
+ }
+
+ // Sort yearMonths
+ krsort($yearMonths);
+
+ return $yearMonths;
+ }
+}

Make first step towards using repository design by creating TransactionRepository