AccessTokenFileStorage   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 117
Duplicated Lines 11.97 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 3
dl 14
loc 117
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B hasToken() 0 33 6
A getToken() 0 8 3
A setToken() 0 6 1
A abandonToken() 0 6 1
A isTokenExpired() 14 14 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Fousky\Component\iDoklad\Storage;
4
5
use Fousky\Component\iDoklad\Exception\TokenNotFoundException;
6
use Fousky\Component\iDoklad\Model\Auth\AccessToken;
7
use Symfony\Component\Filesystem\Filesystem;
8
9
/**
10
 * @author Lukáš Brzák <[email protected]>
11
 */
12
class AccessTokenFileStorage implements AccessTokenStorageInterface
13
{
14
    /** @var string $file */
15
    protected $file;
16
17
    /** @var AccessToken|null $token */
18
    protected $token;
19
20
    /**
21
     * @param string $file
22
     */
23
    public function __construct(string $file)
24
    {
25
        $this->file = $file;
26
    }
27
28
    /**
29
     * Has storage token?
30
     *
31
     * @return bool
32
     */
33
    public function hasToken(): bool
34
    {
35
        if ($this->token instanceof AccessToken) {
36
            return true;
37
        }
38
39
        $this->token = null;
40
41
        if (!file_exists($this->file) || !is_readable($this->file)) {
42
            return false;
43
        }
44
45
        try {
46
            $token = unserialize(
47
                file_get_contents($this->file),
48
                [
49
                    'allowed_classes' => [
50
                        AccessToken::class,
51
                    ],
52
                ]
53
            );
54
55
            if ($token instanceof AccessToken) {
56
                $this->token = $token;
57
58
                return true;
59
            }
60
        } catch (\Throwable $e) {
61
            return false;
62
        }
63
64
        return false;
65
    }
66
67
    /**
68
     * Get AccessToken from Storage or throw TokenNotFoundException if not found.
69
     *
70
     * @throws TokenNotFoundException
71
     *
72
     * @return AccessToken
73
     */
74
    public function getToken(): AccessToken
75
    {
76
        if (null === $this->token || !$this->hasToken()) {
77
            throw new TokenNotFoundException();
78
        }
79
80
        return $this->token;
81
    }
82
83
    /**
84
     * Set AccessToken to the Storage object.
85
     *
86
     * @param AccessToken $token
87
     *
88
     * @throws \Symfony\Component\Filesystem\Exception\IOException
89
     */
90
    public function setToken(AccessToken $token)
91
    {
92
        (new Filesystem())->dumpFile($this->file, serialize($token));
93
94
        $this->token = $token;
95
    }
96
97
    /**
98
     * Abandon AccessToken.
99
     *
100
     * @throws \Symfony\Component\Filesystem\Exception\IOException
101
     */
102
    public function abandonToken()
103
    {
104
        (new Filesystem())->remove($this->file);
105
106
        $this->token = null;
107
    }
108
109
    /**
110
     * Is AccessToken expired?
111
     *
112
     * @return bool
113
     */
114 View Code Duplication
    public function isTokenExpired(): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
    {
116
        try {
117
            $token = $this->getToken();
118
119
            if (!$token instanceof AccessToken) {
120
                return true;
121
            }
122
123
            return $token->isExpired();
124
        } catch (\Throwable $e) {
125
            return true;
126
        }
127
    }
128
}
129