Completed
Push — master ( 592d95...c00565 )
by Abdelrahman
18:11 queued 10s
created

ApiTokenCookieFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Oauth\Factories;
6
7
use Carbon\Carbon;
8
use Firebase\JWT\JWT;
9
use Symfony\Component\HttpFoundation\Cookie;
10
use Illuminate\Contracts\Encryption\Encrypter;
11
use Illuminate\Contracts\Config\Repository as Config;
12
13
class ApiTokenCookieFactory
14
{
15
    /**
16
     * The configuration repository implementation.
17
     *
18
     * @var \Illuminate\Contracts\Config\Repository
19
     */
20
    protected $config;
21
22
    /**
23
     * The encrypter implementation.
24
     *
25
     * @var \Illuminate\Contracts\Encryption\Encrypter
26
     */
27
    protected $encrypter;
28
29
    /**
30
     * Create an API token cookie factory instance.
31
     *
32
     * @param \Illuminate\Contracts\Config\Repository    $config
33
     * @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
34
     *
35
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
36
     */
37
    public function __construct(Config $config, Encrypter $encrypter)
38
    {
39
        $this->config = $config;
40
        $this->encrypter = $encrypter;
41
    }
42
43
    /**
44
     * Create a new API token cookie.
45
     *
46
     * @param mixed  $userId
47
     * @param string $csrfToken
48
     *
49
     * @return \Symfony\Component\HttpFoundation\Cookie
50
     */
51
    public function make($userId, $csrfToken)
52
    {
53
        $config = $this->config->get('session');
54
55
        $expiration = Carbon::now()->addMinutes($config['lifetime']);
56
57
        return new Cookie(
58
            config('rinvex.oauth.cookie'),
59
            $this->createToken($userId, $csrfToken, $expiration),
60
            $expiration,
0 ignored issues
show
Documentation introduced by
$expiration is of type object<Carbon\Carbon>, but the function expects a integer.

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...
61
            $config['path'],
62
            $config['domain'],
63
            $config['secure'],
64
            true,
65
            false,
66
            $config['same_site'] ?? null
67
        );
68
    }
69
70
    /**
71
     * Create a new JWT token for the given user ID and CSRF token.
72
     *
73
     * @param mixed          $userId
74
     * @param string         $csrfToken
75
     * @param \Carbon\Carbon $expiration
76
     *
77
     * @return string
78
     */
79
    protected function createToken($userId, $csrfToken, Carbon $expiration)
80
    {
81
        return JWT::encode([
82
            'sub' => $userId,
83
            'csrf' => $csrfToken,
84
            'expiry' => $expiration->getTimestamp(),
85
        ], $this->encrypter->getKey());
86
    }
87
}
88