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

ExchangeController::decline()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 1
dl 15
loc 15
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 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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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();
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...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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();
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...
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());
0 ignored issues
show
Bug introduced by
It seems like $exchange->getKey() can also be of type Carbon\Carbon and string and boolean and double and Illuminate\Support\Collection; however, parameter $code of Illuminate\Database\Eloq...xception::__construct() does only seem to accept integer, 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

89
                : new ModelNotFoundException(get_class($exchange), /** @scrutinizer ignore-type */ $exchange->getKey());
Loading history...
90
        }
91
    }
92
}
93