1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use App\Judite\Models\Exchange; |
7
|
|
|
use Illuminate\Support\Facades\DB; |
8
|
|
|
use Illuminate\Support\Facades\Mail; |
9
|
|
|
use App\Mail\DeclinedExchangeNotification; |
10
|
|
|
use App\Mail\ConfirmedExchangeNotification; |
11
|
|
|
use Illuminate\Auth\Access\AuthorizationException; |
12
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
13
|
|
|
|
14
|
|
|
class ExchangeController extends Controller |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Create a new controller instance. |
18
|
|
|
*/ |
19
|
|
|
public function __construct() |
20
|
|
|
{ |
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\Response |
30
|
|
|
*/ |
31
|
|
View Code Duplication |
public function confirm($id) |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$exchange = DB::transaction(function () use ($id) { |
34
|
|
|
$exchange = Exchange::findOrFail($id); |
35
|
|
|
$this->checkDecideAuthorization($exchange); |
36
|
|
|
|
37
|
|
|
return $exchange->perform(); |
38
|
|
|
}); |
39
|
|
|
|
40
|
|
|
flash('The shift exchange request was successfully confirmed.')->success(); |
41
|
|
|
Mail::to($exchange->fromStudent()->user) |
42
|
|
|
->send(new ConfirmedExchangeNotification($exchange)); |
43
|
|
|
|
44
|
|
|
return redirect()->back(); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Store a decline of an exchange in storage. |
49
|
|
|
* |
50
|
|
|
* @param int $id |
51
|
|
|
* |
52
|
|
|
* @return \Illuminate\Http\Response |
53
|
|
|
*/ |
54
|
|
View Code Duplication |
public function decline($id) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$exchange = DB::transaction(function () use ($id) { |
57
|
|
|
$exchange = Exchange::findOrFail($id); |
58
|
|
|
$this->checkDecideAuthorization($exchange); |
59
|
|
|
$exchange->delete(); |
60
|
|
|
|
61
|
|
|
return $exchange; |
62
|
|
|
}); |
63
|
|
|
|
64
|
|
|
flash('The shift exchange request was successfully declined.')->success(); |
65
|
|
|
Mail::to($exchange->fromStudent()->user) |
66
|
|
|
->send(new DeclinedExchangeNotification($exchange)); |
67
|
|
|
|
68
|
|
|
return redirect()->back(); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Checks authorization of the authenticated user to decide on the given exchange. |
73
|
|
|
* |
74
|
|
|
* @param \App\Judite\Models\Exchange $exchange |
75
|
|
|
* |
76
|
|
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
77
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
78
|
|
|
*/ |
79
|
|
|
private function checkDecideAuthorization(Exchange $exchange) |
80
|
|
|
{ |
81
|
|
|
// We are going to hide the third-party enrollments from user that |
82
|
|
|
// sent the request. If they try to confirm their own exchanges |
83
|
|
|
// a 403 will be returned, otherwise a 404 will be simulated. |
84
|
|
|
try { |
85
|
|
|
$this->authorize('decide', $exchange); |
86
|
|
|
} catch (AuthorizationException $e) { |
87
|
|
|
throw $exchange->isOwnedBy(auth()->user()->student) |
88
|
|
|
? $e |
89
|
|
|
: new ModelNotFoundException(get_class($exchange), $exchange->getKey()); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
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.