LoadMediaTrait   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Test Coverage

Coverage 70.83%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
eloc 34
dl 0
loc 131
ccs 34
cts 48
cp 0.7083
rs 10
c 2
b 1
f 1
wmc 15

11 Methods

Rating   Name   Duplication   Size   Complexity  
A doLoad() 0 3 1
A setMediaType() 0 3 1
A loadMedia() 0 9 2
A setMediaLoaded() 0 3 1
A appendMedia() 0 3 1
A appendMediaFromLoader() 0 14 3
A isMediaLoaded() 0 3 1
A getMediaType() 0 3 1
A hydrateLoader() 0 6 1
A newMedia() 0 7 1
A getLoaderClass() 0 7 2
1
<?php
2
3
namespace ByTIC\MediaLibrary\Collections\Traits;
4
5
use ByTIC\MediaLibrary\Loaders\AbstractLoader;
6
use ByTIC\MediaLibrary\Loaders\Database;
7
use ByTIC\MediaLibrary\Loaders\Fallback;
0 ignored issues
show
Bug introduced by
The type ByTIC\MediaLibrary\Loaders\Fallback was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use ByTIC\MediaLibrary\Loaders\Filesystem;
9
use ByTIC\MediaLibrary\Loaders\HasLoaderTrait;
10
use ByTIC\MediaLibrary\Media\Media;
11
use ByTIC\MediaLibrary\MediaRepository\HasMediaRepositoryTrait;
12
13
/**
14
 * Trait LoadMediaTrait.
15
 */
16
trait LoadMediaTrait
17
{
18 1
    use HasLoaderTrait;
19 1
    use HasMediaRepositoryTrait;
20
21
    /**
22
     * @var string
23
     */
24
    protected $mediaType = 'files';
25
26
    /**
27
     * @var bool
28
     */
29
    protected $mediaLoaded = false;
30
31 7
    protected function doLoad(): void
32
    {
33 7
        $this->loadMedia();
34 7
    }
35
36 7
    public function loadMedia()
37
    {
38 7
        if ($this->isMediaLoaded()) {
39
            return;
40
        }
41
42 7
        $this->getLoader()->loadMedia();
43
44 7
        $this->setMediaLoaded(true);
45 7
    }
46
47
    /**
48
     * @return bool
49
     */
50 7
    public function isMediaLoaded(): bool
51
    {
52 7
        return $this->mediaLoaded;
53
    }
54
55
    /**
56
     * @param bool $mediaLoaded
57
     */
58 7
    public function setMediaLoaded(bool $mediaLoaded)
59
    {
60 7
        $this->mediaLoaded = $mediaLoaded;
61 7
    }
62
63
    /**
64
     * @return Media
65
     */
66 10
    public function newMedia()
67
    {
68 10
        $mediaFile = new Media();
69 10
        $mediaFile->setCollection($this);
70 10
        $mediaFile->setModel($this->getMediaRepository()->getRecord());
71
72 10
        return $mediaFile;
73
    }
74
75
    /**
76
     * Append a media object inside the collection.
77
     *
78
     * @param Media          $media
79
     * @param AbstractLoader $loader
80
     */
81 5
    public function appendMediaFromLoader(Media $media, $loader)
82
    {
83 5
        $method = 'validateMediaFromLoader';
84 5
        $manager = $this->getRecord()->getManager();
0 ignored issues
show
Bug introduced by
It seems like getRecord() 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

84
        $manager = $this->/** @scrutinizer ignore-call */ getRecord()->getManager();
Loading history...
85 5
        if (!method_exists($manager, $method)) {
86 5
            $this->appendMedia($media);
87
88 5
            return;
89
        }
90
91
        if ($manager->$method($media, $loader)) {
92
            $this->appendMedia($media);
93
94
            return;
95
        }
96
    }
97
98
    /**
99
     * Append a media object inside the collection.
100
     *
101
     * @param Media $media
102
     */
103 5
    public function appendMedia(Media $media)
104
    {
105 5
        $this->items[$media->getName()] = $media;
0 ignored issues
show
Bug Best Practice introduced by
The property items does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
106 5
    }
107
108
    /**
109
     * @return mixed
110
     */
111 12
    public function getMediaType()
112
    {
113 12
        return $this->mediaType;
114
    }
115
116
    /**
117
     * @param mixed $mediaType
118
     */
119 14
    public function setMediaType($mediaType)
120
    {
121 14
        $this->mediaType = $mediaType;
122 14
    }
123
124
    /**
125
     * @param AbstractLoader $loader
126
     *
127
     * @return AbstractLoader
128
     */
129
    protected function hydrateLoader($loader)
130
    {
131
        $loader->setCollection($this);
132
        $loader->setFilesystem($this->getFilesystem());
0 ignored issues
show
Bug introduced by
It seems like getFilesystem() 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

132
        $loader->setFilesystem($this->/** @scrutinizer ignore-call */ getFilesystem());
Loading history...
133
134
        return $loader;
135
    }
136
137
    /**
138
     * @return mixed
139
     */
140
    protected function getLoaderClass()
141
    {
142
        $mediaLoader = config()->get('media-library.media_loader');
143
        if (class_exists($mediaLoader)) {
144
            return $mediaLoader;
145
        }
146
        return Database::class;
147
    }
148
}
149