Completed
Push — feature/controller ( 4da62c...5a6415 )
by René
04:10
created

Cookie::__construct()   B

Complexity

Conditions 6
Paths 14

Size

Total Lines 28
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6.0163
Metric Value
dl 0
loc 28
ccs 12
cts 13
cp 0.9231
rs 8.439
cc 6
eloc 13
nc 14
nop 1
crap 6.0163
1
<?php
2
declare(strict_types = 1);
3
4
namespace Zortje\MVC\Storage\Cookie;
5
6
use Lcobucci\JWT\Builder;
7
use Lcobucci\JWT\Claim;
8
use Lcobucci\JWT\Parser;
9
use Lcobucci\JWT\Signer\Hmac\Sha256;
10
use Lcobucci\JWT\ValidationData;
11
12
/**
13
 * Class Cookie
14
 *
15
 * @package Zortje\MVC\Storage
16
 */
17
class Cookie
18
{
19
20
    const ISSUER = 'zortje/mvc';
21
22
    /**
23
     * @var string[] Internal cookie values
24
     */
25
    protected $values = [];
26
27
    /**
28
     * Cookie constructor.
29
     *
30
     * @param string $token JWT token string
31
     */
32 2
    public function __construct(string $token = '')
33
    {
34
        // @todo SECRET key should be in configuration
35 2
        $secret = 'super-secret-key';
36
37
        try {
38 2
            $token = (new Parser())->parse($token);
39
40 2
            $data = new ValidationData(); // It will use the current time to validate (iat, nbf and exp)
41 2
            $data->setIssuer(self::ISSUER);
42
43 2
            if ($token->validate($data) && $token->verify(new Sha256(), $secret)) {
44
                /**
45
                 * @var Claim $claim
46
                 */
47 1
                $ignored = array_fill_keys(['iss', 'exp'], true);
48
49 1
                foreach ($token->getClaims() as $claim) {
50 1
                    if (isset($ignored[$claim->getName()])) {
51 1
                        continue;
52
                    }
53
54 2
                    $this->values[$claim->getName()] = $claim->getValue();
55
                }
56
            }
57
        } catch (\InvalidArgumentException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
58
        }
59 2
    }
60
61
    /**
62
     * Set value in cookie
63
     *
64
     * @param string $key   Cookie key
65
     * @param string $value Cookie value
66
     */
67 2
    public function set(string $key, string $value)
68
    {
69 2
        $this->values[$key] = $value;
70 2
    }
71
72
    /**
73
     * Get value from cookie
74
     *
75
     * @param string $key Cookie key
76
     *
77
     * @return string Cookie value
78
     */
79 1
    public function get(string $key): string
80
    {
81 1
        return $this->values[$key];
82
    }
83
84
    public function getTokenString(): string
85
    {
86
        // @todo SECRET key should be in configuration
87
        $secret = 'super-secret-key';
88
89
        // @todo should cookie TTL be set in a configuration?
90
        $cookieTTL = '+1 hour';
91
92
        /**
93
         * Build Token
94
         */
95
        $builder = (new Builder());
96
        $builder->setIssuer(self::ISSUER);
97
        $builder->setExpiration((new \DateTime($cookieTTL))->getTimestamp());
98
99
        foreach ($this->values as $key => $value) {
100
            $builder->set($key, $value);
101
        }
102
103
        /**
104
         * Sign and generate new token
105
         */
106
        $builder->sign(new Sha256(), $secret);
107
108
        $token = $builder->getToken();
109
110
        return (string) $token;
111
    }
112
}
113