Issues (27)

src/Token/CacheableTokenStorage.php (2 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Biurad opensource projects.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 *
17
 */
18
19
namespace Biurad\Security\Token;
20
21
use Psr\Cache\CacheItemPoolInterface;
22
use Symfony\Component\HttpFoundation\Session\SessionInterface;
23
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
24
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
25
use Symfony\Contracts\Service\ResetInterface;
26
27
/**
28
 * TokenStorage contains a TokenInterface.
29
 *
30
 * It gives access to the token representing the current user authentication.
31
 *
32
 * @author Divine Niiquaye Ibok <[email protected]>
33
 */
34
class CacheableTokenStorage implements TokenStorageInterface, ResetInterface
35
{
36
    private ?TokenInterface $token = null;
37
    private \Closure $storage;
38
39
    /**
40
     * @param SessionInterface|CacheItemPoolInterface $storage
41
     * @param int|\DateTime|null                      $expiry
42
     */
43
    public function __construct(object $storage, $expiry = 60 * 60 * 24 * 30)
44
    {
45
        $this->storage = function (string $key, TokenInterface $token = null) use ($storage, $expiry): ?TokenInterface {
46
            if (1 === \func_num_args()) {
47
                if ($storage instanceof CacheItemPoolInterface) {
48
                    return $storage->getItem($key)->get();
49
                }
50
51
                if (\is_array($token = $storage->get($key))) {
52
                    [$token, $expiry] = $token;
53
54
                    if (\time() > $expiry) {
55
                        $this->setToken();
56
57
                        return null; // token has expired
58
                    }
59
                }
60
61
                return $token;
62
            }
63
64
            if (null === $token) {
65
                if ($storage instanceof CacheItemPoolInterface) {
66
                    $storage->deleteItem($key);
67
                } else {
68
                    $storage->remove($key);
69
                }
70
            } elseif ($storage instanceof SessionInterface) {
71
                if ($expiry instanceof \DateTimeInterface) {
72
                    $expiry = $expiry->getTimestamp();
73
                }
74
                $storage->set($key, $expiry ? [$token, $expiry] : $token);
75
            } else {
76
                $item = $storage->getItem($key)->set($token);
77
                $storage->save(\is_int($expiry) ? $item->expiresAfter(new \DateInterval('PT'.$expiry.'S')) : $item->expiresAt($expiry));
0 ignored issues
show
Expected at least 1 space before "."; 0 found
Loading history...
Expected at least 1 space after "."; 0 found
Loading history...
78
            }
79
80
            return null;
81
        };
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function getToken(): ?TokenInterface
88
    {
89
        return $this->token ??= ($this->storage)(__CLASS__);
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function setToken(TokenInterface $token = null): void
96
    {
97
        ($this->storage)(__CLASS__, $this->token = $token);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function reset(): void
104
    {
105
        $this->setToken();
106
    }
107
}
108