Completed
Push — master ( cd75c7...9422a0 )
by Gabriel
02:56 queued 11s
created

LoadMediaTrait::appendMediaFromLoader()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 2
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ByTIC\MediaLibrary\Collections\Traits;
4
5
use ByTIC\MediaLibrary\Loaders\AbstractLoader;
6
use ByTIC\MediaLibrary\Loaders\Filesystem;
7
use ByTIC\MediaLibrary\Loaders\HasLoaderTrait;
8
use ByTIC\MediaLibrary\Media\Media;
9
use ByTIC\MediaLibrary\MediaRepository\HasMediaRepositoryTrait;
10
11
/**
12
 * Trait LoadMediaTrait.
13
 */
14
trait LoadMediaTrait
15
{
16
    use HasLoaderTrait;
17
    use HasMediaRepositoryTrait;
18
19
    /**
20
     * @var string
21
     */
22
    protected $mediaType = 'files';
23
24
    /**
25
     * @var bool
26
     */
27
    protected $mediaLoaded = false;
28
29
    public function loadMedia()
30
    {
31
        if ($this->isMediaLoaded()) {
32
            return;
33
        }
34
35
        $this->getLoader()->loadMedia();
36
37
        $this->setMediaLoaded(true);
38
    }
39
40
    /**
41
     * @return bool
42
     */
43
    public function isMediaLoaded(): bool
44
    {
45
        return $this->mediaLoaded;
46
    }
47
48
    /**
49
     * @param bool $mediaLoaded
50
     */
51
    public function setMediaLoaded(bool $mediaLoaded)
52
    {
53
        $this->mediaLoaded = $mediaLoaded;
54
    }
55
56
    /**
57
     * @return Media
58
     */
59
    public function newMedia()
60
    {
61
        $mediaFile = new Media();
62
        $mediaFile->setCollection($this);
63
        $mediaFile->setModel($this->getMediaRepository()->getRecord());
64
65
        return $mediaFile;
66
    }
67
68
    /**
69
     * Append a media object inside the collection.
70
     *
71
     * @param Media $media
72
     * @param AbstractLoader $loader
73
     */
74
    public function appendMediaFromLoader(Media $media, $loader)
75
    {
76
        $method = 'validateMediaFromLoader';
77
        $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

77
        $manager = $this->/** @scrutinizer ignore-call */ getRecord()->getManager();
Loading history...
78
        if (!method_exists($manager, $method)) {
79
            $this->appendMedia($media);
80
            return;
81
        }
82
83
        if ($manager->$method($media, $loader)) {
84
            $this->appendMedia($media);
85
            return;
86
        }
87
    }
88
89
    /**
90
     * Append a media object inside the collection.
91
     *
92
     * @param Media $media
93
     */
94
    public function appendMedia(Media $media)
95
    {
96
        $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...
97
    }
98
99
    /**
100
     * @return mixed
101
     */
102
    public function getMediaType()
103
    {
104
        return $this->mediaType;
105
    }
106
107
    /**
108
     * @param mixed $mediaType
109
     */
110
    public function setMediaType($mediaType)
111
    {
112
        $this->mediaType = $mediaType;
113
    }
114
115
    /**
116
     * @param AbstractLoader $loader
117
     *
118
     * @return AbstractLoader
119
     */
120
    protected function hydrateLoader($loader)
121
    {
122
        $loader->setCollection($this);
123
        $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

123
        $loader->setFilesystem($this->/** @scrutinizer ignore-call */ getFilesystem());
Loading history...
124
125
        return $loader;
126
    }
127
128
    /**
129
     * @return mixed
130
     */
131
    protected function getLoaderClass()
132
    {
133
        return Filesystem::class;
134
    }
135
}
136