Passed
Pull Request — master (#24)
by Damian
01:50
created

RefreshTokenMutationCreator::args()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Firesphere\GraphQLJWT\Mutations;
4
5
use App\Users\GraphQL\Types\TokenStatusEnum;
6
use BadMethodCallException;
7
use Exception;
8
use Firesphere\GraphQLJWT\Helpers\HeaderExtractor;
9
use Firesphere\GraphQLJWT\Helpers\MemberTokenGenerator;
10
use Firesphere\GraphQLJWT\Helpers\RequiresAuthenticator;
11
use Firesphere\GraphQLJWT\Model\JWTRecord;
12
use GraphQL\Type\Definition\ResolveInfo;
13
use GraphQL\Type\Definition\Type;
14
use OutOfBoundsException;
15
use Psr\Container\NotFoundExceptionInterface;
16
use SilverStripe\Control\Controller;
17
use SilverStripe\Core\Extensible;
18
use SilverStripe\GraphQL\MutationCreator;
19
use SilverStripe\GraphQL\OperationResolver;
20
use SilverStripe\ORM\ValidationException;
21
22
class RefreshTokenMutationCreator extends MutationCreator implements OperationResolver
23
{
24
    use RequiresAuthenticator;
25
    use HeaderExtractor;
26
    use MemberTokenGenerator;
27
    use Extensible;
28
29
    public function attributes(): array
30
    {
31
        return [
32
            'name'        => 'refreshToken',
33
            'description' => 'Refreshes a JWT token for a valid user. To be done'
34
        ];
35
    }
36
37
    public function type(): Type
38
    {
39
        return $this->manager->getType('MemberToken');
40
    }
41
42
    /**
43
     * @param mixed $object
44
     * @param array $args
45
     * @param mixed $context
46
     * @param ResolveInfo $info
47
     * @return array
48
     * @throws NotFoundExceptionInterface
49
     * @throws ValidationException
50
     * @throws BadMethodCallException
51
     * @throws OutOfBoundsException
52
     * @throws Exception
53
     */
54
    public function resolve($object, array $args, $context, ResolveInfo $info): array
55
    {
56
        $authenticator = $this->getJWTAuthenticator();
57
        $request = Controller::curr()->getRequest();
58
        $token = $this->getAuthorizationHeader($request);
59
60
        // Check status of existing token
61
        /** @var JWTRecord $record */
62
        list($record, $status) = $authenticator->validateToken($token, $request);
0 ignored issues
show
Bug introduced by
It seems like $token can also be of type null; however, parameter $token of Firesphere\GraphQLJWT\Au...icator::validateToken() 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

62
        list($record, $status) = $authenticator->validateToken(/** @scrutinizer ignore-type */ $token, $request);
Loading history...
63
        $member = null;
64
        switch ($status) {
65
            case TokenStatusEnum::STATUS_OK:
66
            case TokenStatusEnum::STATUS_EXPIRED:
67
                $member = $record->Member();
68
                $renewable = true;
69
                break;
70
            case TokenStatusEnum::STATUS_DEAD:
71
            case TokenStatusEnum::STATUS_INVALID:
72
            default:
73
                $member = null;
74
                $renewable = false;
75
                break;
76
        }
77
78
        // Check if renewable
79
        if (!$renewable) {
80
            return $this->generateResponse($status);
81
        }
82
83
        // Create new token for member
84
        $newToken = $authenticator->generateToken($request, $member);
0 ignored issues
show
Bug introduced by
It seems like $member can also be of type null; however, parameter $member of Firesphere\GraphQLJWT\Au...icator::generateToken() does only seem to accept SilverStripe\Security\Member, 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

84
        $newToken = $authenticator->generateToken($request, /** @scrutinizer ignore-type */ $member);
Loading history...
85
        return $this->generateResponse(TokenStatusEnum::STATUS_OK, $member, $newToken->__toString());
86
    }
87
}
88