Passed
Push — master ( 5cdc85...37718d )
by Morris
38:53 queued 21:57
created

IconController::getTouchIcon()   A

Complexity

Conditions 6
Paths 30

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 18
nc 30
nop 1
dl 0
loc 24
rs 9.0444
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Julius Haertl <[email protected]>
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 * @author Julius Haertl <[email protected]>
7
 * @author Julius Härtl <[email protected]>
8
 *
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
namespace OCA\Theming\Controller;
26
27
use OC\IntegrityCheck\Helpers\FileAccessHelper;
28
use OCA\Theming\IconBuilder;
29
use OCA\Theming\ImageManager;
30
use OCA\Theming\ThemingDefaults;
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\DataDisplayResponse;
36
use OCP\AppFramework\Http\Response;
37
use OCP\Files\NotFoundException;
38
use OCP\IRequest;
39
40
class IconController extends Controller {
41
	/** @var ThemingDefaults */
42
	private $themingDefaults;
43
	/** @var IconBuilder */
44
	private $iconBuilder;
45
	/** @var ImageManager */
46
	private $imageManager;
47
	/** @var FileAccessHelper */
48
	private $fileAccessHelper;
49
50
	/**
51
	 * IconController constructor.
52
	 *
53
	 * @param string $appName
54
	 * @param IRequest $request
55
	 * @param ThemingDefaults $themingDefaults
56
	 * @param IconBuilder $iconBuilder
57
	 * @param ImageManager $imageManager
58
	 * @param FileAccessHelper $fileAccessHelper
59
	 */
60
	public function __construct(
61
		$appName,
62
		IRequest $request,
63
		ThemingDefaults $themingDefaults,
64
		IconBuilder $iconBuilder,
65
		ImageManager $imageManager,
66
		FileAccessHelper $fileAccessHelper
67
	) {
68
		parent::__construct($appName, $request);
69
70
		$this->themingDefaults = $themingDefaults;
71
		$this->iconBuilder = $iconBuilder;
72
		$this->imageManager = $imageManager;
73
		$this->fileAccessHelper = $fileAccessHelper;
74
	}
75
76
	/**
77
	 * @PublicPage
78
	 * @NoCSRFRequired
79
	 *
80
	 * @param $app string app name
81
	 * @param $image string image file name (svg required)
82
	 * @return FileDisplayResponse|NotFoundResponse
83
	 * @throws \Exception
84
	 */
85
	public function getThemedIcon(string $app, string $image): Response {
86
		try {
87
			$iconFile = $this->imageManager->getCachedImage('icon-' . $app . '-' . str_replace('/', '_',$image));
88
		} catch (NotFoundException $exception) {
89
			$icon = $this->iconBuilder->colorSvg($app, $image);
90
			if ($icon === false || $icon === '') {
91
				return new NotFoundResponse();
92
			}
93
			$iconFile = $this->imageManager->setCachedImage('icon-' . $app . '-' . str_replace('/', '_',$image), $icon);
94
		}
95
		if ($iconFile !== false) {
0 ignored issues
show
introduced by
The condition $iconFile !== false is always true.
Loading history...
96
			$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
97
			$response->cacheFor(86400);
98
			return $response;
99
		}
100
101
		return new NotFoundResponse();
102
	}
103
104
	/**
105
	 * Return a 32x32 favicon as png
106
	 *
107
	 * @PublicPage
108
	 * @NoCSRFRequired
109
	 *
110
	 * @param $app string app name
111
	 * @return FileDisplayResponse|DataDisplayResponse
112
	 * @throws \Exception
113
	 */
114
	public function getFavicon(string $app = 'core'): Response {
115
		$response = null;
116
		$iconFile = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $iconFile is dead and can be removed.
Loading history...
117
		try {
118
			$iconFile = $this->imageManager->getImage('favicon', false);
119
			$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
120
		} catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
121
		}
122
		if ($iconFile === null && $this->imageManager->shouldReplaceIcons()) {
123
			try {
124
				$iconFile = $this->imageManager->getCachedImage('favIcon-' . $app);
125
			} catch (NotFoundException $exception) {
126
				$icon = $this->iconBuilder->getFavicon($app);
127
				$iconFile = $this->imageManager->setCachedImage('favIcon-' . $app, $icon);
0 ignored issues
show
Bug introduced by
It seems like $icon can also be of type false; however, parameter $data of OCA\Theming\ImageManager::setCachedImage() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

127
				$iconFile = $this->imageManager->setCachedImage('favIcon-' . $app, /** @scrutinizer ignore-type */ $icon);
Loading history...
128
			}
129
			if ($iconFile !== false) {
0 ignored issues
show
introduced by
The condition $iconFile !== false is always true.
Loading history...
130
				$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
131
			}
132
		}
133
		if($response === null) {
134
			$fallbackLogo = \OC::$SERVERROOT . '/core/img/favicon.png';
135
			$response = new DataDisplayResponse($this->fileAccessHelper->file_get_contents($fallbackLogo), Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
136
		}
137
		$response->cacheFor(86400);
138
		return $response;
139
	}
140
141
	/**
142
	 * Return a 512x512 icon for touch devices
143
	 *
144
	 * @PublicPage
145
	 * @NoCSRFRequired
146
	 *
147
	 * @param $app string app name
148
	 * @return FileDisplayResponse|NotFoundResponse
149
	 * @throws \Exception
150
	 */
151
	public function getTouchIcon(string $app = 'core'): Response {
152
		$response = null;
153
		try {
154
			$iconFile = $this->imageManager->getImage('favicon');
155
			$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
156
		} catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
157
		}
158
		if ($this->imageManager->shouldReplaceIcons()) {
159
			try {
160
				$iconFile = $this->imageManager->getCachedImage('touchIcon-' . $app);
161
			} catch (NotFoundException $exception) {
162
				$icon = $this->iconBuilder->getTouchIcon($app);
163
				$iconFile = $this->imageManager->setCachedImage('touchIcon-' . $app, $icon);
0 ignored issues
show
Bug introduced by
It seems like $icon can also be of type false; however, parameter $data of OCA\Theming\ImageManager::setCachedImage() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

163
				$iconFile = $this->imageManager->setCachedImage('touchIcon-' . $app, /** @scrutinizer ignore-type */ $icon);
Loading history...
164
			}
165
			if ($iconFile !== false) {
0 ignored issues
show
introduced by
The condition $iconFile !== false is always true.
Loading history...
166
				$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/png']);
167
			}
168
		}
169
		if($response === null) {
170
			$fallbackLogo = \OC::$SERVERROOT . '/core/img/favicon-touch.png';
171
			$response = new DataDisplayResponse($this->fileAccessHelper->file_get_contents($fallbackLogo), Http::STATUS_OK, ['Content-Type' => 'image/png']);
172
		}
173
		$response->cacheFor(86400);
174
		return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response also could return the type OCP\AppFramework\Http\DataDisplayResponse which is incompatible with the documented return type OCP\AppFramework\Http\Fi...k\Http\NotFoundResponse.
Loading history...
175
	}
176
}
177