PreProdWarning::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\preprodwarning\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
use Symfony\Component\HttpFoundation\Response;
17
18
/**
19
 * Controller class for the preprodwarning module.
20
 *
21
 * This class serves the different views available in the module.
22
 *
23
 * @package simplesamlphp/simplesamlphp-module-preprodwarning
24
 */
25
class PreProdWarning
26
{
27
    /**
28
     * @var \SimpleSAML\Auth\State|string
29
     * @psalm-var \SimpleSAML\Auth\State|class-string
30
     */
31
    protected $authState = Auth\State::class;
32
33
    /**
34
     * @var \SimpleSAML\Auth\ProcessingChain|string
35
     * @psalm-var \SimpleSAML\Auth\ProcessingChain|class-string
36
     */
37
    protected $procChain = Auth\ProcessingChain::class;
38
39
    /**
40
     * @var \SimpleSAML\Logger|string
41
     * @psalm-var \SimpleSAML\Logger|class-string
42
     */
43
    protected $logger = Logger::class;
44
45
    /** @var \SimpleSAML\Configuration */
46
    protected Configuration $config;
47
48
    /** @var \SimpleSAML\Session */
49
    protected Session $session;
50
51
52
    /**
53
     * Controller constructor.
54
     *
55
     * It initializes the global configuration and session for the controllers implemented here.
56
     *
57
     * @param \SimpleSAML\Configuration $config The configuration to use by the controllers.
58
     * @param \SimpleSAML\Session $session The session to use by the controllers.
59
     *
60
     * @throws \Exception
61
     */
62
    public function __construct(
63
        Configuration $config,
64
        Session $session,
65
    ) {
66
        $this->config = $config;
67
        $this->session = $session;
68
    }
69
70
71
    /**
72
     * Inject the \SimpleSAML\Auth\State dependency.
73
     *
74
     * @param \SimpleSAML\Auth\State $authState
75
     */
76
    public function setAuthState(Auth\State $authState): void
77
    {
78
        $this->authState = $authState;
79
    }
80
81
82
    /**
83
     * Inject the \SimpleSAML\Auth\ProcessingChain dependency.
84
     *
85
     * @param \SimpleSAML\Auth\ProcessingChain $procChain
86
     */
87
    public function setProcessingChain(Auth\ProcessingChain $procChain): void
88
    {
89
        $this->procChain = $procChain;
90
    }
91
92
93
    /**
94
     * Inject the \SimpleSAML\Logger dependency.
95
     *
96
     * @param \SimpleSAML\Logger $logger
97
     */
98
    public function setLogger(Logger $logger): void
99
    {
100
        $this->logger = $logger;
101
    }
102
103
104
    /**
105
     * Show warning.
106
     *
107
     * @param \Symfony\Component\HttpFoundation\Request $request
108
     * @return \Symfony\Component\HttpFoundation\Response
109
     * @throws \Exception
110
     */
111
    public function main(Request $request): Response
112
    {
113
        $this->logger::info('PreProdWarning - Showing warning to user');
114
115
        $id = $request->query->get('StateId', null);
116
        if ($id === null) {
117
            throw new Error\BadRequest('Missing required StateId query parameter.');
118
        }
119
120
        /** @psalm-var array $state */
121
        $state = $this->authState::loadState($id, 'warning:request');
122
123
        if ($request->query->get('yes')) {
124
            // The user has pressed the yes-button
125
            return new RunnableResponse([$this->procChain, 'resumeProcessing'], [$state]);
126
        }
127
128
        $t = new Template($this->config, 'preprodwarning:warning.twig');
129
        $t->data['yesTarget'] = Module::getModuleURL('preprodwarning/warning');
130
        $t->data['yesData'] = ['StateId' => $id];
131
        return $t;
132
    }
133
}
134