Passed
Push — master ( 06d79a...940106 )
by Simon
02:49
created

StorageRepositoryUtility::findLocalStorage()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 7
nc 3
nop 1
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
		if (isset($storageObjects)) {
66
			foreach ($storageObjects as $storage) {
67
				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 131 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...
68
					return $storage;
69
				}
70
			}
71
		}
72
73
		return null;
74
	}
75
76
	/**
77
	 * Creates a directory in the web root if it is not existing.
78
	 *
79
	 * @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...
80
	 * @return bool TRUE if mkdir went well!
81
	 */
82
	public static function createDirectoryAtWebRoot($name)
83
	{
84
		if (!@is_dir(PATH_site . $name)) {
85
			return GeneralUtility::mkdir(PATH_site . $name);
86
		}
87
88
		return true;
89
	}
90
91
	/**
92
	 * Creates a local storage if not exists.
93
	 *
94
	 * @param string $name Local storage name
95
	 * @return NULL|int Uid of the inserted or found record
96
	 * @throws \InvalidArgumentException
97
	 */
98
	public static function createLocalStorage($extensionKey, $name, $message = '')
99
	{
100
		if (!is_string($extensionKey) || empty($extensionKey)) {
101
			throw new \InvalidArgumentException('$extensionKey must be a non empty string.', 1491580810);
102
		}
103
104
		if (!is_string($name) || empty($name)) {
105
			throw new \InvalidArgumentException('$name must be a non empty string.', 1491681665);
106
		}
107
108 View Code Duplication
		if (self::createDirectoryAtWebRoot($name) !== true) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
			FlashMessageUtility::showFlashMessage(
110
				$extensionKey,
111
				'Local storage ' . $name . ' could not be created!',
112
				'Local storage not created',
113
				FlashMessage::WARNING
114
			);
115
116
			return null;
117
		}
118
119
		/** @var $storage \TYPO3\CMS\Core\Resource\ResourceStorage */
120
		$storage = self::findLocalStorage($name);
121
122
		if ($storage !== null) {
123
			FlashMessageUtility::showFlashMessage(
124
				$extensionKey,
125
				'Local storage ' . $name . ' was found.' . (is_string($message) && !empty($message) ? ' ' . $message : ''),
126
				'Local storage found',
127
				FlashMessage::NOTICE
128
			);
129
130
			return $storage->getUid();
131
		}
132
133
		$uid = self::getStorageRepository()->createLocalStorage(
134
			$name . self::STORAGE_SUFFIX,
135
			$name,
136
			'relative',
137
			'This is the local ' . $name . '/ directory. This storage mount has been created automatically by ' . $extensionKey . '.' . (is_string($message) && !empty($message) ? ' ' . $message : ''),
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 191 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...
138
			false
139
		);
140
141
		FlashMessageUtility::showFlashMessage(
142
			$extensionKey,
143
			'Local storage ' . $name . ' successfully created.' . (is_string($message) && !empty($message) ? ' ' . $message : ''),
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 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...
144
			'Local storage created'
145
		);
146
147
		return $uid;
148
	}
149
150
	/**
151
	 * Removes a local storage.
152
	 *
153
	 * @param string $name Local storage name
154
	 * @return void
155
	 * @throws \InvalidArgumentException
156
	 */
157
	public static function removeLocalStorage($extensionKey, $name)
158
	{
159
		if (!is_string($extensionKey) || empty($extensionKey)) {
160
			throw new \InvalidArgumentException('$extensionKey must be a non empty string.', 1491580839);
161
		}
162
163
		if (!is_string($name) || empty($name)) {
164
			throw new \InvalidArgumentException('$name must be a non empty string.', 1491682406);
165
		}
166
167 View Code Duplication
		if (self::findLocalStorage($name) !== null) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
168
			FlashMessageUtility::showFlashMessage(
169
				$extensionKey,
170
				'Local storage ' . $name . ' must be removed by an admin from the root line in the backend and web root directory.',
171
				'Remove local storage',
172
				FlashMessage::NOTICE
173
			);
174
		}
175
	}
176
}