AdminController::createGateUnauthorizedException()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 2
b 0
f 0
nc 1
nop 3
dl 0
loc 8
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Terranet\Administrator\Controllers;
4
5
use App\Http\Controllers\Controller as BaseController;
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Http\Request;
7
use Illuminate\Translation\Translator;
0 ignored issues
show
Bug introduced by
The type Illuminate\Translation\Translator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Symfony\Component\HttpKernel\Exception\HttpException;
9
use Terranet\Administrator\Contracts\Module;
10
use Terranet\Administrator\Middleware\Authenticate;
11
use Terranet\Administrator\Middleware\AuthProvider;
12
use Terranet\Administrator\Middleware\Resources;
13
14
abstract class AdminController extends BaseController
15
{
16
    public function __construct()
17
    {
18
        $this->middleware([
19
            AuthProvider::class,
20 6
            Authenticate::class,
21
            Resources::class,
22 6
        ]);
23 6
    }
24
25
    /**
26
     * Authorize a given action against a set of arguments.
27
     *
28 6
     * @param  mixed  $ability
29 6
     * @param  array|mixed  $arguments
30
     * @return bool
31
     */
32
    public function authorize($ability, $arguments = null)
33
    {
34
        /** @var Module $resource */
35
        $resource = app('scaffold.module');
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

35
        $resource = /** @scrutinizer ignore-call */ app('scaffold.module');
Loading history...
36
37
        if (!$response = $resource->actions()->authorize($ability, $arguments)) {
0 ignored issues
show
Bug introduced by
It seems like $arguments can also be of type array; however, parameter $model of Terranet\Administrator\C...onsManager::authorize() does only seem to accept Illuminate\Database\Eloquent\Model|null, 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

37
        if (!$response = $resource->actions()->authorize($ability, /** @scrutinizer ignore-type */ $arguments)) {
Loading history...
38
            throw $this->createGateUnauthorizedException(
39 2
                $ability,
40
                trans('administrator::errors.unauthorized')
0 ignored issues
show
Bug introduced by
The function trans was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

40
                /** @scrutinizer ignore-call */ 
41
                trans('administrator::errors.unauthorized')
Loading history...
41 2
            );
42 1
        }
43 1
44 1
        return $response;
45
    }
46
47
    protected function redirectTo($module, $key, Request $request)
48 1
    {
49
        if ($next = $request->get('back_to')) {
50
            return redirect()->to($next);
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

50
            return /** @scrutinizer ignore-call */ redirect()->to($next);
Loading history...
51 3
        }
52
53 3
        if ($request->exists('save')) {
54 1
            return redirect()->route('scaffold.edit', ['module' => $module, 'id' => $key]);
55
        }
56
57 2
        return redirect()->route(
58 1
            $request->exists('save_return') ? 'scaffold.index' : 'scaffold.create',
59
            ['module' => $module]
60
        );
61 1
    }
62 1
63 1
    /**
64
     * Throw an unauthorized exception based on gate results.
65
     *
66
     * @param  string  $ability
67
     * @param  string  $message
68
     * @param  \Exception  $previousException
69
     * @return \Symfony\Component\HttpKernel\Exception\HttpException
70
     */
71
    protected function createGateUnauthorizedException(
72
        $ability,
73
        $message = 'This action is unauthorized.',
74
        $previousException = null
75
    ) {
76 1
        $message = sprintf($message.' [%s]', $ability);
77
78
        return new HttpException(403, $message, $previousException);
79
    }
80
81 1
    public function translator()
82
    {
83 1
        return app(Translator::class);
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

83
        return /** @scrutinizer ignore-call */ app(Translator::class);
Loading history...
84
    }
85
}
86