Completed
Pull Request — master (#134)
by Tobias
06:30
created

AccessToken::create()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

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