Database   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 25
dl 0
loc 57
ccs 0
cts 26
cp 0
rs 10
c 1
b 0
f 1
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A tryDatabase() 0 17 4
A getMediaFiles() 0 21 3
A tryFilesystem() 0 4 1
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
        $model = $this->getCollection()->getRecord();
25
26
        foreach ($files as $file) {
27
            MediaModels::records()->createFor(
28
                $file,
29
                $model,
0 ignored issues
show
Bug introduced by
It seems like $model 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

29
                /** @scrutinizer ignore-type */ $model,
Loading history...
30
                $this->getCollection()
31
            );
32
        }
33
34
        $model->mediaProperties($this->getCollection())->saveDbLoaded(true);
0 ignored issues
show
Bug introduced by
The method saveDbLoaded() does not exist on Nip\Records\Collections\Collection. ( Ignorable by Annotation )

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

34
        $model->mediaProperties($this->getCollection())->/** @scrutinizer ignore-call */ saveDbLoaded(true);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
35
36
        return $files;
37
    }
38
39
    /**
40
     * @return array|false
41
     */
42
    protected function tryDatabase()
43
    {
44
        $propertiesRecord = MediaModels::properties()->forCollection($this->getCollection());
45
46
        if (!is_object($propertiesRecord) || $propertiesRecord->dbLoaded() === false) {
47
            return false;
48
        }
49
50
        $mediaRecords = MediaModels::records()->for(
51
            $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

51
            /** @scrutinizer ignore-type */ $this->getCollection()->getRecord(),
Loading history...
52
            $this->getCollection()->getName()
53
        );
54
        $files = [];
55
        foreach ($mediaRecords as $mediaRecord) {
56
            $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...
57
        }
58
        return $files;
59
    }
60
61
    /**
62
     * @return array|false
63
     */
64
    protected function tryFilesystem()
65
    {
66
        $loader = $this->initFromSibling(Filesystem::class);
67
        return $loader->getMediaFiles();
68
    }
69
}
70