Completed
Pull Request — master (#20)
by Harry
11:39 queued 03:51
created

AccessToken::setExpires()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of graze/gigya-client
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/gigya-client/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/gigya-client
12
 */
13
14
namespace Graze\Gigya\Auth\OAuth2;
15
16
use DateTime;
17
use DateTimeInterface;
18
19
class AccessToken
20
{
21
    /** @var string */
22
    private $token;
23
    /** @var DateTimeInterface|null */
24
    private $expires;
25
26
    /**
27
     * AccessToken constructor.
28
     *
29
     * @param string                 $token
30
     * @param DateTimeInterface|null $expires
31
     */
32 11
    public function __construct($token, DateTimeInterface $expires = null)
33
    {
34 11
        $this->token = $token;
35 11
        $this->expires = $expires;
36 11
    }
37
38
    /**
39
     * @return string
40
     */
41 8
    public function getToken()
42
    {
43 8
        return $this->token;
44
    }
45
46
    /**
47
     * @param string $token
48
     *
49
     * @return AccessToken
50
     */
51 1
    public function setToken($token)
52
    {
53 1
        $this->token = $token;
54 1
        return $this;
55
    }
56
57
    /**
58
     * @return DateTimeInterface|null
59
     */
60 7
    public function getExpires()
61
    {
62 7
        return $this->expires;
63
    }
64
65
    /**
66
     * Check if a token is expired
67
     *
68
     * @return bool
69
     */
70 7
    public function isExpired()
71
    {
72 7
        if (!is_null($this->expires)) {
73 5
            $diff = $this->expires->format('U') - (new DateTime())->format('U');
74 5
            return $diff <= 0;
75
        }
76 3
        return false;
77
    }
78
79
    /**
80
     * @param DateTimeInterface|null $expires
81
     *
82
     * @return AccessToken
83
     */
84 1
    public function setExpires($expires)
85
    {
86 1
        $this->expires = $expires;
87 1
        return $this;
88
    }
89
}
90