TokenController::add()   A
last analyzed

Complexity

Conditions 3
Paths 6

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 6
nop 1
dl 0
loc 15
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Http\Requests\TokenAddRequest;
6
use App\Http\Requests\TokenRequest;
7
use App\Http\Requests\TokenSetCreditRequest;
8
use App\Siweocs\Models\ErrorResponse;
9
use App\Siweocs\Models\SiwecosBaseReponse;
10
use App\Siweocs\Models\TokenAddResponse;
11
use App\Siweocs\Models\TokenStatusResponse;
12
use App\Token;
13
use Illuminate\Database\QueryException;
14
15
class TokenController extends Controller
16
{
17
    public function add(TokenAddRequest $request)
18
    {
19
        // Create new token instance
20
        $newToken = new Token(['credits' => $request->json('credits')]);
21
        if (array_key_exists('aclLevel', $request->json())) {
0 ignored issues
show
Bug introduced by
$request->json() of type null|Symfony\Component\HttpFoundation\ParameterBag is incompatible with the type array expected by parameter $search of array_key_exists(). ( Ignorable by Annotation )

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

21
        if (array_key_exists('aclLevel', /** @scrutinizer ignore-type */ $request->json())) {
Loading history...
22
            $newToken->setAclLevel($request->json('aclLevel'));
0 ignored issues
show
Bug introduced by
It seems like $request->json('aclLevel') can also be of type Symfony\Component\HttpFoundation\ParameterBag; however, parameter $aclLevel of App\Token::setAclLevel() 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

22
            $newToken->setAclLevel(/** @scrutinizer ignore-type */ $request->json('aclLevel'));
Loading history...
23
        }
24
        // Try insertion to database
25
        try {
26
            $newToken->save();
27
28
            return response()->json(new TokenAddResponse($newToken->token));
29
        } catch (QueryException $queryException) {
30
            // Query has failed
31
            return response($queryException->getMessage(), 500);
32
        }
33
    }
34
35
    public function revoke(TokenRequest $request)
36
    {
37
        // Get requested Token
38
        $requestedToken = Token::getTokenByString($request->json('token'));
0 ignored issues
show
Bug introduced by
It seems like $request->json('token') can also be of type Symfony\Component\HttpFoundation\ParameterBag; however, parameter $token of App\Token::getTokenByString() does only seem to accept 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

38
        $requestedToken = Token::getTokenByString(/** @scrutinizer ignore-type */ $request->json('token'));
Loading history...
39
40
        if ($requestedToken instanceof Token) {
41
            try {
42
                $requestedToken->delete();
43
44
                return response()->json(new SiwecosBaseReponse('Token revoked', false));
45
            } catch (QueryException $queryException) {
46
                return response()->json(new ErrorResponse($queryException->getMessage()));
47
            } catch (\Exception $e) {
48
                return response($e->getMessage(), 500);
49
            }
50
        }
51
52
        return response('Token not found', 404);
53
    }
54
55
    public function status(TokenRequest $request)
56
    {
57
        $requestedToken = Token::getTokenByString($request->json('token'));
0 ignored issues
show
Bug introduced by
It seems like $request->json('token') can also be of type Symfony\Component\HttpFoundation\ParameterBag; however, parameter $token of App\Token::getTokenByString() does only seem to accept 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

57
        $requestedToken = Token::getTokenByString(/** @scrutinizer ignore-type */ $request->json('token'));
Loading history...
58
        if ($requestedToken instanceof Token) {
59
            return response()->json(new TokenStatusResponse($requestedToken));
60
        } else {
61
            return response('Token not found', 404);
62
        }
63
    }
64
65
    public function setCredits(TokenSetCreditRequest $request)
66
    {
67
        $requestedToken = Token::getTokenByString($request->json('token'));
0 ignored issues
show
Bug introduced by
It seems like $request->json('token') can also be of type Symfony\Component\HttpFoundation\ParameterBag; however, parameter $token of App\Token::getTokenByString() does only seem to accept 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

67
        $requestedToken = Token::getTokenByString(/** @scrutinizer ignore-type */ $request->json('token'));
Loading history...
68
        $credits = $request->json('credits');
69
        if ($requestedToken instanceof Token) {
70
            if ($requestedToken->setTokenCredits($credits)) {
0 ignored issues
show
Bug introduced by
It seems like $credits can also be of type Symfony\Component\HttpFoundation\ParameterBag; however, parameter $credits of App\Token::setTokenCredits() 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

70
            if ($requestedToken->setTokenCredits(/** @scrutinizer ignore-type */ $credits)) {
Loading history...
71
                return response()->json(new SiwecosBaseReponse('Token credits changed', false));
72
            }
73
74
            return response('Token credits NOT changed', 500);
75
        }
76
77
        return response('Token not found', 404);
78
    }
79
}
80