GdaemonTasksController   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 6
eloc 34
dl 0
loc 77
ccs 0
cts 16
cp 0
rs 10
c 1
b 1
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A output() 0 11 1
A __construct() 0 6 1
A get() 0 21 3
A list() 0 8 1
1
<?php
2
3
namespace Gameap\Http\Controllers\API;
4
5
use Gameap\Helpers\PermissionHelper;
6
use Gameap\Helpers\ServerPermissionHelper;
7
use Gameap\Http\Controllers\AuthController;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Gameap\Http\Controllers\API\AuthController. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
use Gameap\Models\GdaemonTask;
9
use Gameap\Models\User;
10
use Gameap\Repositories\GdaemonTaskRepository;
11
use Illuminate\Contracts\Auth\Factory as AuthFactory;
12
use Illuminate\Http\Response;
13
use Spatie\QueryBuilder\QueryBuilder;
14
15
class GdaemonTasksController extends AuthController
16
{
17
    /**
18
     * The GdaemonTaskRepository instance.
19
     *
20
     * @var \Gameap\Repositories\GdaemonTaskRepository
21
     */
22
    public $repository;
23
24
    /** @var AuthFactory */
25
    protected $authFactory;
26
27
    /**
28
     * GdaemonTasksController constructor.
29
     * @param GdaemonTaskRepository $repository
30
     */
31
    public function __construct(GdaemonTaskRepository $repository, AuthFactory $auth)
32
    {
33
        parent::__construct();
34
35
        $this->repository = $repository;
36
        $this->authFactory = $auth;
37
    }
38
39
    /**
40
     * @param GdaemonTask $gdaemonTask
41
     * @return array
42
     */
43
    public function get(GdaemonTask $gdaemonTask)
44
    {
45
        /** @var User $currentUser */
46
        $currentUser = $this->authFactory->guard()->user();
47
48
        if (!$currentUser->can(PermissionHelper::ADMIN_PERMISSIONS)) {
49
            if (empty($gdaemonTask->server)) {
50
                abort(Response::HTTP_FORBIDDEN);
51
            }
52
53
            $this->authorize(ServerPermissionHelper::CONTROL_ABILITY, $gdaemonTask->server);
54
        }
55
56
        return [
57
            'id'         => $gdaemonTask->id,
58
            'run_aft_id' => $gdaemonTask->id,
59
            'created_at' => $gdaemonTask->created_at->toDateTimeString(),
60
            'updated_at' => $gdaemonTask->updated_at->toDateTimeString(),
61
            'server_id'  => $gdaemonTask->server_id,
62
            'task'       => $gdaemonTask->task,
63
            'status'     => $gdaemonTask->status,
64
        ];
65
    }
66
67
    public function list()
68
    {
69
        return QueryBuilder::for(
70
            GdaemonTask::select('id','created_at', 'updated_at', 'dedicated_server_id', 'server_id', 'task', 'status', 'cmd')
0 ignored issues
show
Bug introduced by
It seems like Gameap\Models\GdaemonTas...task', 'status', 'cmd') can also be of type Illuminate\Database\Query\Builder; however, parameter $subject of Spatie\QueryBuilder\QueryBuilder::for() does only seem to accept Illuminate\Database\Eloq...lations\Relation|string, 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

70
            /** @scrutinizer ignore-type */ GdaemonTask::select('id','created_at', 'updated_at', 'dedicated_server_id', 'server_id', 'task', 'status', 'cmd')
Loading history...
71
        )
72
            ->allowedSorts(['created_at', 'id'])
73
            ->allowedFilters(['status', 'dedicated_server_id', 'server_id', 'task', 'status'])
74
            ->jsonPaginate();
75
    }
76
77
    /**
78
     * @param GdaemonTask $gdaemonTask
79
     * @return array
80
     */
81
    public function output(GdaemonTask $gdaemonTask)
82
    {
83
        return [
84
            'id'                    => $gdaemonTask->id,
85
            'dedicated_server_id'   => $gdaemonTask->dedicated_server_id,
86
            'server_id'             => $gdaemonTask->server_id,
87
            'task'                  => $gdaemonTask->task,
88
            'created_at'            => $gdaemonTask->created_at,
89
            'updated_at'            => $gdaemonTask->updated_at,
90
            'output'                => $gdaemonTask->output,
91
            'status'                => $gdaemonTask->status,
92
        ];
93
    }
94
}
95