Passed
Branch master (7b1276)
by Marcel
06:24
created

PageController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
c 0
b 0
f 0
nc 1
nop 9
dl 0
loc 20
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\IL10N;
20
use OCP\ILogger;
21
use OCP\IRequest;
22
use OCP\IURLGenerator;
23
24
/**
25
 * Controller class for main page.
26
 */
27
class PageController extends Controller
28
{
29
    private $userId;
30
    private $l10n;
31
    private $configManager;
32
    private $logger;
33
    private $share;
34
    /** @var IURLGenerator */
35
    private $url;
36
    /** @var DataSession */
37
    private $DataSession;
38
39
    public function __construct(
40
        string $AppName,
41
        IRequest $request,
42
        $userId,
43
        ILogger $logger,
44
        IConfig $configManager,
45
        IL10N $l10n,
46
        IURLGenerator $url,
47
        ShareController $share,
48
        DataSession $DataSession
49
    )
50
    {
51
        parent::__construct($AppName, $request);
52
        $this->userId = $userId;
53
        $this->configManager = $configManager;
54
        $this->l10n = $l10n;
55
        $this->logger = $logger;
56
        $this->url = $url;
57
        $this->share = $share;
58
        $this->DataSession = $DataSession;
59
    }
60
61
    /**
62
     * @NoAdminRequired
63
     * @NoCSRFRequired
64
     */
65
    public function index()
66
    {
67
        $response = new TemplateResponse('analytics', 'main');
68
        return $response;
69
    }
70
71
    /**
72
     * @PublicPage
73
     * @NoCSRFRequired
74
     * @UseSession
75
     *
76
     * @param string $token
77
     * @param string $password
78
     * @return RedirectResponse|TemplateResponse
79
     */
80
    public function authenticatePassword(string $token, string $password = '')
81
    {
82
        return $this->indexPublic($token, $password);
83
    }
84
85
    /**
86
     * @PublicPage
87
     * @UseSession
88
     * @NoCSRFRequired
89
     * @param $token
90
     * @param string $password
91
     * @return TemplateResponse|RedirectResponse
92
     */
93
    public function indexPublic($token, string $password = '')
94
    {
95
        $share = $this->share->getDatasetByToken($token);
96
97
        if ($share === false) {
0 ignored issues
show
introduced by
The condition $share === false is always false.
Loading history...
98
            // Dataset not shared or wrong token
99
            return new RedirectResponse($this->url->linkToRoute('core.login.showLoginForm', [
100
                'redirect_url' => $this->url->linkToRoute('analytics.page.index', ['token' => $token]),
101
            ]));
102
        } else {
103
            if ($share['password'] !== null) {
104
                $password = $password !== '' ? $password : (string)$this->DataSession->getPasswordForShare($token);
105
                $passwordVerification = $this->share->verifyPassword($password, $share['password']);
106
                if ($passwordVerification === true) {
107
                    $this->DataSession->setPasswordForShare($token, $password);
108
                } else {
109
                    $this->DataSession->removePasswordForShare($token);
110
                    return new TemplateResponse('analytics', 'authenticate', ['wrongpw' => $password !== '',], 'guest');
111
                }
112
            }
113
            $params = array();
114
            $params['token'] = $token;
115
            $response = new TemplateResponse('analytics', 'public', $params);
116
            return $response;
117
        }
118
    }
119
}