ExchangeController::confirm()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Judite\Models\Exchange;
6
use Illuminate\Support\Facades\DB;
7
use App\Events\ExchangeWasDeclined;
8
use App\Events\ExchangeWasConfirmed;
9
use Illuminate\Support\Facades\Auth;
10
11
class ExchangeController extends Controller
12
{
13
    /**
14
     * Create a new controller instance.
15
     */
16
    public function __construct()
17
    {
18
        $this->middleware('auth');
19
        $this->middleware('can.student');
20
        $this->middleware('student.verified');
21
        $this->middleware('can.exchange');
22
    }
23
24
    /**
25
     * Store a confirmation of an exchange in storage.
26
     *
27
     * @param int $id
28
     *
29
     * @return \Illuminate\Http\RedirectResponse
30
     */
31
    public function confirm($id)
32
    {
33
        $exchange = DB::transaction(function () use ($id) {
34
            $exchange = Auth::student()->proposedExchanges()->findOrFail($id);
35
36
            return $exchange->perform();
37
        });
38
        flash('The shift exchange request was successfully confirmed.')->success();
39
        event(new ExchangeWasConfirmed($exchange));
0 ignored issues
show
Bug introduced by
It seems like $exchange can also be of type null; however, parameter $exchange of App\Events\ExchangeWasConfirmed::__construct() does only seem to accept App\Judite\Models\Exchange, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
        event(new ExchangeWasConfirmed(/** @scrutinizer ignore-type */ $exchange));
Loading history...
40
41
        return redirect()->back();
42
    }
43
44
    /**
45
     * Store a decline of an exchange in storage.
46
     *
47
     * @param int $id
48
     *
49
     * @return \Illuminate\Http\RedirectResponse
50
     */
51
    public function decline($id)
52
    {
53
        $exchange = DB::transaction(function () use ($id) {
54
            $exchange = Auth::student()->proposedExchanges()->findOrFail($id);
55
            $exchange->delete();
56
57
            return $exchange;
58
        });
59
        flash('The shift exchange request was successfully declined.')->success();
60
        event(new ExchangeWasDeclined($exchange));
0 ignored issues
show
Bug introduced by
It seems like $exchange can also be of type null; however, parameter $exchange of App\Events\ExchangeWasDeclined::__construct() does only seem to accept App\Judite\Models\Exchange, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
        event(new ExchangeWasDeclined(/** @scrutinizer ignore-type */ $exchange));
Loading history...
61
62
        return redirect()->back();
63
    }
64
65
    /**
66
     * Remove the specified resource from storage.
67
     *
68
     * @param int $id
69
     *
70
     * @return \Illuminate\Http\RedirectResponse
71
     */
72
    public function destroy($id)
73
    {
74
        DB::transaction(function () use ($id) {
75
            Auth::student()->requestedExchanges()->findOrFail($id)->delete();
76
        });
77
        flash('The shift exchange request was successfully deleted.')->success();
78
79
        return redirect()->back();
80
    }
81
}
82