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\Events\ExchangeWasDeclined; |
10
|
|
|
use App\Events\ExchangeWasConfirmed; |
11
|
|
|
use Illuminate\Support\Facades\Auth; |
12
|
|
|
use App\Http\Requests\Exchange\CreateRequest; |
13
|
|
|
|
14
|
|
|
class ExchangeController extends Controller |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Create a new controller instance. |
18
|
|
|
*/ |
19
|
|
|
public function __construct() |
20
|
|
|
{ |
21
|
|
|
$this->middleware('auth'); |
22
|
|
|
$this->middleware('can.student'); |
23
|
|
|
$this->middleware('student.verified'); |
24
|
|
|
$this->middleware('can.exchange'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Store a confirmation of an exchange in storage. |
29
|
|
|
* |
30
|
|
|
* @param int $id |
31
|
|
|
* |
32
|
|
|
* @return \Illuminate\Http\RedirectResponse |
33
|
|
|
*/ |
34
|
|
View Code Duplication |
public function confirm($id) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$exchange = DB::transaction(function () use ($id) { |
37
|
|
|
$exchange = Auth::student()->proposedExchanges()->findOrFail($id); |
38
|
|
|
|
39
|
|
|
return $exchange->perform(); |
40
|
|
|
}); |
41
|
|
|
flash('The shift exchange request was successfully confirmed.')->success(); |
42
|
|
|
event(new ExchangeWasConfirmed($exchange)); |
43
|
|
|
|
44
|
|
|
return redirect()->back(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Show the form for creating a new resource. |
49
|
|
|
* |
50
|
|
|
* @param $request |
51
|
|
|
* |
52
|
|
|
* @return \Illuminate\View\View|\Illuminate\Http\RedirectResponse |
53
|
|
|
*/ |
54
|
|
|
public function create(CreateRequest $request) |
55
|
|
|
{ |
56
|
|
|
try { |
57
|
|
|
$id = $request->input('enrollment_id'); |
58
|
|
|
$data = DB::transaction(function () use ($id) { |
59
|
|
|
$enrollment = Auth::student()->enrollments()->findOrFail($id); |
60
|
|
|
|
61
|
|
|
if (! $enrollment->availableForExchange()) { |
62
|
|
|
throw new \LogicException('The enrollment is not available for exchange.'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$matchingEnrollments = Enrollment::similarEnrollments($enrollment) |
66
|
|
|
->orderByStudent() |
67
|
|
|
->get(); |
68
|
|
|
|
69
|
|
|
$course = $enrollment->course; |
70
|
|
|
|
71
|
|
|
$shiftsAvailable = $course->shifts() |
72
|
|
|
->orderBy('tag') |
73
|
|
|
->get() |
74
|
|
|
->except([ |
75
|
|
|
'id' => $enrollment->shift->id, |
76
|
|
|
]) |
77
|
|
|
->pluck('tag'); |
78
|
|
|
|
79
|
|
|
return compact('enrollment', 'matchingEnrollments', 'shiftsAvailable'); |
80
|
|
|
}); |
81
|
|
|
|
82
|
|
|
$data['matchingEnrollments'] = $data['matchingEnrollments']->map(function ($item) { |
83
|
|
|
return [ |
84
|
|
|
'id' => $item->id, |
85
|
|
|
'_toString' => $item->present()->inlineToString(), |
86
|
|
|
]; |
87
|
|
|
}); |
88
|
|
|
|
89
|
|
|
return view('exchanges.create', $data); |
90
|
|
|
} catch (\LogicException $e) { |
91
|
|
|
flash($e->getMessage())->error(); |
92
|
|
|
|
93
|
|
|
return redirect()->route('dashboard'); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Store a decline of an exchange in storage. |
99
|
|
|
* |
100
|
|
|
* @param int $id |
101
|
|
|
* |
102
|
|
|
* @return \Illuminate\Http\RedirectResponse |
103
|
|
|
*/ |
104
|
|
View Code Duplication |
public function decline($id) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$exchange = DB::transaction(function () use ($id) { |
107
|
|
|
$exchange = Auth::student()->proposedExchanges()->findOrFail($id); |
108
|
|
|
$exchange->delete(); |
109
|
|
|
|
110
|
|
|
return $exchange; |
111
|
|
|
}); |
112
|
|
|
flash('The shift exchange request was successfully declined.')->success(); |
113
|
|
|
event(new ExchangeWasDeclined($exchange)); |
114
|
|
|
|
115
|
|
|
return redirect()->back(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Remove the specified resource from storage. |
120
|
|
|
* |
121
|
|
|
* @param int $id |
122
|
|
|
* |
123
|
|
|
* @return \Illuminate\Http\RedirectResponse |
124
|
|
|
*/ |
125
|
|
|
public function destroy($id) |
126
|
|
|
{ |
127
|
|
|
DB::transaction(function () use ($id) { |
128
|
|
|
Auth::student()->requestedExchanges()->findOrFail($id)->delete(); |
129
|
|
|
}); |
130
|
|
|
flash('The shift exchange request was successfully deleted.')->success(); |
131
|
|
|
|
132
|
|
|
return redirect()->back(); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.