Completed
Pull Request — develop (#110)
by Daan van
10:17 queued 07:23
created

SessionLifetimeGuard::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
/**
4
 * Copyright 2016 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of 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,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupSelfService\SelfServiceBundle\Security\Authentication\Session;
20
21
use Surfnet\StepupSelfService\SelfServiceBundle\Security\Authentication\AuthenticatedSessionStateHandler;
22
use Surfnet\StepupSelfService\SelfServiceBundle\Value\DateTime;
23
use Surfnet\StepupSelfService\SelfServiceBundle\Value\TimeFrame;
24
25
class SessionLifetimeGuard
26
{
27
    /**
28
     * @var TimeFrame
29
     */
30
    private $relativeTimeoutLimit;
31
    /**
32
     * @var TimeFrame
33
     */
34
    private $absoluteTimeoutLimit;
35
36
    public function __construct(TimeFrame $absoluteTimeoutLimit, TimeFrame $relativeTimeoutLimit)
37
    {
38
        $this->absoluteTimeoutLimit = $absoluteTimeoutLimit;
39
        $this->relativeTimeoutLimit = $relativeTimeoutLimit;
40
    }
41
42
    /**
43
     * @param AuthenticatedSessionStateHandler $sessionStateHandler
44
     * @return bool
45
     */
46
    public function sessionLifetimeWithinLimits(AuthenticatedSessionStateHandler $sessionStateHandler)
47
    {
48
        return $this->sessionLifetimeWithinAbsoluteLimit($sessionStateHandler)
49
                && $this->sessionLifetimeWithinRelativeLimit($sessionStateHandler);
50
    }
51
52
    /**
53
     * @param AuthenticatedSessionStateHandler $sessionStateHandler
54
     * @return bool
55
     */
56 View Code Duplication
    public function sessionLifetimeWithinAbsoluteLimit(AuthenticatedSessionStateHandler $sessionStateHandler)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
    {
58
        if (!$sessionStateHandler->isAuthenticationMomentLogged()) {
59
            return true;
60
        }
61
62
        $authenticationMoment = $sessionStateHandler->getAuthenticationMoment();
63
        $sessionTimeoutMoment = $this->absoluteTimeoutLimit->getEndWhenStartingAt($authenticationMoment);
64
        $now = DateTime::now();
65
66
        if ($now->comesBeforeOrIsEqual($sessionTimeoutMoment)) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return $now->comesBefore...$sessionTimeoutMoment);.
Loading history...
67
            return true;
68
        }
69
70
        return false;
71
    }
72
73
    /**
74
     * @param AuthenticatedSessionStateHandler $sessionStateHandler
75
     * @return bool
76
     */
77 View Code Duplication
    public function sessionLifetimeWithinRelativeLimit(AuthenticatedSessionStateHandler $sessionStateHandler)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
    {
79
        if (!$sessionStateHandler->hasSeenInteraction()) {
80
            return true;
81
        }
82
83
        $lastInteractionMoment = $sessionStateHandler->getLastInteractionMoment();
84
        $sessionTimeoutMoment = $this->relativeTimeoutLimit->getEndWhenStartingAt($lastInteractionMoment);
85
        $now = DateTime::now();
86
87
        if ($now->comesBeforeOrIsEqual($sessionTimeoutMoment)) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return $now->comesBefore...$sessionTimeoutMoment);.
Loading history...
88
            return true;
89
        }
90
91
        return false;
92
    }
93
}
94