Passed
Push — master ( da5844...d3a53d )
by Morris
12:45 queued 10s
created

BackgroundService   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
eloc 81
dl 0
loc 137
rs 10
c 7
b 0
f 0
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setFileBackground() 0 5 1
A setShippedBackground() 0 5 2
A setDefaultBackground() 0 2 1
A setColorBackground() 0 5 2
A __construct() 0 12 3
A getBackground() 0 9 3
1
<?php
2
/**
3
 * @copyright Copyright (c) 2020 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[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
24
declare(strict_types=1);
25
26
27
namespace OCA\Dashboard\Service;
28
29
use OCP\Files\IAppData;
30
use OCP\Files\IRootFolder;
31
use OCP\Files\NotFoundException;
32
use OCP\Files\SimpleFS\ISimpleFile;
33
use OCP\IConfig;
34
35
class BackgroundService {
36
	public const THEMING_MODE_DARK = 'dark';
37
38
	public const SHIPPED_BACKGROUNDS = [
39
		'anatoly-mikhaltsov-butterfly-wing-scale.jpg' => [
40
			'attribution' => 'Butterfly wing scale (Anatoly Mikhaltsov, CC BY-SA)',
41
			'attribution_url' => 'https://commons.wikimedia.org/wiki/File:%D0%A7%D0%B5%D1%88%D1%83%D0%B9%D0%BA%D0%B8_%D0%BA%D1%80%D1%8B%D0%BB%D0%B0_%D0%B1%D0%B0%D0%B1%D0%BE%D1%87%D0%BA%D0%B8.jpg',
42
		],
43
		'bernie-cetonia-aurata-take-off-composition.jpg' => [
44
			'attribution' => 'Cetonia aurata take off composition (Bernie, Public Domain)',
45
			'attribution_url' => 'https://commons.wikimedia.org/wiki/File:Cetonia_aurata_take_off_composition_05172009.jpg',
46
			'theming' => self::THEMING_MODE_DARK,
47
		],
48
		'dejan-krsmanovic-ribbed-red-metal.jpg' => [
49
			'attribution' => 'Ribbed red metal (Dejan Krsmanovic, CC BY)',
50
			'attribution_url' => 'https://www.flickr.com/photos/dejankrsmanovic/42971456774/',
51
		],
52
		'eduardo-neves-pedra-azul.jpg' => [
53
			'attribution' => 'Pedra azul milky way (Eduardo Neves, CC BY-SA)',
54
			'attribution_url' => 'https://commons.wikimedia.org/wiki/File:Pedra_Azul_Milky_Way.jpg',
55
		],
56
		'european-space-agency-barents-bloom.jpg' => [
57
			'attribution' => 'Barents bloom (European Space Agency, CC BY-SA)',
58
			'attribution_url' => 'https://www.esa.int/ESA_Multimedia/Images/2016/08/Barents_bloom',
59
		],
60
		'hannes-fritz-flippity-floppity.jpg' => [
61
			'attribution' => 'Flippity floppity (Hannes Fritz, CC BY-SA)',
62
			'attribution_url' => 'http://hannes.photos/flippity-floppity',
63
		],
64
		'hannes-fritz-roulette.jpg' => [
65
			'attribution' => 'Roulette (Hannes Fritz, CC BY-SA)',
66
			'attribution_url' => 'http://hannes.photos/roulette',
67
		],
68
		'hannes-fritz-sea-spray.jpg' => [
69
			'attribution' => 'Sea spray (Hannes Fritz, CC BY-SA)',
70
			'attribution_url' => 'http://hannes.photos/sea-spray',
71
		],
72
		'kamil-porembinski-clouds.jpg' => [
73
			'attribution' => 'Clouds (Kamil Porembiński, CC BY-SA)',
74
			'attribution_url' => 'https://www.flickr.com/photos/paszczak000/8715851521/',
75
		],
76
		'bernard-spragg-new-zealand-fern.jpg' => [
77
			'attribution' => 'New zealand fern (Bernard Spragg, CC0)',
78
			'attribution_url' => 'https://commons.wikimedia.org/wiki/File:NZ_Fern.(Blechnum_chambersii)_(11263534936).jpg',
79
		],
80
		'rawpixel-pink-tapioca-bubbles.jpg' => [
81
			'attribution' => 'Pink tapioca bubbles (Rawpixel, CC BY)',
82
			'attribution_url' => 'https://www.flickr.com/photos/byrawpixel/27665140298/in/photostream/',
83
			'theming' => self::THEMING_MODE_DARK,
84
		],
85
		'nasa-waxing-crescent-moon.jpg' => [
86
			'attribution' => 'Waxing crescent moon (NASA, Public Domain)',
87
			'attribution_url' => 'https://www.nasa.gov/image-feature/a-waxing-crescent-moon',
88
		],
89
		'tommy-chau-already.jpg' => [
90
			'attribution' => 'Cityscape (Tommy Chau, CC BY)',
91
			'attribution_url' => 'https://www.flickr.com/photos/90975693@N05/16910999368',
92
		],
93
		'tommy-chau-lion-rock-hill.jpg' => [
94
			'attribution' => 'Lion rock hill (Tommy Chau, CC BY)',
95
			'attribution_url' => 'https://www.flickr.com/photos/90975693@N05/17136440246',
96
			'theming' => self::THEMING_MODE_DARK,
97
		],
98
		'lali-masriera-yellow-bricks.jpg' => [
99
			'attribution' => 'Yellow bricks (Lali Masriera, CC BY)',
100
			'attribution_url' => 'https://www.flickr.com/photos/visualpanic/3982464447',
101
			'theming' => self::THEMING_MODE_DARK,
102
		]
103
	];
104
	/**
105
	 * @var \OCP\Files\Folder
106
	 */
