StorageRepositoryUtility::getStorageRepository()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace Gilbertsoft\Lib\Utility;
3
4
/*
5
 * This file is part of the "GS Library" Extension for TYPO3 CMS.
6
 *
7
 * Copyright (C) 2017 by Gilbertsoft (gilbertsoft.org)
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (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 General Public License for more details.
18
 *
19
 * For the full license information, please read the LICENSE file that
20
 * was distributed with this source code.
21
 *
22
 * The TYPO3 project - inspiring people to share!
23
 */
24
25
/**
26
 * Use declarations
27
 */
28
use TYPO3\CMS\Core\Core\Environment;
29
use TYPO3\CMS\Core\Messaging\FlashMessage;
30
use TYPO3\CMS\Core\Utility\GeneralUtility;
31
32
/**
33
 * GS Storage Repository Utility class.
34
 */
35
class StorageRepositoryUtility
36
{
37
    /**
38
     * @const string Suffix added to storage name
39
     */
40
    const STORAGE_SUFFIX = '/ (auto-created)';
41
42
    /**
43
     * Get the storage repository
44
     *
45
     * @return \TYPO3\CMS\Core\Resource\StorageRepository
46
     */
47
    public static function getStorageRepository()
48
    {
49
        return GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\StorageRepository::class);
50
    }
51
52
    /**
53
     * Searches for a local storage.
54
     *
55
     * @param string $name Local storage name
56
     * @return NULL|\TYPO3\CMS\Core\Resource\ResourceStorage
57
     */
58
    public static function findLocalStorage($name)
59
    {
60
        /** @var $storageObjects \TYPO3\CMS\Core\Resource\ResourceStorage[] */
61
        $storageObjects = self::getStorageRepository()->findByStorageType('Local');
62
63
        foreach ($storageObjects as $storage) {
64
            if (isset($storage->getConfiguration()['basePath']) && ($storage->getConfiguration()['basePath'] == rtrim($name, '/') . '/')) {
65
                return $storage;
66
            }
67
        }
68
69
        return null;
70
    }
71
72
    /**
73
     * Creates a directory in the web root if it is not existing.
74
     *
75
     * @param string $name Relative path to folder from web root, see PHP mkdir() function. Removes trailing slash internally.
76
     * @return bool TRUE if mkdir went well!
77
     */
78
    public static function createDirectoryAtWebRoot($name)
79
    {
80
        if (!@is_dir(Environment::getPublicPath() . '/' . $name)) {
81
            return GeneralUtility::mkdir(Environment::getPublicPath() . '/' . $name);
82
        }
83
84
        return true;
85
    }
86
87
    /**
88
     * Creates a local storage if not exists.
89
     *
90
     * @param string $extensionKey
91
     * @param string $name Local storage name
92
     * @param string $message Optional message
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
        $addMessage = (is_string($message) && !empty($message) ? ' ' . $message : '');
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.' . $addMessage,
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 . '.' . $addMessage,
133
            false
134
        );
135
136
        FlashMessageUtility::showFlashMessage(
137
            $extensionKey,
138
            'Local storage ' . $name . ' successfully created.' . $addMessage,
139
            'Local storage created'
140
        );
141
142
        return $uid;
143
    }
144
145
    /**
146
     * Removes a local storage.
147
     *
148
     * @param string $extensionKey
149
     * @param string $name Local storage name
150
     * @return void
151
     * @throws \InvalidArgumentException
152
     */
153
    public static function removeLocalStorage($extensionKey, $name)
154
    {
155
        if (!is_string($name) || empty($name)) {
156
            throw new \InvalidArgumentException('$name must be a non empty string.', 1491682406);
157
        }
158
159 View Code Duplication
        if (self::findLocalStorage($name) !== null) {
160
            FlashMessageUtility::showFlashMessage(
161
                $extensionKey,
162
                'Local storage ' . $name . ' must be removed by an admin from the root line in the backend and web root directory.',
163
                'Remove local storage',
164
                FlashMessage::NOTICE
165
            );
166
        }
167
    }
168
}
169