Passed
Pull Request — 2.x (#704)
by Talv
08:50
created

FileRepository   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 68.42%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 53
ccs 13
cts 19
cp 0.6842
rs 10
c 1
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A afterDelete() 0 5 2
A filter() 0 4 1
A __construct() 0 3 1
A prepareFieldsBeforeCreate() 0 14 3
1
<?php
2
3
namespace A17\Twill\Repositories;
4
5
use A17\Twill\Models\File;
6
use A17\Twill\Repositories\Behaviors\HandleTags;
7
use Illuminate\Support\Facades\Config;
8
use Illuminate\Support\Facades\Storage;
9
10
class FileRepository extends ModuleRepository
11
{
12
    use HandleTags;
0 ignored issues
show
introduced by
The trait A17\Twill\Repositories\Behaviors\HandleTags requires some properties which are not provided by A17\Twill\Repositories\FileRepository: $name, $id
Loading history...
13
14
    /**
15
     * @param File $model
16
     */
17 4
    public function __construct(File $model)
18
    {
19 4
        $this->model = $model;
20 4
    }
21
22
    /**
23
     * @param \Illuminate\Database\Query\Builder $query
24
     * @param array $scopes
25
     * @return \Illuminate\Database\Query\Builder
26
     */
27 2
    public function filter($query, array $scopes = [])
28
    {
29 2
        $this->searchIn($query, $scopes, 'search', ['filename']);
30 2
        return parent::filter($query, $scopes);
31
    }
32
33
    /**
34
     * @param A17\Twill\Models\File $object
0 ignored issues
show
Bug introduced by
The type A17\Twill\Repositories\A17\Twill\Models\File was not found. Did you mean A17\Twill\Models\File? If so, make sure to prefix the type with \.
Loading history...
35
     * @return void
36
     */
37
    public function afterDelete($object)
38
    {
39
        $storageId = $object->uuid;
40
        if (Config::get('twill.file_library.cascade_delete')) {
41
            Storage::disk(Config::get('twill.file_library.disk'))->delete($storageId);
42
        }
43
    }
44
45
    /**
46
     * @param array $fields
47
     * @return array
48
     */
49 3
    public function prepareFieldsBeforeCreate($fields)
50
    {
51
            // if a disk has a root other than / set it'll be re-applied when calculating the file path so we need to strip it here...
52 3
            $diskName = config('twill.file_library.disk');
53 3
            $diskConfig = config('filesystems.disks.'.$diskName);
54 3
            $diskRoot = $diskConfig['root'] ?? false;
55
56 3
            if($diskConfig['driver'] === 's3' && $diskRoot){
57
                $fields['uuid'] = ltrim($fields['uuid'], $diskRoot);
58
            }
59
60 3
            $fields['size'] = Storage::disk($diskName)->size($fields['uuid']);
61
62 3
        return $fields;
63
    }
64
}
65