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

HasFilesystemTrait::getBasePathForMedia()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 0
dl 0
loc 15
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ByTIC\MediaLibrary\Collections\Traits;
4
5
use ByTIC\MediaLibrary\PathGenerator\PathGeneratorFactory;
6
use Nip\Filesystem\FileDisk;
7
8
/**
9
 * Trait HasFilesystemTrait.
10
 */
11
trait HasFilesystemTrait
12
{
13
    protected $filesystem;
14
15
    /**
16
     * @return FileDisk
17
     */
18
    public function getFilesystem()
19
    {
20
        if ($this->filesystem == null) {
21
            $this->initFilesystem();
22
        }
23
24
        return $this->filesystem;
25
    }
26
27
    /**
28
     * @param mixed $filesystem
29
     */
30
    public function setFilesystem($filesystem)
31
    {
32
        $this->filesystem = $filesystem;
33
    }
34
35
    /**
36
     * @return mixed
37
     */
38
    public function getBasePathForMedia()
39
    {
40
        $method = 'getBasePathForMedia';
41
        $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

41
        $manager = $this->/** @scrutinizer ignore-call */ getRecord()->getManager();
Loading history...
42
43
        $media = $this->newMedia();
0 ignored issues
show
Bug introduced by
It seems like newMedia() 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

43
        /** @scrutinizer ignore-call */ 
44
        $media = $this->newMedia();
Loading history...
44
45
        if (method_exists($manager, $method)) {
46
            $path = $manager->$method($media);
47
            if (!empty($path)) {
48
                return $path;
49
            }
50
        }
51
52
        return PathGeneratorFactory::create()::$method($media);
53
    }
54
55
    protected function initFilesystem()
56
    {
57
        $this->setFilesystem($this->generateFilesystem());
58
    }
59
60
    /**
61
     * @return FileDisk
62
     */
63
    protected function generateFilesystem()
64
    {
65
        return $this->getMediaRepository()->getRecord()->getMediaFilesystemDisk();
0 ignored issues
show
Bug introduced by
It seems like getMediaRepository() 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

65
        return $this->/** @scrutinizer ignore-call */ getMediaRepository()->getRecord()->getMediaFilesystemDisk();
Loading history...
66
    }
67
}
68