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

LogoutService::logout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
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\Adapter\AdapterInterface;
12
use Aura\Auth\Session\SessionInterface;
13
use Aura\Auth\Status;
14
use Aura\Auth\Auth;
15
16
/**
17
 *
18
 * Logout handler.
19
 *
20
 * @package Aura.Auth
21
 *
22
 */
23
class LogoutService
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
     * Constructor.
46
     *
47
     * @param AdapterInterface $adapter A credential storage adapter.
48
     *
49
     * @param SessionInterface $session A session manager.
50
     *
51
     */
52 12
    public function __construct(
53
        AdapterInterface $adapter,
54
        SessionInterface $session
55
    ) {
56 12
        $this->adapter = $adapter;
57 12
        $this->session = $session;
58 12
    }
59
60
    /**
61
     *
62
     * Log the user out via the adapter.
63
     *
64
     * @param Auth $auth An authentication tracker.
65
     *
66
     * @param string $status The status after logout.
67
     *
68
     * @return null
69
     *
70
     */
71 3
    public function logout(Auth $auth, $status = Status::ANON)
72
    {
73 3
        $this->adapter->logout($auth, $status);
74 3
        $this->forceLogout($auth, $status);
75 3
    }
76
77
    /**
78
     *
79
     * Forces a successful logout.
80
     *
81
     * @param Auth $auth An authentication tracker.
82
     *
83
     * @param string $status The status after logout.
84
     *
85
     * @return string The new authentication status.
86
     *
87
     */
88 4
    public function forceLogout(Auth $auth, $status = Status::ANON)
89
    {
90 4
        $this->session->regenerateId();
91
92 4
        $auth->set(
93 4
            $status,
94 4
            null,
95 4
            null,
96 4
            null,
97 4
            array()
98 4
        );
99
100 4
        return $status;
101
    }
102
}
103