SigningTokens::createSigner()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 18
rs 9.4285
1
<?php
2
3
namespace Framgia\Jwt;
4
5
use Lcobucci\JWT\Signer;
6
use Illuminate\Support\Str;
7
use InvalidArgumentException;
8
9
trait SigningTokens
10
{
11
    protected function createSigner($algorithm = 'sha256', $type = null)
12
    {
13
        if (is_null($type)) {
14
            $type = class_basename($this);
15
        } else {
16
            $type = Str::studly($type);
17
        }
18
19
        $algorithm = Str::studly($algorithm);
20
21
        // Check if provided class exists and extends BaseVerifier
22
        // to avoid possible code injections
23
        if (is_subclass_of($class = Signer::class.'\\'.$type.'\\'.$algorithm, Signer\BaseSigner::class)) {
24
            return new $class;
25
        }
26
27
        throw new InvalidArgumentException('['.$algorithm.'] is not supported in ['.$type.'] verifier.');
28
    }
29
}
30