Passed
Push — master ( 4390b1...293f44 )
by Marcel
02:24
created

PageController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 8
dl 0
loc 19
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * Data Analytics
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the LICENSE.md file.
7
 *
8
 * @author Marcel Scherello <[email protected]>
9
 * @copyright 2019 Marcel Scherello
10
 */
11
12
namespace OCA\Analytics\Controller;
13
14
use OCA\Analytics\DataSession;
15
use OCP\AppFramework\Controller;
16
use OCP\AppFramework\Http\RedirectResponse;
17
use OCP\AppFramework\Http\TemplateResponse;
18
use OCP\IConfig;
19
use OCP\ILogger;
20
use OCP\IRequest;
21
use OCP\IURLGenerator;
22
23
/**
24
 * Controller class for main page.
25
 */
26
class PageController extends Controller
27
{
28
    protected $appName;
29
    private $userId;
30
    private $configManager;
31
    private $logger;
32
    private $ShareController;
33
    /** @var IURLGenerator */
34
    private $url;
35
    /** @var DataSession */
36
    private $DataSession;
37
38
    public function __construct(
39
        string $appName,
40
        IRequest $request,
41
        $userId,
42
        ILogger $logger,
43
        IConfig $configManager,
44
        IURLGenerator $url,
45
        ShareController $ShareController,
46
        DataSession $DataSession
47
    )
48
    {
49
        parent::__construct($appName, $request);
50
        $this->appName = $appName;
51
        $this->userId = $userId;
52
        $this->configManager = $configManager;
53
        $this->logger = $logger;
54
        $this->url = $url;
55
        $this->ShareController = $ShareController;
56
        $this->DataSession = $DataSession;
57
    }
58
59
    /**
60
     * @NoAdminRequired
61
     * @NoCSRFRequired
62
     */
63
    public function index()
64
    {
65
        $params = array();
66
        $params['token'] = '';
67
        return new TemplateResponse($this->appName, 'main', $params);
68
    }
69
70
    /**
71
     * @NoAdminRequired
72
     * @NoCSRFRequired
73
     */
74
    public function advanced()
75
    {
76
        return new TemplateResponse($this->appName, 'main_advanced');
77
    }
78
79
    /**
80
     * @PublicPage
81
     * @NoCSRFRequired
82
     * @UseSession
83
     *
84
     * @param string $token
85
     * @param string $password
86
     * @return RedirectResponse|TemplateResponse
87
     */
88
    public function authenticatePassword(string $token, string $password = '')
89
    {
90
        return $this->indexPublic($token, $password);
91
    }
92
93
    /**
94
     * @PublicPage
95
     * @UseSession
96
     * @NoCSRFRequired
97
     * @param $token
98
     * @param string $password
99
     * @return TemplateResponse|RedirectResponse
100
     */
101
    public function indexPublic($token, string $password = '')
102
    {
103
        $share = $this->ShareController->getDatasetByToken($token);
104
105
        if (empty($share)) {
106
            // Dataset not shared or wrong token
107
            return new RedirectResponse($this->url->linkToRoute('core.login.showLoginForm', [
108
                'redirect_url' => $this->url->linkToRoute($this->appName . '.page.index', ['token' => $token]),
109
            ]));
110
        } else {
111
            if ($share['password'] !== null) {
112
                $password = $password !== '' ? $password : (string)$this->DataSession->getPasswordForShare($token);
113
                $passwordVerification = $this->ShareController->verifyPassword($password, $share['password']);
114
                if ($passwordVerification === true) {
115
                    $this->DataSession->setPasswordForShare($token, $password);
116
                } else {
117
                    $this->DataSession->removePasswordForShare($token);
118
                    return new TemplateResponse($this->appName, 'authenticate', ['wrongpw' => $password !== '',], 'guest');
119
                }
120
            }
121
            $params = array();
122
            $params['token'] = $token;
123
            return new TemplateResponse($this->appName, 'public', $params);
124
        }
125
    }
126
}