MediaRecords::for()   A
last analyzed

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
     * @param Record $model
22
     * @param $collection
23
     * @return MediaProperty[]|Collection
24
     */
25
    public function for(Record $model, $collection)
26
    {
27
        $collection = is_object($collection) ? $collection->getName() : $collection;
28
29
        if ($model->hasRelation('Media')) {
30
            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

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

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