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

MemberTokenGenerator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 60
rs 10
c 0
b 0
f 0
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getErrorMessage() 0 15 6
A generateResponse() 0 25 4
1
<?php declare(strict_types=1);
2
3
namespace Firesphere\GraphQLJWT\Helpers;
4
5
use App\Users\GraphQL\Types\TokenStatusEnum;
6
use InvalidArgumentException;
7
use SilverStripe\Core\Extensible;
8
use SilverStripe\Security\Member;
9
10
/**
11
 * Generates / Validates a MemberTokenType for graphql responses
12
 *
13
 * @mixin Extensible
14
 */
15
trait MemberTokenGenerator
16
{
17
    /**
18
     * Humanise error message based on status code
19
     *
20
     * @param string $status
21
     * @return string
22
     * @throws InvalidArgumentException
23
     */
24
    public function getErrorMessage(string $status): string
25
    {
26
        switch ($status) {
27
            case TokenStatusEnum::STATUS_EXPIRED:
28
                return _t('JWT.STATUS_EXPIRED', 'Token is expired, please renew your token with a refreshToken query');
29
            case TokenStatusEnum::STATUS_DEAD:
30
                return _t('JWT.STATUS_DEAD', 'Token is expired, but is too old to renew. Please log in again.');
31
            case TokenStatusEnum::STATUS_INVALID:
32
                return _t('JWT.STATUS_INVALID', 'Invalid token provided');
33
            case TokenStatusEnum::STATUS_BAD_LOGIN:
34
                return _t('JWT.STATUS_BAD_LOGIN', 'Sorry your email and password combination is rejected');
35
            case TokenStatusEnum::STATUS_OK:
36
                return _t('JWT.STATUS_OK', 'Token is ok');
37
            default:
38
                throw new InvalidArgumentException("Invalid status");
39
        }
40
    }
41
42
    /**
43
     * Generate MemberToken response
44
     *
45
     * @param string $status Status code
46
     * @param Member $member
47
     * @param string $token
48
     * @return array Response in format required by MemberToken
49
     */
50
    protected function generateResponse(string $status, Member $member = null, string $token = null): array
51
    {
52
        // Success response
53
        if ($status === TokenStatusEnum::STATUS_OK) {
54
            $response = [
55
                'Valid'   => true,
56
                'Member'  => $member && $member->exists() ? $member : null,
57
                'Token'   => $token,
58
                'Status'  => $status,
59
                'Code'    => 200,
60
                'Message' => $this->getErrorMessage($status),
61
            ];
62
        } else {
63
            $response = [
64
                'Valid'   => false,
65
                'Member'  => null,
66
                'Token'   => $token,
67
                'Status'  => $status,
68
                'Code'    => 401,
69
                'Message' => $this->getErrorMessage($status),
70
            ];
71
        }
72
73
        $this->extend('updateMemberToken', $response);
0 ignored issues
show
Bug introduced by
It seems like extend() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
        $this->/** @scrutinizer ignore-call */ 
74
               extend('updateMemberToken', $response);
Loading history...
74
        return $response;
75
    }
76
}
77