MemberTokenGenerator   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 50
rs 10
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getErrorMessage() 0 15 6
A generateResponse() 0 15 5
1
<?php declare(strict_types=1);
2
3
namespace Firesphere\GraphQLJWT\Helpers;
4
5
use Firesphere\GraphQLJWT\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
        $valid = $status === TokenStatusEnum::STATUS_OK;
54
        $response = [
55
            'Valid'   => $valid,
56
            'Member'  => $valid && $member && $member->exists() ? $member : null,
57
            'Token'   => $token,
58
            'Status'  => $status,
59
            'Code'    => $valid ? 200 : 401,
60
            'Message' => $this->getErrorMessage($status),
61
        ];
62
63
        $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

63
        $this->/** @scrutinizer ignore-call */ 
64
               extend('updateMemberToken', $response);
Loading history...
64
        return $response;
65
    }
66
}
67