Passed
Push — master ( 42aa8f...8137e8 )
by Simon
02:14
created

CreateTokenMutationCreator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
dl 0
loc 63
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A type() 0 3 1
A args() 0 5 1
B resolve() 0 32 6
A attributes() 0 5 1
1
<?php
2
3
namespace Firesphere\GraphQLJWT;
4
5
use GraphQL\Type\Definition\ResolveInfo;
0 ignored issues
show
Bug introduced by
The type GraphQL\Type\Definition\ResolveInfo was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use GraphQL\Type\Definition\Type;
7
use SilverStripe\Control\Controller;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\GraphQL\MutationCreator;
10
use SilverStripe\GraphQL\OperationResolver;
11
use SilverStripe\Security\Authenticator;
12
use SilverStripe\Security\IdentityStore;
13
use SilverStripe\Security\Member;
14
use SilverStripe\Security\Security;
15
16
class CreateTokenMutationCreator extends MutationCreator implements OperationResolver
17
{
18
    public function attributes()
19
    {
20
        return [
21
            'name'        => 'createToken',
22
            'description' => 'Creates a JWT token for a valid user'
23
        ];
24
    }
25
26
    public function type()
27
    {
28
        return $this->manager->getType('MemberToken');
29
    }
30
31
    public function args()
32
    {
33
        return [
34
            'Email'    => ['type' => Type::nonNull(Type::string())],
35
            'Password' => ['type' => Type::nonNull(Type::string())]
36
        ];
37
    }
38
39
    /**
40
     * @param mixed $object
41
     * @param array $args
42
     * @param mixed $context
43
     * @param ResolveInfo $info
44
     * @return null|Member|static
45
     * @throws \Psr\Container\NotFoundExceptionInterface
46
     */
47
    public function resolve($object, array $args, $context, ResolveInfo $info)
48
    {
49
        $security = Injector::inst()->get(Security::class);
50
        $authenticators = $security->getApplicableAuthenticators(Authenticator::LOGIN);
51
        $request = Controller::curr()->getRequest();
52
        $member = null;
53
54
        if (count($authenticators)) {
55
            foreach ($authenticators as $authenticator) {
56
                $member = $authenticator->authenticate($args, $request, $result);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $result seems to be never defined.
Loading history...
57
                if ($result->isValid()) {
58
                    break;
59
                }
60
            }
61
        }
62
        $authenticator = Injector::inst()->get(JWTAuthenticator::class);
63
64
        if ($member instanceof Member) {
65
            $member->Token = $authenticator->generateToken($member);
66
        } elseif (JWTAuthenticator::config()->get('anonymous_allowed')) {
67
            $member = Member::create(['ID' => 0, 'FirstName' => 'Anonymous']);
68
            // Create an anonymous token
69
            $member->Token = $authenticator->generateToken($member);
70
        } else {
71
            Security::setCurrentUser(null);
72
            Injector::inst()->get(IdentityStore::class)->logOut();
73
74
            // Return a token-less member
75
            $member = Member::create();
76
        }
77
78
        return $member;
79
    }
80
}
81