ServersTasksController::commandAuthorize()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6.6829

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 14
c 1
b 1
f 0
dl 0
loc 19
ccs 11
cts 15
cp 0.7332
rs 9.2222
cc 6
nc 6
nop 2
crap 6.6829
1
<?php
2
3
namespace Gameap\Http\Controllers\API;
4
5
use Carbon\Carbon;
6
use Gameap\Exceptions\Repositories\RepositoryValidationException;
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\Http\Requests\API\ServerTaskCreateRequest;
9
use Gameap\Http\Requests\API\ServerTaskUpdateRequest;
10
use Gameap\Models\Server;
11
use Gameap\Models\ServerTask;
12
use Gameap\Repositories\ServersTasksRepository;
13
use Illuminate\Http\JsonResponse;
14
use Illuminate\Http\Response;
15
16
class ServersTasksController extends AuthController
17
{
18
    /** @var ServersTasksRepository */
19
    protected $repository;
20
21 33
    public function __construct(ServersTasksRepository $serversTasksRepository)
22
    {
23 33
        parent::__construct();
24 33
25 33
        $this->repository = $serversTasksRepository;
26
    }
27
28
    /**
29
     * @param Server $server
30
     * @return array
31
     * @throws \Illuminate\Auth\Access\AuthorizationException
32 12
     */
33
    public function getList(Server $server)
34 12
    {
35
        $this->authorize('server-tasks', $server);
36 9
37
        $tasks = [];
38 9
39 3
        foreach ($this->repository->getTasks($server->id) as $task) {
40 3
            $tasks[] = $task;
41
        }
42
43 9
        return $tasks;
44
    }
45
46
    /**
47
     * @param ServerTaskCreateRequest $request
48
     * @return JsonResponse
49
     * @throws RepositoryValidationException|\Illuminate\Auth\Access\AuthorizationException
50
     */
51 24
    public function store(ServerTaskCreateRequest $request)
52
    {
53 24
        $fields = $request->all();
54
55 24
        $server = Server::findOrFail($fields['server_id']);
56 24
        $this->authorize('server-tasks', $server);
57
58 21
        $this->commandAuthorize($fields['command'], $server);
59
60 9
        $fields['execute_date'] = $this->convertDateToUTC($fields['execute_date']);
61
62 9
        $serverTaskId = $this->repository->store($fields);
63
64 9
        return response()->json([
65 9
            'message'      => 'success',
66 9
            'serverTaskId' => $serverTaskId,
67 9
        ], Response::HTTP_CREATED);
68
    }
69
70
    /**
71
     * @param ServerTaskUpdateRequest $request
72
     * @param Server $server
73
     * @param ServerTask $serverTask
74
     * @return JsonResponse
75
     * @throws RepositoryValidationException|\Illuminate\Auth\Access\AuthorizationException
76
     * @noinspection PhpUnusedParameterInspection
77
     */
78 24
    public function update(ServerTaskUpdateRequest $request, Server $server, ServerTask $serverTask)
79
    {
80 24
        $fields = $request->all();
81
82 24
        $this->authorize('server-tasks', $server);
83 21
        $this->commandAuthorize($fields['command'], $server);
84
85 9
        $fields['execute_date'] = $this->convertDateToUTC($fields['execute_date']);
86
87 9
        $this->repository->update($serverTask->id, $fields);
88
89 9
        return response()->json(['message' => 'success'], Response::HTTP_OK);
90
    }
91
92
    /**
93
     * @param Server $server
94
     * @param ServerTask $serverTask
95
     * @return JsonResponse
96
     * @throws \Exception
97
     * @noinspection PhpUnusedParameterInspection
98
     */
99 12
    public function destroy(Server $server, ServerTask $serverTask)
100
    {
101 12
        $this->authorize('server-tasks', $server);
102
103 9
        $serverTask->delete();
104 9
        return response()->json(['message' => 'success'], Response::HTTP_OK);
105
    }
106
107
    /**
108
     * @param string $command
109
     * @param Server $server
110
     * @throws \Illuminate\Auth\Access\AuthorizationException
111
     */
112 24
    private function commandAuthorize(string $command, Server $server): void
113
    {
114 24
        switch ($command) {
115 24
            case 'start':
116 15
                $this->authorize('server-start', $server);
117 12
                break;
118
119 9
            case 'stop':
120 3
                $this->authorize('server-stop', $server);
121
                break;
122
123 6
            case 'restart':
124 3
                $this->authorize('server-restart', $server);
125
                break;
126
127 3
            case 'update':
128
            case 'reinstall':
129 3
                $this->authorize('server-update', $server);
130
                break;
131
        }
132 12
    }
133
134 12
    private function convertDateToUTC(string $date): string
135
    {
136 12
        $convertedDate = Carbon::createFromFormat(Carbon::DEFAULT_TO_STRING_FORMAT, $date, config('timezone'));
137 12
        $convertedDate->setTimezone('UTC');
138
139 12
        return $convertedDate->toDateTimeString();
140
    }
141
142 3
    private function convertDateToLocal(string $date): string
0 ignored issues
show
Unused Code introduced by
The method convertDateToLocal() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
143
    {
144 3
        $convertedDate = Carbon::createFromFormat(Carbon::DEFAULT_TO_STRING_FORMAT, $date, 'UTC');
145 3
        $convertedDate->setTimezone(config('timezone'));
146
147 3
        return $convertedDate->toDateTimeString();
148
    }
149
}
150