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

FileRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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