ValidateTokenQueryCreator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 47
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A type() 0 3 1
A args() 0 3 1
A attributes() 0 5 1
A resolve() 0 11 2
1
<?php declare(strict_types=1);
2
3
namespace Firesphere\GraphQLJWT\Queries;
4
5
use BadMethodCallException;
6
use Exception;
7
use Firesphere\GraphQLJWT\Authentication\JWTAuthenticator;
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 Firesphere\GraphQLJWT\Types\TokenStatusEnum;
13
use GraphQL\Type\Definition\ResolveInfo;
14
use GraphQL\Type\Definition\Type;
15
use OutOfBoundsException;
16
use Psr\Container\NotFoundExceptionInterface;
17
use SilverStripe\Control\Controller;
18
use SilverStripe\Core\Extensible;
19
use SilverStripe\GraphQL\OperationResolver;
20
use SilverStripe\GraphQL\QueryCreator;
21
22
class ValidateTokenQueryCreator extends QueryCreator 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'        => 'validateToken',
33
            'description' => 'Validates a given token from the Bearer header'
34
        ];
35
    }
36
37
    public function args(): array
38
    {
39
        return [];
40
    }
41
42
    public function type(): Type
43
    {
44
        return $this->manager->getType('MemberToken');
45
    }
46
47
    /**
48
     * @param mixed $object
49
     * @param array $args
50
     * @param mixed $context
51
     * @param ResolveInfo $info
52
     * @return array
53
     * @throws NotFoundExceptionInterface
54
     * @throws OutOfBoundsException
55
     * @throws BadMethodCallException
56
     * @throws Exception
57
     */
58
    public function resolve($object, array $args, $context, ResolveInfo $info): array
59
    {
60
        /** @var JWTAuthenticator $authenticator */
61
        $authenticator = $this->getJWTAuthenticator();
62
        $request = Controller::curr()->getRequest();
63
        $token = $this->getAuthorizationHeader($request);
64
65
        /** @var JWTRecord $record */
66
        list($record, $status) = $authenticator->validateToken($token, $request);
67
        $member = $status === TokenStatusEnum::STATUS_OK ? $record->Member() : null;
68
        return $this->generateResponse($status, $member, $token);
69
    }
70
}
71