TokensController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 26
c 2
b 0
f 0
dl 0
loc 63
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A list() 0 12 1
A abilities() 0 7 1
A destroy() 0 8 1
A __construct() 0 9 1
A store() 0 8 1
1
<?php
2
3
namespace Gameap\Http\Controllers\API;
4
5
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...
6
use Gameap\Http\Requests\API\GeneratePersonalAccessTokenRequest;
7
use Gameap\Models\User;
8
use Gameap\Services\PersonalAccessTokenService;
9
use Illuminate\Contracts\Auth\Factory as AuthFactory;
10
use Illuminate\Support\Facades\Auth;
11
12
class TokensController extends AuthController
13
{
14
    /** @var PersonalAccessTokenService */
15
    private $personalAccessTokenService;
16
17
    /** @var AuthFactory */
18
    protected $authFactory;
19
20
    public function __construct(
21
        PersonalAccessTokenService $personalAccessTokenService,
22
        AuthFactory $auth
23
    )
24
    {
25
        parent::__construct();
26
27
        $this->personalAccessTokenService = $personalAccessTokenService;
28
        $this->authFactory = $auth;
29
    }
30
31
    public function list()
32
    {
33
        /** @var User $currentUser */
34
        $currentUser = $this->authFactory->guard()->user();
35
36
        return response()->json(collect($currentUser->tokens)->map(function ($token) {
37
            return [
38
                'id' => $token->id,
39
                'name' => $token->name,
40
                'abilities' => $token->abilities,
41
                'last_used_at' => $token->last_used_at,
42
                'created_at' => $token->created_at,
43
            ];
44
        }));
45
    }
46
47
    public function abilities()
48
    {
49
        /** @var User $currentUser */
50
        $currentUser = $this->authFactory->guard()->user();
51
        $tokenAbilities = $this->personalAccessTokenService->getGrouppedAbilitiesDescriptions($currentUser);
52
53
        return response()->json($tokenAbilities);
54
    }
55
56
    public function store(GeneratePersonalAccessTokenRequest $request)
57
    {
58
        /** @var User $currentUser */
59
        $currentUser = $this->authFactory->guard()->user();
60
        $token = $currentUser->createToken($request->token_name, $request->abilities);
61
62
        return response()->json([
63
            'token' => $token->plainTextToken,
64
        ]);
65
    }
66
67
    public function destroy($tokenId)
68
    {
69
        /** @var User $currentUser */
70
        $currentUser = $this->authFactory->guard()->user();
71
        $currentUser->tokens()->where('id', $tokenId)->delete();
72
73
        return response()->json([
74
            'message' => 'Token deleted',
75
        ]);
76
    }
77
}