NamshiAdapter::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4286
cc 2
eloc 3
nc 2
nop 3
crap 2
1
<?php
2
3
namespace Tymon\JWTAuth\Providers\JWT;
4
5
use Exception;
6
use Namshi\JOSE\JWS;
7
use Tymon\JWTAuth\Exceptions\JWTException;
8
use Tymon\JWTAuth\Exceptions\TokenInvalidException;
9
10
class NamshiAdapter extends JWTProvider implements JWTInterface
11
{
12
    /**
13
     * @var \Namshi\JOSE\JWS
14
     */
15
    protected $jws;
16
17
    /**
18
     * @param string  $secret
19
     * @param string  $algo
20
     * @param null    $driver
21
     */
22 9
    public function __construct($secret, $algo, $driver = null)
23
    {
24 9
        parent::__construct($secret, $algo);
25
26 9
        $this->jws = $driver ?: new JWS(['typ' => 'JWT', 'alg' => $algo]);
27 9
    }
28
29
    /**
30
     * Create a JSON Web Token
31
     *
32
     * @return string
33
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
34
     */
35 6
    public function encode(array $payload)
36
    {
37
        try {
38 6
            $this->jws->setPayload($payload)->sign($this->secret);
0 ignored issues
show
Documentation introduced by
$this->secret is of type string, but the function expects a resource.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
39
40 3
            return $this->jws->getTokenString();
41 3
        } catch (Exception $e) {
42 3
            throw new JWTException('Could not create token: ' . $e->getMessage());
43
        }
44
    }
45
46
    /**
47
     * Decode a JSON Web Token
48
     *
49
     * @param  string  $token
50
     * @return array
51
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
52
     */
53 3
    public function decode($token)
54
    {
55
        try {
56 3
            $jws = JWS::load($token);
57 3
        } catch (Exception $e) {
58 3
            throw new TokenInvalidException('Could not decode token: ' . $e->getMessage());
59
        }
60
61
        if (! $jws->verify($this->secret, $this->algo)) {
62
            throw new TokenInvalidException('Token Signature could not be verified.');
63
        }
64
65
        return $jws->getPayload();
66
    }
67
}
68