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

MediaRecords::for()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 3
eloc 11
c 2
b 1
f 1
nc 4
nop 2
dl 0
loc 17
ccs 0
cts 11
cp 0
crap 12
rs 9.9
1
<?php
2
3
namespace ByTIC\MediaLibrary\Models\MediaRecords;
4
5
use ByTIC\MediaLibrary\Media\Media;
6
use ByTIC\MediaLibrary\Media\Traits\FileMethodsTrait;
7
use ByTIC\MediaLibrary\Models\MediaProperties\MediaProperty;
8
use Nip\Records\AbstractModels\Record;
9
use Nip\Records\Collections\Collection;
10
use Nip\Records\RecordManager;
11
12
/**
13
 * Class MediaRecords
14
 * @package ByTIC\MediaLibrary\Models\MediaRecords
15
 *
16
 * @method MediaRecord getNew
17
 */
18
class MediaRecords extends RecordManager
19
{
20
21
    /**
22
     * @param Record $model
23
     * @param $collection
24
     * @return MediaProperty[]|Collection
25
     */
26
    public function for(Record $model, $collection)
27
    {
28
        $collection = is_object($collection) ? $collection->getName() : $collection;
29
30
        if ($model->hasRelation('Media')) {
31
            return $model->getRelation('Media')->getResults()->filter(
0 ignored issues
show
Bug introduced by
The method getResults() does not exist on Nip\Helpers\AbstractHelper. It seems like you code against a sub-type of Nip\Helpers\AbstractHelper such as Nip_Helper_Url or Nip\Helpers\View\Stylesheets or Nip\Helpers\View\Strings or Nip\Helpers\View\Paginator or Nip\Helpers\View\Arrays or Nip\Helpers\View\Icontext or Nip\Helpers\View\Color or Nip\Helpers\View\Scripts or Nip\Helpers\View\Url. ( Ignorable by Annotation )

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

31
            return $model->getRelation('Media')->/** @scrutinizer ignore-call */ getResults()->filter(
Loading history...
32
                function ($item) use ($collection) {
33
                    return $item->collection_name == $collection;
34
                }
35
            );
36
        }
37
38
        return $this->findByParams([
39
            'where' => [
40
                ['model =?', $model->getManager()->getMorphName()],
41
                ['model_id =?', $model->getPrimaryKey()],
42
                ['collection_name=?', $collection]
43
            ]
44
        ]);
45
    }
46
47
    /**
48
     * @param Media|FileMethodsTrait $media
49
     */
50
    public function deteleMedia(Media $media)
51
    {
52
        $model = $media->getModel();
53
        $this->deleteByParams([
54
            'where' => [
55
                ['model =?', $model->getManager()->getMorphName()],
0 ignored issues
show
Bug introduced by
It seems like getMorphName() 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

55
                ['model =?', $model->getManager()->/** @scrutinizer ignore-call */ getMorphName()],
Loading history...
56
                ['model_id =?', $model->getPrimaryKey()],
57
                ['collection_name=?', $media->getCollection()->getName()],
58
                ['path=?', $media->getPath()]
59
            ]
60
        ]);
61
    }
62
63
    /**
64
     * @param $file
65
     * @param Record $model
66
     * @param $collection
67
     * @return Record
68
     */
69
    public function createFor($file, Record $model, $collection)
70
    {
71
        $record = $this->getNew();
72
        $record->populateFromFile($file);
73
        $record->populateFromModel($model);
74
        $record->populateFromCollection($collection);
75
        $record->insert();
76
        return $record;
77
    }
78
79
    /**
80
     * @inheritDoc
81
     * @noinspection PhpMissingParentCallCommonInspection
82
     */
83
    protected function generateTable()
84
    {
85
        return 'media-records';
86
    }
87
}
88