StorageService   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
eloc 9
c 2
b 0
f 0
dl 0
loc 29
rs 10
1
<?php
2
declare(strict_types=1);
3
/**
4
 * StorageService.php
5
 * Copyright (c) 2020 [email protected]
6
 *
7
 * This file is part of the Firefly III CSV importer
8
 * (https://github.com/firefly-iii/csv-importer).
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
22
 */
23
24
namespace App\Services\Storage;
25
26
use Illuminate\Contracts\Filesystem\FileNotFoundException;
27
use Storage;
28
use Str;
29
use UnexpectedValueException;
30
31
/**
32
 * Class StorageService
33
 */
34
class StorageService
35
{
36
    /**
37
     * @param string $content
38
     *
39
     * @return string
40
     */
41
    public static function storeContent(string $content): string
42
    {
43
        $fileName = Str::random(20);
44
        $disk     = Storage::disk('uploads');
45
        $disk->put($fileName, $content);
46
47
        return $fileName;
48
    }
49
50
    /**
51
     * @param string $name
52
     *
53
     * @throws FileNotFoundException
54
     * @return string
55
     */
56
    public static function getContent(string $name): string
57
    {
58
        $disk = Storage::disk('uploads');
59
        if ($disk->exists($name)) {
60
            return $disk->get($name);
61
        }
62
        throw new UnexpectedValueException(sprintf('No such file %s', $name));
63
    }
64
65
}
66