Passed
Push — master ( 7c118a...93f8c8 )
by Maxence
02:48
created

TemplatesService::generateFile()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
rs 9.6666
cc 2
eloc 7
nc 1
nop 2
1
<?php
2
3
4
namespace OCA\CMSPico\Service;
5
6
use DirectoryIterator;
7
use Exception;
8
use OCA\CMSPico\Exceptions\WriteAccessException;
9
use OCA\CMSPico\Model\TemplateFile;
10
use OCA\CMSPico\Model\Website;
11
use OCP\Files\Folder;
0 ignored issues
show
Bug introduced by
The type OCP\Files\Folder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use OCP\IL10N;
0 ignored issues
show
Bug introduced by
The type OCP\IL10N was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
class TemplatesService {
15
16
	const TEMPLATE_DEFAULT = 'sample_pico';
17
	const TEMPLATE_DIR = __DIR__ . '/../../templates/';
18
19
	/** @var IL10N */
20
	private $l10n;
21
22
	/** @var MiscService */
23
	private $miscService;
24
25
	/** @var Folder */
26
	private $websiteFolder;
27
28
	/**
29
	 * TemplatesService constructor.
30
	 *
31
	 * @param IL10N $l10n
32
	 * @param MiscService $miscService
33
	 */
34
	function __construct(IL10N $l10n, MiscService $miscService) {
35
		$this->l10n = $l10n;
36
		$this->miscService = $miscService;
37
	}
38
39
40
	/**
41
	 * @param Website $website
42
	 */
43
	public function installTemplates(Website $website) {
44
45
		$files = $this->getSourceFiles(self::TEMPLATE_DIR . $website->getTemplateSource() . '/');
46
47
		$this->initWebsiteFolder($website);
48
		$data = $this->generateData($website);
49
		foreach ($files as $file) {
50
			$file->applyData($data);
51
			$this->generateFile($file, $website);
52
		}
53
	}
54
55
56
	/**
57
	 * @param $base
58
	 * @param string $dir
59
	 *
60
	 * @return TemplateFile[]
61
	 */
62
	private function getSourceFiles($base, $dir = '') {
63
64
		MiscService::endSlash($base);
65
		MiscService::endSlash($dir);
66
67
		$files = [];
68
		foreach (new DirectoryIterator($base . $dir) as $file) {
69
70
			if (substr($file->getFilename(), 0, 1) === '.') {
71
				continue;
72
			}
73
74
			if ($file->isDir()) {
75
				$files = array_merge($files, $this->getSourceFiles($base, $dir . $file->getFilename()));
76
				continue;
77
			}
78
79
			$files[] = new TemplateFile($base, $dir . $file->getFilename());
80
		}
81
82
		return $files;
83
	}
84
85
86
	/**
87
	 * @param TemplateFile $file
88
	 * @param Website $website
89
	 *
90
	 * @throws WriteAccessException
91
	 */
92
	private function generateFile(TemplateFile $file, Website $website) {
93
		try {
94
			$this->initFolder(pathinfo($website->getPath() . $file->getFileName(), PATHINFO_DIRNAME));
95
96
			$new = $this->websiteFolder->newFile($website->getPath() . $file->getFileName());
97
			$new->putContent($file->getContent());
98
		} catch (Exception $e) {
99
			throw new WriteAccessException(
100
				$this->l10n->t('Cannot generate template file in this folder')
101
			);
102
		}
103
104
	}
105
106
107
	/**
108
	 * @param Website $website
109
	 */
110
	private function initWebsiteFolder(Website $website) {
111
		$this->websiteFolder = \OC::$server->getUserFolder($website->getUserId());
112
		$this->initFolder($website->getPath());
113
	}
114
115
116
	/**
117
	 * @param Website $website
118
	 *
119
	 * @return array
120
	 */
121
	private function generateData(Website $website) {
122
		return [
123
			'site_title' => $website->getName(),
124
			'base_url'   => \OC::$WEBROOT . $website->getSite()
125
		];
126
	}
127
128
	private function initFolder($path) {
129
130
		if (!$this->websiteFolder->nodeExists($path)) {
131
			$this->websiteFolder->newFolder($path);
132
		}
133
	}
134
135
136
}