Completed
Push — master ( 1436fc...7e0f0d )
by Simon
01:30
created

CreateMemberMutationCreator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 22.73 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 6
dl 10
loc 44
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A attributes() 0 7 1
A type() 0 4 1
A args() 10 10 1
A resolve() 0 17 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace MySite\GraphQL;
4
5
use Firesphere\GraphQLJWT\JWTAuthenticator;
6
use GraphQL\Type\Definition\ResolveInfo;
7
use GraphQL\Type\Definition\Type;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\GraphQL\MutationCreator;
10
use SilverStripe\GraphQL\OperationResolver;
11
use SilverStripe\Security\Member;
12
13
class CreateMemberMutationCreator extends MutationCreator implements OperationResolver
14
{
15
    public function attributes()
16
    {
17
        return [
18
            'name'        => 'createMember',
19
            'description' => 'Creates a member without permissions or group assignments'
20
        ];
21
    }
22
23
    public function type()
24
    {
25
        return $this->manager->getType('member');
26
    }
27
28 View Code Duplication
    public function args()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
    {
30
        return [
31
            'FirstName' => ['type' => Type::string()],
32
            'Surname'   => ['type' => Type::string()],
33
            'Email'     => ['type' => Type::nonNull(Type::string())],
34
            'Password'  => ['type' => Type::nonNull(Type::string())],
35
            'Token'     => ['type' => Type::string()]
36
        ];
37
    }
38
39
    public function resolve($object, array $args, $context, ResolveInfo $info)
40
    {
41
        if (!Member::get()->filter(['Email' => $args['Email']])->count()) {
42
            /** @var Member $member */
43
            $member = Member::create($args);
44
            $id = $member->write();
45
            $member->ID = $id;
46
            $token = Injector::inst()->get(JWTAuthenticator::class)->generateToken($member);
47
            $member->Token = $token;
48
            $member->addToGroupByCode('administrators');
49
        } else {
50
            // Return an empty member. This makes it easier to capture an error
51
            $member = Member::create();
52
        }
53
54
        return $member;
55
    }
56
}
57