107
	private $userFolder;
108
	/**
109
	 * @var \OCP\Files\SimpleFS\ISimpleFolder
110
	 */
111
	private $dashboardUserFolder;
112
	/**
113
	 * @var IConfig
114
	 */
115
	private $config;
116
	private $userId;
117
118
	public function __construct(IRootFolder $rootFolder, IAppData $appData, IConfig $config, $userId) {
119
		if ($userId === null) {
120
			return;
121
		}
122
		$this->userFolder = $rootFolder->getUserFolder($userId);
123
		try {
124
			$this->dashboardUserFolder = $appData->getFolder($userId);
125
		} catch (NotFoundException $e) {
126
			$this->dashboardUserFolder = $appData->newFolder($userId);
127
		}
128
		$this->config = $config;
129
		$this->userId = $userId;
130
	}
131
132
	public function setDefaultBackground(): void {
133
		$this->config->deleteUserValue($this->userId, 'dashboard', 'background');
134
	}
135
136
	/**
137
	 * @param $path
138
	 * @throws NotFoundException
139
	 * @throws \OCP\Files\NotPermittedException
140
	 * @throws \OCP\PreConditionNotMetException
141
	 */
142
	public function setFileBackground($path): void {
143
		$this->config->setUserValue($this->userId, 'dashboard', 'background', 'custom');
144
		/** @var \OCP\Files\File $file */
145
		$file = $this->userFolder->get($path);
146
		$this->dashboardUserFolder->newFile('background.jpg', $file->fopen('r'));
147
	}
148
149
	public function setShippedBackground($fileName): void {
150
		if (!array_key_exists($fileName, self::SHIPPED_BACKGROUNDS)) {
151
			throw new \InvalidArgumentException('The given file name is invalid');
152
		}
153
		$this->config->setUserValue($this->userId, 'dashboard', 'background', $fileName);
154
	}
155
156
	public function setColorBackground(string $color): void {
157
		if (!preg_match('/^\#([0-9a-f]{3}|[0-9a-f]{6})$/i', $color)) {
158
			throw new \InvalidArgumentException('The given color is invalid');
159
		}
160
		$this->config->setUserValue($this->userId, 'dashboard', 'background', $color);
161
	}
162
163
	public function getBackground(): ?ISimpleFile {
164
		$background = $this->config->getUserValue($this->userId, 'dashboard', 'background', 'default');
165
		if ($background === 'custom') {
166
			try {
167
				return $this->dashboardUserFolder->getFile('background.jpg');
168
			} catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
169
			}
170
		}
171
		return null;
172
	}
173
}
174