Passed
Pull Request — master (#3)
by Tim
02:10
created

ExpiryCheck::about2expire()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 15
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 27
rs 9.7666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\expirycheck\Controller;
6
7
use SimpleSAML\Auth;
8
use SimpleSAML\Configuration;
9
use SimpleSAML\Error;
10
use SimpleSAML\HTTP\RunnableResponse;
11
use SimpleSAML\Logger;
12
use SimpleSAML\Module;
13
use SimpleSAML\Session;
14
use SimpleSAML\XHTML\Template;
15
use Symfony\Component\HttpFoundation\Request;
16
17
use function htmlspecialchars;
18
19
/**
20
 * Controller class for the expirycheck module.
21
 *
22
 * This class serves the different views available in the module.
23
 *
24
 * @package simplesamlphp/simplesamlphp
25
 */
26
class ExpiryCheck
27
{
28
    /** @var \SimpleSAML\Configuration */
29
    protected Configuration $config;
30
31
    /** @var \SimpleSAML\Session */
32
    protected Session $session;
33
34
    /**
35
     * @var \SimpleSAML\Auth\State|string
36
     * @psalm-var \SimpleSAML\Auth\State|class-string
37
     */
38
    protected $authState = Auth\State::class;
39
40
41
    /**
42
     * Controller constructor.
43
     *
44
     * It initializes the global configuration and session for the controllers implemented here.
45
     *
46
     * @param \SimpleSAML\Configuration $config The configuration to use by the controllers.
47
     * @param \SimpleSAML\Session $session The session to use by the controllers.
48
     *
49
     * @throws \Exception
50
     */
51
    public function __construct(
52
        Configuration $config,
53
        Session $session
54
    ) {
55
        $this->config = $config;
56
        $this->session = $session;
57
    }
58
59
60
    /**
61
     * Inject the \SimpleSAML\Auth\State dependency.
62
     *
63
     * @param \SimpleSAML\Auth\State $authState
64
     */
65
    public function setAuthState(Auth\State $authState): void
66
    {
67
        $this->authState = $authState;
68
    }
69
70
71
    /**
72
     * About to expire.
73
     *
74
     * @param \Symfony\Component\HttpFoundation\Request $request The current request.
75
     *
76
     * @return \SimpleSAML\XHTML\Template|\SimpleSAML\HTTP\RunnableResponse
77
     */
78
    public function about2expire(Request $request)
79
    {
80
        Logger::info('expirycheck - User has been warned that NetID is near to expirational date.');
81
82
        $stateId = $request->get('StateId');
83
        if ($stateId === null) {
84
            throw new Error\BadRequest('Missing required StateId query parameter.');
85
        }
86
87
        /** @psalm-var array $state */
88
        $state = $this->authState::loadState($stateId, 'expirywarning:about2expire');
89
90
        if ($request->get('yes') !== null) {
91
            // The user has pressed the yes-button
92
            return new RunnableResponse([Auth\ProcessingChain::class, 'resumeProcessing'], [$state]);
93
        }
94
95
        $daysleft = $state['daysleft'];
0 ignored issues
show
Unused Code introduced by
The assignment to $daysleft is dead and can be removed.
Loading history...
96
97
        $t = new Template($this->config, 'expirycheck:about2expire.twig');
98
        $t->data['autofocus'] = 'yesbutton';
99
        $t->data['yesTarget'] = Module::getModuleURL('expirycheck/about2expire');
100
        $t->data['yesData'] = ['StateId' => $stateId];
101
        $t->data['expireOnDate'] = $state['expireOnDate'];
102
        $t->data['netId'] = htmlspecialchars($state['netId']);
103
104
        return $t;
105
    }
106
107
108
    /**
109
     * Expired.
110
     *
111
     * @param \Symfony\Component\HttpFoundation\Request $request The current request.
112
     *
113
     * @return \SimpleSAML\XHTML\Template
114
     */
115
    public function expired(Request $request): Template
116
    {
117
        Logger::info('expirycheck - User has been warned that NetID is near to expirational date.');
118
119
        $stateId = $request->get('StateId');
120
        if ($stateId === null) {
121
            throw new Error\BadRequest('Missing required StateId query parameter.');
122
        }
123
124
        /** @psalm-var array $state */
125
        $state = $this->authState::loadState($stateId, 'expirywarning:expired');
126
127
        $t = new Template($this->config, 'expirycheck:expired.twig');
128
        $t->data['expireOnDate'] = $state['expireOnDate'];
129
        $t->data['netId'] = $state['netId'];
130
131
        return $t;
132
    }
133
}
134