Passed
Push — master ( 2dc8db...8c7204 )
by Simon
02:29
created

StorageRepositoryUtility::removeLocalStorage()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 8
Ratio 53.33 %

Importance

Changes 0
Metric Value
dl 8
loc 15
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 3
nop 2
1
<?php
2
3
/*
4
 * This file is part of the "GS Library" Extension for TYPO3 CMS.
5
 *
6
 * Copyright (C) 2017 by Gilbertsoft (gilbertsoft.org)
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * For the full license information, please read the LICENSE file that
19
 * was distributed with this source code.
20
 *
21
 * The TYPO3 project - inspiring people to share!
22
 */
23
24
namespace Gilbertsoft\Lib\Utility;
25
26
27
/**
28
 * Use declarations
29
 */
30
use TYPO3\CMS\Core\Messaging\FlashMessage;
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
33
34
/**
35
 * GS Storage Repository Utility class.
36
 */
37
class StorageRepositoryUtility
38
{
39
	/**
40
	 * @const string Suffix added to storage name
41
	 */
42
	const STORAGE_SUFFIX = '/ (auto-created)';
43
44
	/**
45
	 * Get the storage repository
46
	 *
47
	 * @return \TYPO3\CMS\Core\Resource\StorageRepository
48
	 */
49
	public static function getStorageRepository()
50
	{
51
		return GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\StorageRepository::class);
52
	}
53
54
	/**
55
	 * Searches for a local storage.
56
	 *
57
	 * @param string $name Local storage name
58
	 * @return NULL|\TYPO3\CMS\Core\Resource\ResourceStorage
59
	 */
60
	public static function findLocalStorage($name)
61
	{
62
		/** @var $storageObjects \TYPO3\CMS\Core\Resource\ResourceStorage[] */
63
		$storageObjects = self::getStorageRepository()->findByStorageType('Local');
64
65
		foreach ($storageObjects as $storage) {
66
			if (isset($storage->getConfiguration()['basePath']) && ($storage->getConfiguration()['basePath'] == rtrim($name, '/') . '/')) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 130 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
67
				return $storage;
68
			}
69
		}
70
71
		return null;
72
	}
73
74
	/**
75
	 * Creates a directory in the web root if it is not existing.
76
	 *
77
	 * @param string $name Relative path to folder from web root, see PHP mkdir() function. Removes trailing slash internally.
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 123 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
78
	 * @return bool TRUE if mkdir went well!
79
	 */
80
	public static function createDirectoryAtWebRoot($name)
81
	{
82
		if (!@is_dir(PATH_site . $name)) {
83
			return GeneralUtility::mkdir(PATH_site . $name);
84
		}
85
86
		return true;
87
	}
88
89
	/**
90
	 * Creates a local storage if not exists.
91
	 *
92
	 * @param string $name Local storage name
93
	 * @return NULL|int Uid of the inserted or found record
94
	 * @throws \InvalidArgumentException
95
	 */
96
	public static function createLocalStorage($extensionKey, $name, $message = '')
97
	{
98
		if (!is_string($name) || empty($name)) {
99
			throw new \InvalidArgumentException('$name must be a non empty string.', 1491681665);
100
		}
101
102 View Code Duplication
		if (self::createDirectoryAtWebRoot($name) !== true) {
103
			FlashMessageUtility::showFlashMessage(
104
				$extensionKey,
105
				'Local storage ' . $name . ' could not be created!',
106
				'Local storage not created',
107
				FlashMessage::WARNING
108
			);
109
110
			return null;
111
		}
112
113
		$message = (is_string($message) && !empty($message) ? ' ' . $message : '');
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
114
		/** @var $storage \TYPO3\CMS\Core\Resource\ResourceStorage */
115
		$storage = self::findLocalStorage($name);
116
117
		if ($storage !== null) {
118
			FlashMessageUtility::showFlashMessage(
119
				$extensionKey,
120
				'Local storage ' . $name . ' was found.' . $message,
121
				'Local storage found',
122
				FlashMessage::NOTICE
123
			);
124
125
			return $storage->getUid();
126
		}
127
128
		$uid = self::getStorageRepository()->createLocalStorage(
129
			$name . self::STORAGE_SUFFIX,
130
			$name,
131
			'relative',
132
			'This is the local ' . $name . '/ directory. This storage mount has been created automatically by ' . $extensionKey . '.' . $message,
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 136 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
133
			false
134
		);
135
136
		FlashMessageUtility::showFlashMessage(
137
			$extensionKey,
138
			'Local storage ' . $name . ' successfully created.' . $message,
139
			'Local storage created'
140
		);
141
142
		return $uid;
143
	}
144
145
	/**
146
	 * Removes a local storage.
147
	 *
148
	 * @param string $name Local storage name
149
	 * @return void
150
	 * @throws \InvalidArgumentException
151
	 */
152
	public static function removeLocalStorage($extensionKey, $name)
153
	{
154
		if (!is_string($name) || empty($name)) {
155
			throw new \InvalidArgumentException('$name must be a non empty string.', 1491682406);
156
		}
157
158 View Code Duplication
		if (self::findLocalStorage($name) !== null) {
159
			FlashMessageUtility::showFlashMessage(
160
				$extensionKey,
161
				'Local storage ' . $name . ' must be removed by an admin from the root line in the backend and web root directory.',
162
				'Remove local storage',
163
				FlashMessage::NOTICE
164
			);
165
		}
166
	}
167
}