Passed
Push — master ( 34ef5e...ffe1e4 )
by Marcel
08:33
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
        return new TemplateResponse($this->appName, 'main');
66
    }
67
68
    /**
69
     * @NoAdminRequired
70
     * @NoCSRFRequired
71
     */
72
    public function config()
73
    {
74
        return new TemplateResponse($this->appName, 'main_config');
75
    }
76
77
    /**
78
     * @PublicPage
79
     * @NoCSRFRequired
80
     * @UseSession
81
     *
82
     * @param string $token
83
     * @param string $password
84
     * @return RedirectResponse|TemplateResponse
85
     */
86
    public function authenticatePassword(string $token, string $password = '')
87
    {
88
        return $this->indexPublic($token, $password);
89
    }
90
91
    /**
92
     * @PublicPage
93
     * @UseSession
94
     * @NoCSRFRequired
95
     * @param $token
96
     * @param string $password
97
     * @return TemplateResponse|RedirectResponse
98
     */
99
    public function indexPublic($token, string $password = '')
100
    {
101
        $share = $this->ShareController->getDatasetByToken($token);
102
103
        if (empty($share)) {
104
            // Dataset not shared or wrong token
105
            return new RedirectResponse($this->url->linkToRoute('core.login.showLoginForm', [
106
                'redirect_url' => $this->url->linkToRoute($this->appName . '.page.index', ['token' => $token]),
107
            ]));
108
        } else {
109
            if ($share['password'] !== null) {
110
                $password = $password !== '' ? $password : (string)$this->DataSession->getPasswordForShare($token);
111
                $passwordVerification = $this->ShareController->verifyPassword($password, $share['password']);
112
                if ($passwordVerification === true) {
113
                    $this->DataSession->setPasswordForShare($token, $password);
114
                } else {
115
                    $this->DataSession->removePasswordForShare($token);
116
                    return new TemplateResponse($this->appName, 'authenticate', ['wrongpw' => $password !== '',], 'guest');
117
                }
118
            }
119
            $params = array();
120
            $params['token'] = $token;
121
            return new TemplateResponse($this->appName, 'public', $params);
122
        }
123
    }
124
}