Passed
Push — develop ( a8f030...cce3fe )
by Francisco
02:56
created

EnrollmentExchangeController::destroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use App\Judite\Models\Exchange;
7
use App\Judite\Models\Enrollment;
8
use Illuminate\Support\Facades\DB;
9
use App\Http\Requests\Exchange\CreateRequest;
10
use App\Exceptions\EnrollmentCannotBeExchangedException;
11
use App\Exceptions\ExchangeEnrollmentsOnDifferentCoursesException;
12
13
class EnrollmentExchangeController extends Controller
14
{
15
    /**
16
     * Create a new controller instance.
17
     */
18
    public function __construct()
19
    {
20
        $this->middleware('can.exchange');
21
    }
22
23
    /**
24
     * Show the form for creating a new resource.
25
     *
26
     * @param int $enrollmentId
27
     *
28
     * @return \Illuminate\Http\Response
29
     */
30
    public function create($enrollmentId)
31
    {
32
        try {
33
            $data = DB::transaction(function () use ($enrollmentId) {
34
                $enrollment = Enrollment::ownedBy(auth()->user()->student)
35
                    ->findOrFail($enrollmentId);
36
37
                if ($enrollment->availableForExchange()) {
38
                    throw new \LogicException('The enrollment is not available for exchange.');
39
                }
40
41
                $matchingEnrollments = Enrollment::similarEnrollments($enrollment)
42
                    ->orderByStudent()
43
                    ->get();
44
45
                return compact('enrollment', 'matchingEnrollments');
46
            });
47
48
            $data['matchingEnrollments'] = $data['matchingEnrollments']->map(function ($item) {
49
                return [
50
                    'id' => $item->id,
51
                    '_toString' => $item->present()->inlineToString(),
52
                ];
53
            });
54
55
            return view('exchanges.create', $data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('exchanges.create', $data) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
56
        } catch (\LogicException $e) {
57
            flash($e->getMessage())->error();
58
59
            return redirect()->route('home');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route('home') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
60
        }
61
    }
62
63
    /**
64
     * Store a newly created resource in storage.
65
     *
66
     * @param int                                       $enrollmentId
67
     * @param \App\Http\Requests\Exchange\CreateRequest $request
68
     *
69
     * @return \Illuminate\Http\Response
70
     */
71
    public function store($enrollmentId, CreateRequest $request)
72
    {
73
        try {
74
            DB::transaction(function () use ($enrollmentId, $request) {
75
                $this->validate($request, [
76
                    'to_enrollment_id' => 'exists:enrollments,id',
77
                ]);
78
79
                $fromEnrollment = Enrollment::ownedBy(auth()->user()->student)->findOrFail($enrollmentId);
80
                $toEnrollment = Enrollment::find($request->input('to_enrollment_id'));
81
82
                // Firstly check if the inverse exchange for the same enrollments
83
                // already exists. If the inverse record is found then we will
84
                // exchange and update both enrollments of this exchange.
85
                if ($exchange = Exchange::findMatchingExchange($fromEnrollment, $toEnrollment)) {
86
                    return $exchange->perform();
87
                }
88
89
                // Otherwise, we create a new exchange between both enrollments
90
                // so the user that owns the target enrollment can confirm the
91
                // exchange and allow the other user to enroll on the shift.
92
                $exchange = Exchange::make();
93
                $exchange->setExchangeEnrollments($fromEnrollment, $toEnrollment);
94
                $exchange->save();
95
96
                return $exchange;
97
            });
98
99
            flash('The exchange was successfully proposed.')->success();
100
        } catch (EnrollmentCannotBeExchangedException | ExchangeEnrollmentsOnDifferentCoursesException $e) {
101
            flash($e->getMessage())->error();
102
        }
103
104
        return redirect()->route('home');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route('home') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
105
    }
106
107
    /**
108
     * Remove the specified resource from storage.
109
     *
110
     * @param int $id
111
     *
112
     * @return \Illuminate\Http\Response
113
     */
114
    public function destroy($id)
115
    {
116
        DB::transaction(function () use ($id) {
117
            $exchange = Exchange::ownedBy(auth()->user()->student)
118
                ->findOrFail($id);
119
120
            $exchange->delete();
121
122
            return $exchange;
123
        });
124
125
        flash('The shift exchange request was successfully deleted.')->success();
126
127
        return redirect()->back();
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back() returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
128
    }
129
}
130