MediaProperties::for()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 12
c 1
b 0
f 1
nc 4
nop 2
dl 0
loc 16
ccs 0
cts 2
cp 0
crap 12
rs 9.8666
1
<?php
2
3
namespace ByTIC\MediaLibrary\Models\MediaProperties;
4
5
use ByTIC\MediaLibrary\Collections\Collection;
6
use ByTIC\MediaLibrary\HasMedia\Traits\HasMediaPropertiesTrait;
7
use Nip\Records\AbstractModels\Record;
8
use Nip\Records\RecordManager;
9
10
/**
11
 * Class MediaProperties
12
 * @package ByTIC\MediaLibrary\Models\MediaRecords
13
 *
14
 * @method MediaProperty getNew()
15
 */
16
class MediaProperties extends RecordManager
17
{
18
    /**
19
     * @deprecated use initRelationsMedia
20
     */
21
    protected function initRelations()
22
    {
23
        parent::initRelations();
24
25
        $this->initRelationsMediaTrait();
26
    }
27
28
    protected function initRelationsMediaTrait()
29
    {
30
        $this->morphTo(
31
            'Model',
32
            ['morphTypeField' => 'model', 'fk' => 'model_id']
33
        );
34
    }
35
36
    /**
37
     * @param Record|HasMediaPropertiesTrait $model
38
     * @param Collection|string $collection
39
     * @return MediaProperty|Record
40
     */
41
    public function for(Record $model, $collection)
42
    {
43
        $collection = is_object($collection) ? $collection->getName() : $collection;
44
        if ($model->hasRelation('MediaProperties')) {
45
            return $model->getRelation('MediaProperties')->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

45
            return $model->getRelation('MediaProperties')->/** @scrutinizer ignore-call */ getResults()->filter(
Loading history...
46
                function ($item) use ($collection) {
47
                    return $item->collection_name == $collection;
48
                }
49
            )->current();
50
        }
51
52
        return $this->findOneByParams([
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->findOneByParams(a...ame=?', $collection)))) targeting Nip\Records\AbstractMode...ager::findOneByParams() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
53
            'where' => [
54
                ['model =?', $model->getManager()->getMorphName()],
55
                ['model_id =?', $model->getPrimaryKey()],
56
                ['collection_name=?', $collection]
57
            ]
58
        ]);
59
    }
60
61
    /**
62
     * @param $collection
63
     * @return MediaProperty|Record
64
     */
65
    public function forCollection(Collection $collection)
66
    {
67
        return $this->for($collection->getRecord(), $collection->getName());
0 ignored issues
show
Bug introduced by
It seems like $collection->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

67
        return $this->for(/** @scrutinizer ignore-type */ $collection->getRecord(), $collection->getName());
Loading history...
68
    }
69
70
    /**
71
     * @param Record|HasMediaPropertiesTrait $model
72
     * @param $collection
73
     * @return MediaProperty
74
     */
75
    public function createFor(Record $model, $collection)
76
    {
77
        $record = $this->getNew();
78
        $record->populateFromModel($model);
79
        $record->populateFromCollection($collection);
80
        $record->setCustomPropertiesAttribute('{}');
81
        $record->insert();
82
        return $record;
83
    }
84
85
    /**
86
     * @inheritDoc
87
     * @noinspection PhpMissingParentCallCommonInspection
88
     */
89
    protected function generateTable()
90
    {
91
        return 'media-properties';
92
    }
93
}
94