Completed
Push — master ( af3ab1...c8340a )
by Morris
21:16 queued 06:37
created

CssController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 8
dl 74
loc 74
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 1
B getCss() 24 24 3
A getFile() 15 15 3

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
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2016, John Molakvoæ ([email protected])
5
 *
6
 * @author Joas Schilling <[email protected]>
7
 * @author John Molakvoæ (skjnldsv) <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 * @author Roeland Jago Douma <[email protected]>
10
 *
11
 * @license GNU AGPL version 3 or any later version
12
 *
13
 * This program is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License as
15
 * published by the Free Software Foundation, either version 3 of the
16
 * License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License
24
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25
 *
26
 */
27
28
namespace OC\Core\Controller;
29
30
use OC\Files\AppData\Factory;
31
use OCP\AppFramework\Controller;
32
use OCP\AppFramework\Http;
33
use OCP\AppFramework\Http\NotFoundResponse;
34
use OCP\AppFramework\Http\FileDisplayResponse;
35
use OCP\AppFramework\Http\Response;
36
use OCP\AppFramework\Utility\ITimeFactory;
37
use OCP\Files\IAppData;
38
use OCP\Files\NotFoundException;
39
use OCP\Files\SimpleFS\ISimpleFile;
40
use OCP\Files\SimpleFS\ISimpleFolder;
41
use OCP\IConfig;
42
use OCP\IRequest;
43
44 View Code Duplication
class CssController 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...
45
46
	/** @var IAppData */
47
	protected $appData;
48
49
	/** @var ITimeFactory */
50
	protected $timeFactory;
51
52
	public function __construct(string $appName,
53
								IRequest $request,
54
								Factory $appDataFactory,
55
								ITimeFactory $timeFactory) {
56
		parent::__construct($appName, $request);
57
58
		$this->appData = $appDataFactory->get('css');
59
		$this->timeFactory = $timeFactory;
60
	}
61
62
	/**
63
	 * @PublicPage
64
	 * @NoCSRFRequired
65
	 *
66
	 * @param string $fileName css filename with extension
67
	 * @param string $appName css folder name
68
	 * @return FileDisplayResponse|NotFoundResponse
69
	 */
70
	public function getCss(string $fileName, string $appName): Response {
71
		try {
72
			$folder = $this->appData->getFolder($appName);
73
			$gzip = false;
74
			$file = $this->getFile($folder, $fileName, $gzip);
75
		} catch(NotFoundException $e) {
76
			return new NotFoundResponse();
77
		}
78
79
		$response = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'text/css']);
80
		if ($gzip) {
81
			$response->addHeader('Content-Encoding', 'gzip');
82
		}
83
84
		$ttl = 31536000;
85
		$response->addHeader('Cache-Control', 'max-age='.$ttl.', immutable');
86
87
		$expires = new \DateTime();
88
		$expires->setTimestamp($this->timeFactory->getTime());
89
		$expires->add(new \DateInterval('PT'.$ttl.'S'));
90
		$response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
91
		$response->addHeader('Pragma', 'cache');
92
		return $response;
93
	}
94
95
	/**
96
	 * @param ISimpleFolder $folder
97
	 * @param string $fileName
98
	 * @param bool $gzip is set to true if we use the gzip file
99
	 * @return ISimpleFile
100
	 * @throws NotFoundException
101
	 */
102
	private function getFile(ISimpleFolder $folder, string $fileName, bool &$gzip): ISimpleFile {
103
		$encoding = $this->request->getHeader('Accept-Encoding');
104
105
		if (strpos($encoding, 'gzip') !== false) {
106
			try {
107
				$gzip = true;
108
				return $folder->getFile($fileName . '.gzip'); # Safari doesn't like .gz
109
			} catch (NotFoundException $e) {
110
				// continue
111
			}
112
		}
113
114
		$gzip = false;
115
		return $folder->getFile($fileName);
116
	}
117
}
118