Passed
Push — master ( c6f9a3...c9f7be )
by Gabriel
03:32
created

LoadMediaTrait::doLoad()   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 0
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 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 7
    protected function doLoad(): void
30
    {
31 7
        $this->loadMedia();
32 7
    }
33
34 7
    public function loadMedia()
35
    {
36 7
        if ($this->isMediaLoaded()) {
37
            return;
38
        }
39
40 7
        $this->getLoader()->loadMedia();
41
42 7
        $this->setMediaLoaded(true);
43 7
    }
44
45
    /**
46
     * @return bool
47
     */
48 7
    public function isMediaLoaded(): bool
49
    {
50 7
        return $this->mediaLoaded;
51
    }
52
53
    /**
54
     * @param bool $mediaLoaded
55
     */
56 7
    public function setMediaLoaded(bool $mediaLoaded)
57
    {
58 7
        $this->mediaLoaded = $mediaLoaded;
59 7
    }
60
61
    /**
62
     * @return Media
63
     */
64 8
    public function newMedia()
65
    {
66 8
        $mediaFile = new Media();
67 8
        $mediaFile->setCollection($this);
68 8
        $mediaFile->setModel($this->getMediaRepository()->getRecord());
69
70 8
        return $mediaFile;
71
    }
72
73
    /**
74
     * Append a media object inside the collection.
75
     *
76
     * @param Media          $media
77
     * @param AbstractLoader $loader
78
     */
79 5
    public function appendMediaFromLoader(Media $media, $loader)
80
    {
81 5
        $method = 'validateMediaFromLoader';
82 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

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

130
        $loader->setFilesystem($this->/** @scrutinizer ignore-call */ getFilesystem());
Loading history...
131
132
        return $loader;
133
    }
134
135
    /**
136
     * @return mixed
137
     */
138 7
    protected function getLoaderClass()
139
    {
140 7
        return Filesystem::class;
141
    }
142
}
143