TimeoutListener::notify()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 19
c 0
b 0
f 0
ccs 14
cts 14
cp 1
rs 9.2
cc 4
eloc 13
nc 4
nop 1
crap 4
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Caridea
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
 * use this file except in compliance with the License. You may obtain a copy of
8
 * the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 * License for the specific language governing permissions and limitations under
16
 * the License.
17
 *
18
 * @copyright 2015-2018 LibreWorks contributors
19
 * @license   Apache-2.0
20
 */
21
namespace Caridea\Auth;
22
23
/**
24
 * A listener which will log out a session if it's idle or has been active for too long.
25
 *
26
 * @copyright 2015-2018 LibreWorks contributors
27
 * @license   Apache-2.0
28
 */
29
class TimeoutListener implements \Caridea\Event\Listener
30
{
31
    use \Psr\Log\LoggerAwareTrait;
32
33
    /**
34
     * @var int Session timeout length in seconds
35
     */
36
    protected $timeout;
37
    /**
38
     * @var int Session expiration length in seconds
39
     */
40
    protected $expire;
41
42
    /**
43
     * Creates a new timeout listener.
44
     *
45
     * @param int $timeout The number of seconds until a session should be
46
     *      considered idle. If omitted, the default is 20 minutes.
47
     * @param int $expire The number of seconds until a session should be
48
     *      considered expired. If omitted, the default is 24 hours.
49
     */
50 3
    public function __construct(int $timeout = 1200, int $expire = 86400)
51
    {
52 3
        $this->timeout = (int)$timeout;
53 3
        $this->expire = (int)$expire;
54 3
        $this->logger = new \Psr\Log\NullLogger();
55 3
    }
56
57
    /**
58
     * {@inheritDoc}
59
     */
60 3
    public function notify(\Caridea\Event\Event $event): void
61
    {
62 3
        if ($event instanceof Event\Resume) {
63 3
            $now = microtime(true);
64 3
            if (($this->timeout + $event->getLastActive()) < $now) {
65 1
                $this->logger->info(
66 1
                    "Authentication for {user} has timed out",
67 1
                    ['user' => $event->getPrincipal()]
68
                );
69 1
                $event->getSource()->logout();
70 3
            } elseif (($this->expire + $event->getFirstActive()) < $now) {
71 1
                $this->logger->info(
72 1
                    "Authentication for {user} has expired",
73 1
                    ['user' => $event->getPrincipal()]
74
                );
75 1
                $event->getSource()->logout();
76
            }
77
        }
78 3
    }
79
}
80