Test Failed
Push — master ( c9f7be...859573 )
by Gabriel
06:41
created

Database::getMediaFiles()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 15
c 1
b 0
f 1
nc 3
nop 0
dl 0
loc 24
rs 9.7666
1
<?php
2
3
namespace ByTIC\MediaLibrary\Loaders;
4
5
use ByTIC\MediaLibrary\Support\MediaModels;
6
use Nip\Filesystem\File;
7
8
/**
9
 * Class Database
10
 */
11
class Database extends AbstractLoader
12
{
13
    /**
14
     * @return File[]
15
     */
16
    public function getMediaFiles()
17
    {
18
        $files = $this->tryDatabase();
19
        if (is_array($files)) {
20
            return $files;
21
        }
22
        $files = $this->tryFilesystem();
23
24
        foreach ($files as $file) {
25
            MediaModels::records()->createFor(
26
                $file,
27
                $this->getCollection()->getRecord(),
0 ignored issues
show
Bug introduced by
It seems like $this->getCollection()->getRecord() can also be of type ByTIC\MediaLibrary\HasMedia\HasMediaTrait; however, parameter $model of ByTIC\MediaLibrary\Model...diaRecords::createFor() does only seem to accept Nip\Records\AbstractModels\Record, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

27
                /** @scrutinizer ignore-type */ $this->getCollection()->getRecord(),
Loading history...
28
                $this->getCollection()->getName()
29
            );
30
        }
31
32
        $propertiesRecord = MediaModels::properties()->createFor(
33
            $this->getCollection()->getRecord(),
0 ignored issues
show
Bug introduced by
It seems like $this->getCollection()->getRecord() can also be of type ByTIC\MediaLibrary\HasMedia\HasMediaTrait; however, parameter $model of ByTIC\MediaLibrary\Model...Properties::createFor() does only seem to accept Nip\Records\AbstractModels\Record, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
            /** @scrutinizer ignore-type */ $this->getCollection()->getRecord(),
Loading history...
34
            $this->getCollection()->getName()
35
        );
36
        $propertiesRecord->dbLoaded(true);
37
        $propertiesRecord->save();
38
39
        return $files;
40
    }
41
42
    /**
43
     * @return array|false
44
     */
45
    protected function tryDatabase()
46
    {
47
        $propertiesRecord = MediaModels::properties()->for(
48
            $this->getCollection()->getRecord(),
0 ignored issues
show
Bug introduced by
It seems like $this->getCollection()->getRecord() can also be of type ByTIC\MediaLibrary\HasMedia\HasMediaTrait; however, parameter $model of ByTIC\MediaLibrary\Model...\MediaProperties::for() does only seem to accept Nip\Records\AbstractModels\Record, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
            /** @scrutinizer ignore-type */ $this->getCollection()->getRecord(),
Loading history...
49
            $this->getCollection()->getName()
50
        );
51
        if (!is_object($propertiesRecord) || $propertiesRecord->dbLoaded() === false) {
52
            return false;
53
        }
54
55
        $mediaRecords = MediaModels::records()->for(
56
            $this->getCollection()->getRecord(),
0 ignored issues
show
Bug introduced by
It seems like $this->getCollection()->getRecord() can also be of type ByTIC\MediaLibrary\HasMedia\HasMediaTrait; however, parameter $model of ByTIC\MediaLibrary\Model...rds\MediaRecords::for() does only seem to accept Nip\Records\AbstractModels\Record, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
            /** @scrutinizer ignore-type */ $this->getCollection()->getRecord(),
Loading history...
57
            $this->getCollection()->getName()
58
        );
59
        $files = [];
60
        foreach ($mediaRecords as $mediaRecord) {
61
            $files[] = new File($this->getFilesystem(),$mediaRecord->path);
0 ignored issues
show
Bug Best Practice introduced by
The property path does not exist on ByTIC\MediaLibrary\Model...roperties\MediaProperty. Since you implemented __get, consider adding a @property annotation.
Loading history...
62
        }
63
        return $files;
64
    }
65
66
    /**
67
     * @return array|false
68
     */
69
    protected function tryFilesystem()
70
    {
71
        $loader = $this->initFromSibling(Filesystem::class);
72
        return $loader->getMediaFiles();
73
    }
74
}
75