Passed
Push — master ( 442fd6...c2e31f )
by Maxence
02:11
created

PicoService   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
wmc 7
c 7
b 0
f 0
dl 0
loc 94
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A generateConfig() 0 8 1
A getAbsolutePathFromPage() 0 2 1
A pluginNextcloudMustBeLoaded() 0 6 2
A getContent() 0 21 2
1
<?php
2
/**
3
 * CMS Pico - Integration of Pico within your files to create websites.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\CMSPico\Service;
28
29
use Exception;
30
use HTMLPurifier;
31
use HTMLPurifier_Config;
32
use OC\App\AppManager;
33
use OCA\CMSPico\AppInfo\Application;
34
use OCA\CMSPico\Exceptions\PicoRuntimeException;
35
use OCA\CMSPico\Exceptions\PluginNextcloudNotLoadedException;
36
use OCA\CMSPico\Model\Website;
37
use Pico;
38
39
class PicoService {
40
41
	const DIR_CONFIG = 'Pico/config/';
42
	const DIR_PLUGINS = 'Pico/plugins/';
43
	const DIR_THEMES = 'Pico/themes/';
44
45
	const NC_PLUGIN = 'Nextcloud';
46
47
	private $userId;
48
49
	/** @var AppManager */
50
	private $appManager;
51
52
	/** @var MiscService */
53
	private $miscService;
54
55
	/**
56
	 * PicoService constructor.
57
	 *
58
	 * @param string $userId
59
	 * @param AppManager $appManager
60
	 * @param MiscService $miscService
61
	 */
62
	function __construct($userId, AppManager $appManager, MiscService $miscService) {
63
		$this->userId = $userId;
64
		$this->appManager = $appManager;
65
		$this->miscService = $miscService;
66
	}
67
68
69
	/**
70
	 * @param Website $website
71
	 *
72
	 * @return string
73
	 * @throws PicoRuntimeException
74
	 */
75
	public function getContent(Website $website) {
76
77
		$appPath = MiscService::endSlash($this->appManager->getAppPath(Application::APP_NAME));
78
		$pico = new Pico(
79
			$website->getAbsolutePath(), $appPath . self::DIR_CONFIG,
80
			$appPath . self::DIR_PLUGINS, $appPath . self::DIR_THEMES
81
		);
82
83
		$this->generateConfig($pico, $website);
84
		try {
85
			$content = $pico->run();
86
		} catch (\Exception $e) {
87
			throw new PicoRuntimeException($e->getMessage());
88
		}
89
90
		$this->pluginNextcloudMustBeLoaded($pico);
91
		$absolutePath = $this->getAbsolutePathFromPage($pico);
92
		$website->contentMustBeLocal($absolutePath);
93
		$website->viewerMustHaveAccess($absolutePath, $pico->getFileMeta());
94
95
		return $content;
96
	}
97
98
99
	/**
100
	 * @param Pico $pico
101
	 * @param Website $website
102
	 */
103
	private function generateConfig(Pico &$pico, Website $website) {
104
		$pico->setConfig(
105
			[
106
				'content_dir' => 'content/',
107
				'content_ext' => '.md',
108
				'theme'       => $website->getTheme(),
109
				'site_title'  => $website->getName(),
110
				'base_url'    => \OC::$WEBROOT . $website->getSite()
111
			]
112
		);
113
	}
114
115
116
	/**
117
	 * @param Pico $pico
118
	 *
119
	 * @return string
120
	 */
121
	private function getAbsolutePathFromPage(Pico $pico) {
122
		return $pico->getConfig()['content_dir'] . $pico->getCurrentPage()['id'] . '.md';
123
	}
124
125
126
127
	private function pluginNextcloudMustBeLoaded(Pico $pico) {
128
		try {
129
			$pico->getPlugin(self::NC_PLUGIN);
130
		} catch (Exception $e)
131
		{
132
			throw new PluginNextcloudNotLoadedException($e->getMessage());
133
		}
134
	}
135
}