Create RecurringRepository
MODIFY
app/Http/Controllers/RecurringController.php
app/Http/Controllers/RecurringController.php
namespace App\Http\Controllers;
-use App\Helper;
use Illuminate\Http\Request;
use App\Models\Recurring;
use Auth;
use App\Jobs\ProcessRecurrings;
+use App\Repositories\RecurringRepository;
class RecurringController extends Controller {
+ private $recurringRepository;
+
+ public function __construct(RecurringRepository $recurringRepository)
+ {
+ $this->recurringRepository = $recurringRepository;
+ }
+
public function index() {
return view('recurrings.index', [
'recurrings' => session('space')->recurrings()->orderBy('created_at', 'DESC')->get()
@@ -33,32 +40,18 @@ class RecurringController extends Controller {
}
public function store(Request $request) {
- $request->validate([
- 'type' => 'required|in:earning,spending',
- 'day' => ['required',
- 'regex:/\b(0?[1-9]|[12][0-9]|3[01])\b/',
- ],
- 'end' => 'nullable|date|date_format:Y-m-d',
- 'tag' => 'nullable|exists:tags,id', // TODO CHECK IF TAG BELONGS TO USER
- 'description' => 'required|max:255',
- 'amount' => 'required|regex:/^\d*(\.\d{2})?$/'
- ]);
-
- $user = Auth::user();
-
- $recurring = new Recurring;
-
- $recurring->space_id = session('space')->id;
- $recurring->type = $request->type;
- $recurring->interval = 'monthly';
- $recurring->day = ltrim($request->input('day'), '0');
- $recurring->starts_on = date('Y-m-d');
- $recurring->ends_on = $request->input('end');
- $recurring->tag_id = $request->input('tag');
- $recurring->description = $request->input('description');
- $recurring->amount = Helper::rawNumberToInteger($request->input('amount'));
+ $request->validate($this->recurringRepository->getValidationRules());
+
+ $recurring = $this->recurringRepository->create(
+ session('space')->id,
+ $request->type,
+ (int) ltrim($request->input('day'), 0),
+ $request->input('end', null),
+ $request->input('tag', null),
+ $request->input('description'),
+ $request->input('amount')
+ );
- $recurring->save();
ProcessRecurrings::dispatch();
return redirect()->route('recurrings.show', ['recurring' => $recurring->id]);

MODIFY
app/Models/Recurring.php
app/Models/Recurring.php
@@ -10,12 +10,20 @@ use Illuminate\Database\Eloquent\SoftDeletes;
class Recurring extends Model {
use SoftDeletes;
- protected $dates = ['deleted_at'];
-
protected $fillable = [
- 'type'
+ 'space_id',
+ 'type',
+ 'interval',
+ 'day',
+ 'starts_on',
+ 'ends_on',
+ 'tag_id',
+ 'description',
+ 'amount'
];
+ protected $dates = ['deleted_at'];
+
protected $dispatchesEvents = [
'created' => RecurringCreated::class,
'deleted' => RecurringDeleted::class

ADD
app/Repositories/RecurringRepository.php
+<?php
+
+namespace App\Repositories;
+
+use App\Helper;
+use App\Models\Recurring;
+use Exception;
+
+class RecurringRepository
+{
+ public function getValidationRules(): array
+ {
+ return [
+ 'type' => 'required|in:earning,spending',
+ 'day' => ['required',
+ 'regex:/\b(0?[1-9]|[12][0-9]|3[01])\b/',
+ ],
+ 'end' => 'nullable|date|date_format:Y-m-d',
+ 'tag' => 'nullable|exists:tags,id', // TODO CHECK IF TAG BELONGS TO USER
+ 'description' => 'required|max:255',
+ 'amount' => 'required|regex:/^\d*(\.\d{2})?$/'
+ ];
+ }
+
+ public function create(int $spaceId, string $type, int $day, ?string $endDate, ?int $tagId, string $description, string $amount): Recurring
+ {
+ if ($type !== 'earning' && $type !== 'spending') {
+ throw new Exception('Unknown type "' . $type . '"');
+ }
+
+ return Recurring::create([
+ 'space_id' => $spaceId,
+ 'type' => $type,
+ 'interval' => 'monthly',
+ 'day' => $day,
+ 'starts_on' => date('Y-m-d'),
+ 'ends_on' => $endDate,
+ 'tag_id' => $tagId,
+ 'description' => $description,
+ 'amount' => Helper::rawNumberToInteger($amount)
+ ]);
+ }
+}

Create RecurringRepository