PageController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 19
nc 1
nop 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
 * ownCloud - News
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Alessandro Cosentino <[email protected]>
9
 * @author Bernhard Posselt <[email protected]>
10
 * @copyright Alessandro Cosentino 2012
11
 * @copyright Bernhard Posselt 2012, 2014
12
 */
13
14
namespace OCA\News\Controller;
15
16
use OCP\IRequest;
17
use OCP\IConfig;
18
use OCP\IL10N;
19
use OCP\IURLGenerator;
20
use OCP\AppFramework\Controller;
21
use OCP\AppFramework\Http\TemplateResponse;
22
use OCP\AppFramework\Http\JSONResponse;
23
use OCP\AppFramework\Http;
24
use OCP\AppFramework\Http\ContentSecurityPolicy;
25
26
use OCA\News\Service\StatusService;
27
use OCA\News\Config\AppConfig;
28
use OCA\News\Config\Config;
29
use OCA\News\Explore\RecommendedSites;
30
use OCA\News\Explore\RecommendedSiteNotFoundException;
31
use OCA\News\Db\FeedType;
32
33
class PageController extends Controller {
34
35
    private $settings;
36
    private $l10n;
37
    private $userId;
38
    private $appConfig;
39
    private $urlGenerator;
40
    private $config;
41
    private $recommendedSites;
42
    private $statusService;
43
44
    use JSONHttpError;
45
46
    public function __construct($AppName,
47
                                IRequest $request,
48
                                IConfig $settings,
49
                                IURLGenerator $urlGenerator,
50
                                AppConfig $appConfig,
51
                                Config $config,
52
                                IL10N $l10n,
53
                                RecommendedSites $recommendedSites,
54
                                StatusService $statusService,
55
                                $UserId){
56
        parent::__construct($AppName, $request);
57
        $this->settings = $settings;
58
        $this->urlGenerator = $urlGenerator;
59
        $this->appConfig = $appConfig;
60
        $this->l10n = $l10n;
61
        $this->userId = $UserId;
62
        $this->config = $config;
63
        $this->recommendedSites = $recommendedSites;
64
        $this->statusService = $statusService;
65
    }
66
67
68
    /**
69
     * @NoAdminRequired
70
     * @NoCSRFRequired
71
     */
72
    public function index() {
73
        $status = $this->statusService->getStatus();
74
        $response = new TemplateResponse($this->appName, 'index', [
75
            'cronWarning' => $status['warnings']['improperlyConfiguredCron'],
76
            'url_generator' => $this->urlGenerator
77
        ]);
78
79
        $csp = new ContentSecurityPolicy();
80
        $csp->addAllowedImageDomain('*')
81
            ->addAllowedMediaDomain('*')
82
            ->addAllowedConnectDomain('*')  // chrome breaks on audio elements
83
            ->addAllowedFrameDomain('https://youtube.com')
84
            ->addAllowedFrameDomain('https://www.youtube.com')
85
            ->addAllowedFrameDomain('https://player.vimeo.com')
86
            ->addAllowedFrameDomain('https://www.player.vimeo.com')
87
            ->addAllowedFrameDomain('https://vk.com')
88
            ->addAllowedFrameDomain('https://www.vk.com');
89
        $response->setContentSecurityPolicy($csp);
90
91
        return $response;
92
    }
93
94
95
    /**
96
     * @NoAdminRequired
97
     */
98
    public function settings() {
99
        $settings = [
100
            'showAll',
101
            'compact',
102
            'preventReadOnScroll',
103
            'oldestFirst',
104
            'compactExpand'
105
        ];
106
107
        $exploreUrl = $this->config->getExploreUrl();
108
        if (trim($exploreUrl) === '') {
109
            // default url should not feature the sites.en.json
110
            $exploreUrl = $this->urlGenerator->linkToRoute(
111
                'news.page.explore', ['lang' => 'en']
112
            );
113
            $exploreUrl = preg_replace('/feeds\.en\.json$/', '', $exploreUrl);
114
        }
115
116
        $result = [
117
            'language' => $this->l10n->getLanguageCode(),
118
            'exploreUrl' => $exploreUrl
119
        ];
120
121
        foreach ($settings as $setting) {
122
            $result[$setting] = $this->settings->getUserValue(
123
                $this->userId, $this->appName, $setting
124
            ) === '1';
125
        }
126
        return ['settings' => $result];
127
    }
128
129
130
    /**
131
     * @NoAdminRequired
132
     *
133
     * @param bool $showAll
134
     * @param bool $compact
135
     * @param bool $preventReadOnScroll
136
     * @param bool $oldestFirst
137
     */
138
    public function updateSettings($showAll, $compact, $preventReadOnScroll,
139
                                   $oldestFirst, $compactExpand) {
140
        $settings = ['showAll',
141
            'compact',
142
            'preventReadOnScroll',
143
            'oldestFirst',
144
            'compactExpand'
145
        ];
146
147
        foreach ($settings as $setting) {
148
            if (${$setting}) {
149
                $value = '1';
150
            } else {
151
                $value = '0';
152
            }
153
            $this->settings->setUserValue($this->userId, $this->appName,
154
                                          $setting, $value);
155
        }
156
    }
157
158
159
    /**
160
     * @NoCSRFRequired
161
     * @PublicPage
162
     *
163
     * Generates a web app manifest, according to specs in:
164
     * https://developer.mozilla.org/en-US/Apps/Build/Manifest
165
     */
166
    public function manifest() {
167
        $config = $this->appConfig->getConfig();
168
169
        // size of the icons: 128x128 is required by FxOS for all app manifests
170
        $iconSizes = ['128', '512'];
171
        $icons = [];
172
173
        $locale = str_replace('_', '-', $this->l10n->getLanguageCode());
174
175
        foreach ($iconSizes as $size) {
176
            $filename = 'app-' . $size . '.png';
177
            if (file_exists(__DIR__ . '/../img/' . $filename)) {
178
                $icons[$size] = $this->urlGenerator->imagePath($config['id'],
179
                    $filename);
180
            }
181
        }
182
183
184
        $data = [
185
            "name" => $config['name'],
186
            "type" => 'web',
187
            "default_locale" => $locale,
188
            "description" => $config['description'],
189
            "launch_path" => $this->urlGenerator->linkToRoute(
190
                $config['navigation']['route']),
191
            "icons" => $icons,
192
            "developer" => [
193
                "name" => $config['author'],
194
                "url" => $config['homepage']
195
            ]
196
        ];
197
198
        $response = new JSONResponse($data);
199
        $response->addHeader('Content-Type',
200
            'application/x-web-app-manifest+json');
201
202
        return $response;
203
    }
204
205
    /**
206
     * @NoAdminRequired
207
     *
208
     * @param string $lang
209
     */
210
    public function explore($lang) {
211
        $this->settings->setUserValue($this->userId, $this->appName,
212
            'lastViewedFeedId', 0);
213
        $this->settings->setUserValue($this->userId, $this->appName,
214
            'lastViewedFeedType', FeedType::EXPLORE);
215
216
        try {
217
            return $this->recommendedSites->forLanguage($lang);
218
        } catch (RecommendedSiteNotFoundException $ex) {
219
            return $this->error($ex, Http::STATUS_NOT_FOUND);
220
        }
221
    }
222
223
224
}
225