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

MediaProperties::for()   A

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 12
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
use Nip\Records\Traits\Relations\HasRelationsRecordTrait;
10
11
/**
12
 * Class MediaProperties
13
 * @package ByTIC\MediaLibrary\Models\MediaRecords
14
 *
15
 * @method MediaProperty getNew()
16
 */
17
class MediaProperties extends RecordManager
18
{
19
    /**
20
     * @param Record|HasMediaPropertiesTrait $model
21
     * @param Collection|string $collection
22
     * @return MediaProperty|Record
23
     */
24
    public function for(Record $model, $collection)
25
    {
26
        $collection = is_object($collection) ? $collection->getName() : $collection;
27
        if ($model->hasRelation('MediaProperties')) {
28
            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

28
            return $model->getRelation('MediaProperties')->/** @scrutinizer ignore-call */ getResults()->filter(
Loading history...
29
                function ($item) use ($collection) {
30
                    return $item->collection_name == $collection;
31
                }
32
            )->current();
33
        }
34
35
        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...
36
            'where' => [
37
                ['model =?', $model->getManager()->getMorphName()],
38
                ['model_id =?', $model->getPrimaryKey()],
39
                ['collection_name=?', $collection]
40
            ]
41
        ]);
42
    }
43
44
    /**
45
     * @param $collection
46
     * @return MediaProperty|Record
47
     */
48
    public function forCollection(Collection $collection)
49
    {
50
        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

50
        return $this->for(/** @scrutinizer ignore-type */ $collection->getRecord(), $collection->getName());
Loading history...
51
    }
52
53
    /**
54
     * @param Record|HasMediaPropertiesTrait $model
55
     * @param $collection
56
     * @return MediaProperty
57
     */
58
    public function createFor(Record $model, $collection)
59
    {
60
        $record = $this->getNew();
61
        $record->populateFromModel($model);
62
        $record->populateFromCollection($collection);
63
        $record->insert();
64
        return $record;
65
    }
66
67
    /**
68
     * @inheritDoc
69
     * @noinspection PhpMissingParentCallCommonInspection
70
     */
71
    protected function generateTable()
72
    {
73
        return 'media-properties';
74
    }
75
}
76