Completed
Pull Request — 2.x (#80)
by
unknown
02:00
created

ResumeService::timedOut()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 19
ccs 12
cts 12
cp 1
rs 9.4285
cc 3
eloc 11
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\Auth\Service;
10
11
use Aura\Auth\Auth;
12
use Aura\Auth\Adapter\AdapterInterface;
13
use Aura\Auth\Session\SessionInterface;
14
use Aura\Auth\Session\Timer;
15
16
/**
17
 *
18
 * Resume handler.
19
 *
20
 * @package Aura.Auth
21
 *
22
 */
23
class ResumeService
24
{
25
    /**
26
     *
27
     * A credential storage adapter.
28
     *
29
     * @var AdapterInterface
30
     *
31
     */
32
    protected $adapter;
33
34
    /**
35
     *
36
     * A session manager.
37
     *
38
     * @var SessionInterface
39
     *
40
     */
41
    protected $session;
42
43
    /**
44
     *
45
     * A session timer.
46
     *
47
     * @var Timer
48
     *
49
     */
50
    protected $timer;
51
52
    /**
53
     *
54
     * The logout handler to use if the session has timed out.
55
     *
56
     * @var LogoutService
57
     *
58
     */
59
    protected $logout_service;
60
61
    /**
62
     *
63
     * Constructor.
64
     *
65
     * @param AdapterInterface $adapter A credential storage adapter.
66
     *
67
     * @param SessionInterface $session A session manager.
68
     *
69
     * @param Timer $timer A session timer.
70
     *
71
     * @param LogoutService $logout_service The logout handler to use if the
72
     * session has timed out.
73
     *
74
     */
75 7
    public function __construct(
76
        AdapterInterface $adapter,
77
        SessionInterface $session,
78
        Timer $timer,
79
        LogoutService $logout_service
80
    ) {
81 7
        $this->adapter = $adapter;
82 7
        $this->session = $session;
83 7
        $this->timer = $timer;
84 7
        $this->logout_service = $logout_service;
85 7
    }
86
87
    /**
88
     *
89
     * Resumes any previous session, logging out the user as idled or
90
     * expired if needed.
91
     *
92
     * @param Auth $auth An authentication tracker.
93
     *
94
     * @return bool Whether or not a session still exists.
95
     *
96
     */
97 4
    public function resume(Auth $auth)
98
    {
99 4
        $this->session->resume();
100 4
        if (! $this->timedOut($auth)) {
101 2
            $auth->setLastActive(time());
102 2
            $this->adapter->resume($auth);
103 2
        }
104 4
    }
105
106
    /**
107
     *
108
     * Sets the user timeout status, and logs out if expired.
109
     *
110
     * @param Auth $auth An authentication tracker.
111
     *
112
     * @return bool
113
     *
114
     */
115 4
    protected function timedOut(Auth $auth)
116
    {
117 4
        if ($auth->isAnon()) {
118 1
            return false;
119
        }
120
121 3
        $timeout_status = $this->timer->getTimeoutStatus(
122 3
            $auth->getFirstActive(),
123 3
            $auth->getLastActive()
124 3
        );
125
126 3
        if ($timeout_status) {
127 2
            $auth->setStatus($timeout_status);
128 2
            $this->logout_service->logout($auth, $timeout_status);
129 2
            return true;
130
        }
131
132 1
        return false;
133
    }
134
}
135