StorageService   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

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