Completed
Pull Request — master (#134)
by Tobias
10:56
created

AccessToken::create()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 3
cts 3
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 1
crap 4
1
<?php
2
3
namespace Happyr\LinkedIn;
4
5
/**
6
 * @author Tobias Nyholm
7
 */
8
class AccessToken
9
{
10
    /**
11
     * @var null|string token
12
     */
13
    private $token;
14
15
    /**
16
     * @var \DateTime expiresAt
17
     */
18
    private $expiresAt;
19
20
    /**
21
     * @param string        $token
22
     * @param \DateTime|int $expiresIn
23
     */
24 6
    public function __construct($token = null, $expiresIn = null)
25
    {
26 6
        $this->token = $token;
27
28 6
        if ($expiresIn !== null) {
29 2
            if ($expiresIn instanceof \DateTime) {
30 1
                $this->expiresAt = $expiresIn;
31 1
            } else {
32 2
                $this->expiresAt = new \DateTime(sprintf('+%dseconds', $expiresIn));
33
            }
34 2
        }
35 6
    }
36
37
    /**
38
     * Restore a stored access token.
39
     *
40 3
     * @param string|AccessToken $token
41
     *
42 3
     * @return AccessToken
43
     */
44
    public static function create($token)
45
    {
46
        if (empty($token)) {
47
            return new self();
48
        }
49
50 2
        if ($token instanceof AccessToken) {
51
            return $token;
52 2
        }
53
54
        $unserialized = @unserialize($token);
55
        if ($unserialized instanceof AccessToken) {
56
            return $unserialized;
57
        }
58
59
        return new self($token);
60 1
    }
61
62 1
    /**
63
     * @return string
64 1
     */
65
    public function __toString()
66
    {
67
        return $this->token ?: '';
68
    }
69
70 2
    /**
71
     * Does a token string exist?
72 2
     *
73
     * @return bool
74
     */
75
    public function hasToken()
76
    {
77
        return !empty($this->token);
78
    }
79
80 1
    /**
81
     * @param \DateTime $expiresAt
82 1
     *
83
     * @return $this
84 1
     */
85
    public function setExpiresAt(\DateTime $expiresAt = null)
86
    {
87
        $this->expiresAt = $expiresAt;
88
89
        return $this;
90 1
    }
91
92 1
    /**
93
     * @return \DateTime
94
     */
95
    public function getExpiresAt()
96
    {
97
        return $this->expiresAt;
98
    }
99
100
    /**
101
     * @param null|string $token
102
     *
103
     * @return $this
104
     */
105
    public function setToken($token)
106
    {
107
        $this->token = $token;
108
109
        return $this;
110
    }
111
112
    /**
113
     * @return null|string
114
     */
115
    public function getToken()
116
    {
117
        return $this->token;
118
    }
119
}
120