Completed
Push — master ( 94ab2e...2cd79a )
by Lukas
12:30
created

JsController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 47
loc 47
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 6 6 1
A getJs() 17 17 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @copyright 2017, Roeland Jago Douma <[email protected]>
4
 *
5
 * @author Roeland Jago Douma <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
namespace OC\Core\Controller;
24
25
use OCP\AppFramework\Controller;
26
use OCP\AppFramework\Http;
27
use OCP\AppFramework\Http\NotFoundResponse;
28
use OCP\AppFramework\Http\FileDisplayResponse;
29
use OCP\AppFramework\Utility\ITimeFactory;
30
use OCP\Files\IAppData;
31
use OCP\Files\NotFoundException;
32
use OCP\IRequest;
33
34 View Code Duplication
class JsController extends Controller {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
36
	/** @var IAppData */
37
	protected $appData;
38
39
	/** @var ITimeFactory */
40
	protected $timeFactory;
41
42
	/**
43
	 * @param string $appName
44
	 * @param IRequest $request
45
	 * @param IAppData $appData
46
	 * @param ITimeFactory $timeFactory
47
	 */
48
	public function __construct($appName, IRequest $request, IAppData $appData, ITimeFactory $timeFactory) {
49
		parent::__construct($appName, $request);
50
51
		$this->appData = $appData;
52
		$this->timeFactory = $timeFactory;
53
	}
54
55
	/**
56
	 * @PublicPage
57
	 * @NoCSRFRequired
58
	 *
59
	 * @param string $fileName css filename with extension
60
	 * @param string $appName css folder name
61
	 * @return FileDisplayResponse|NotFoundResponse
62
	 */
63
	public function getJs($fileName, $appName) {
64
		try {
65
			$folder = $this->appData->getFolder($appName);
66
			$jsFile = $folder->getFile($fileName);
67
		} catch(NotFoundException $e) {
68
			return new NotFoundResponse();
69
		}
70
71
		$response = new FileDisplayResponse($jsFile, Http::STATUS_OK, ['Content-Type' => 'application/javascript']);
72
		$response->cacheFor(86400);
73
		$expires = new \DateTime();
74
		$expires->setTimestamp($this->timeFactory->getTime());
75
		$expires->add(new \DateInterval('PT24H'));
76
		$response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
77
		$response->addHeader('Pragma', 'cache');
78
		return $response;
79
	}
80
}
81