Completed
Push — master ( 5bb2ae...adea1c )
by Terzi
04:11
created

AdminController::createGateUnauthorizedException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 8
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Terranet\Administrator\Controllers;
4
5
use App\Http\Controllers\Controller as BaseController;
6
use Illuminate\Http\Request;
7
use Illuminate\Translation\Translator;
8
use Symfony\Component\HttpKernel\Exception\HttpException;
9
use Terranet\Administrator\Middleware\Authenticate;
10
use Terranet\Administrator\Middleware\AuthProvider;
11
use Terranet\Administrator\Middleware\Resources;
12
13
abstract class AdminController extends BaseController
14
{
15
    /**
16
     * @var null|\Illuminate\Translation\Translator
17
     */
18
    protected $translator;
19
20 6
    public function __construct(Translator $translator)
21
    {
22 6
        $this->middleware([
23 6
            AuthProvider::class,
24
            Authenticate::class,
25
            Resources::class,
26
        ]);
27
28 6
        $this->translator = $translator;
29 6
    }
30
31
    /**
32
     * Authorize a given action against a set of arguments.
33
     *
34
     * @param mixed $ability
35
     * @param array|mixed $arguments
36
     *
37
     * @return bool
38
     */
39 2
    public function authorize($ability, $arguments = [])
40
    {
41 2
        if (!$response = app('scaffold.actions')->authorize($ability, $arguments)) {
0 ignored issues
show
Bug introduced by
The method authorize() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

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

41
        if (!$response = app('scaffold.actions')->/** @scrutinizer ignore-call */ authorize($ability, $arguments)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42 1
            throw $this->createGateUnauthorizedException(
43 1
                $ability,
44 1
                $this->translator->trans('administrator::errors.unauthorized')
0 ignored issues
show
Bug introduced by
The method trans() does not exist on null. ( Ignorable by Annotation )

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

44
                $this->translator->/** @scrutinizer ignore-call */ 
45
                                   trans('administrator::errors.unauthorized')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
            );
46
        }
47
48 1
        return $response;
49
    }
50
51 3
    protected function redirectTo($module, $key, Request $request)
52
    {
53 3
        if ($next = $request->get('back_to')) {
54 1
            return redirect()->to($next);
55
        }
56
57 2
        if ($request->exists('save')) {
58 1
            return redirect()->route('scaffold.edit', ['module' => $module, 'id' => $key]);
59
        }
60
61 1
        return redirect()->route(
62 1
            $request->exists('save_return') ? 'scaffold.index' : 'scaffold.create',
63 1
            ['module' => $module]
64
        );
65
    }
66
67
    /**
68
     * Throw an unauthorized exception based on gate results.
69
     *
70
     * @param  string $ability
71
     * @param  string $message
72
     * @param  \Exception $previousException
73
     *
74
     * @return \Symfony\Component\HttpKernel\Exception\HttpException
75
     */
76 1
    protected function createGateUnauthorizedException(
77
        $ability,
78
        $message = 'This action is unauthorized.',
79
        $previousException = null
80
    ) {
81 1
        $message = sprintf($message.' [%s]', $ability);
82
83 1
        return new HttpException(403, $message, $previousException);
84
    }
85
}
86