Passed
Push — master ( 859573...a81ea2 )
by Gabriel
03:21
created

AbstractLoader::setCollection()   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 ByTIC\MediaLibrary\Loaders;
4
5
use ByTIC\MediaLibrary\Collections\Collection;
6
use ByTIC\MediaLibrary\Collections\Traits\LoadMediaTrait;
7
use Nip\Filesystem\File;
8
use Nip\Filesystem\FileDisk;
9
10
/**
11
 * Class AbstractLoader.
12
 */
13
abstract class AbstractLoader implements LoaderInterface
14
{
15
    /**
16
     * @var FileDisk
17
     */
18
    protected $filesystem;
19
20
    /**
21
     * @var Collection
22
     */
23
    protected $collection;
24
25
    /**
26
     * @return FileDisk
27
     */
28 7
    public function getFilesystem()
29
    {
30 7
        return $this->filesystem;
31
    }
32
33
    /**
34
     * @param FileDisk $filesystem
35
     */
36 7
    public function setFilesystem($filesystem)
37
    {
38 7
        $this->filesystem = $filesystem;
39 7
    }
40
41
    /**
42
     * Load media for a collection.
43
     */
44 7
    public function loadMedia()
45
    {
46 7
        $files = $this->getMediaFiles();
47
48 7
        foreach ($files as $file) {
49 5
            $mediaFile = $this->getCollection()->newMedia();
50 5
            $mediaFile->setFile($file);
51 5
            $this->appendMediaInCollection($mediaFile);
52
        }
53 7
    }
54
55
    /**
56
     * @return File[]
57
     */
58
    abstract public function getMediaFiles();
59
60
    /**
61
     * @return Collection
62
     */
63 7
    public function getCollection(): Collection
64
    {
65 7
        return $this->collection;
66
    }
67
68
    /**
69
     * @param Collection|LoadMediaTrait $collection
70
     */
71 7
    public function setCollection(Collection $collection)
72
    {
73 7
        $this->collection = $collection;
74 7
    }
75
76
    /**
77
     * @param string $loader
78
     * @return AbstractLoader
79
     */
80
    protected function initFromSibling($loader)
81
    {
82
        /** @var AbstractLoader $class */
83
        $loader = is_object($loader) ? $loader : new $loader();
0 ignored issues
show
introduced by
The condition is_object($loader) is always false.
Loading history...
84
        $loader->setFilesystem($this->getFilesystem());
85
        $loader->setCollection($this->getCollection());
86
87
        return $loader;
88
    }
89
90
    /**
91
     * @param $media
92
     */
93 5
    protected function appendMediaInCollection($media)
94
    {
95 5
        $this->getCollection()->appendMediaFromLoader($media, $this);
96 5
    }
97
98
    /**
99
     * @return string
100
     */
101 7
    public function getBasePath()
102
    {
103 7
        return $this->getCollection()->getBasePathForMedia();
104
    }
105
}
106