Passed
Pull Request — 2.x (#727)
by Bekzat
16:53
created

HasFiles   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 18.18%

Importance

Changes 0
Metric Value
eloc 26
c 0
b 0
f 0
dl 0
loc 62
ccs 6
cts 33
cp 0.1818
rs 10
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A files() 0 7 1
A file() 0 12 3
A fileObject() 0 3 1
A findFile() 0 15 5
A filesList() 0 15 3
1
<?php
2
3
namespace A17\Twill\Models\Behaviors;
4
5
use A17\Twill\Models\File;
6
use A17\Twill\Services\FileLibrary\FileService;
7
8
trait HasFiles
9
{
10 17
    public function files()
11
    {
12 17
        return $this->morphToMany(
0 ignored issues
show
Bug introduced by
It seems like morphToMany() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

12
        return $this->/** @scrutinizer ignore-call */ morphToMany(
Loading history...
13 17
            File::class,
14 17
            'fileable',
15 17
            config('twill.fileables_table', 'twill_fileables')
16 17
        )->withPivot(['role', 'locale'])->withTimestamps();
17
    }
18
19
    private function findFile($role, $locale)
20
    {
21
        $locale = $locale ?? app()->getLocale();
0 ignored issues
show
introduced by
The method getLocale() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

21
        $locale = $locale ?? app()->/** @scrutinizer ignore-call */ getLocale();
Loading history...
22
23
        $file = $this->files->first(function ($file) use ($role, $locale) {
24
            return $file->pivot->role === $role && $file->pivot->locale === $locale;
25
        });
26
27
        if (!$file && config('translatable.use_property_fallback', false)) {
28
            $file = $this->files->first(function ($file) use ($role) {
29
                return $file->pivot->role === $role && $file->pivot->locale === config('translatable.fallback_locale');
30
            });
31
        }
32
33
        return $file;
34
    }
35
36
    public function file($role, $locale = null, $file = null)
37
    {
38
39
        if (!$file) {
40
            $file = $this->findFile($role, $locale);
41
        }
42
43
        if ($file) {
44
            return FileService::getUrl($file->uuid);
0 ignored issues
show
Bug introduced by
The method getUrl() does not exist on A17\Twill\Services\FileLibrary\FileService. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
            return FileService::/** @scrutinizer ignore-call */ getUrl($file->uuid);
Loading history...
45
        }
46
47
        return null;
48
    }
49
50
    public function filesList($role, $locale = null)
51
    {
52
        $locale = $locale ?? app()->getLocale();
53
54
        $files = $this->files->filter(function ($file) use ($role, $locale) {
55
            return $file->pivot->role === $role && $file->pivot->locale === $locale;
56
        });
57
58
        $urls = [];
59
60
        foreach ($files as $file) {
61
            $urls[] = $this->file($role, $locale, $file);
62
        }
63
64
        return $urls;
65
    }
66
67
    public function fileObject($role, $locale = null)
68
    {
69
        return $this->findFile($role, $locale);
70
    }
71
72
}
73