Passed
Push — master ( 291050...8bc8e6 )
by Simon
01:52
created

CreateTokenMutationCreator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

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

4 Methods

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