Test Failed
Push — master ( c9f7be...859573 )
by Gabriel
06:41
created

MediaRecords::createFor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 6
c 1
b 0
f 1
nc 1
nop 3
dl 0
loc 8
rs 10
1
<?php
2
3
namespace ByTIC\MediaLibrary\Models\MediaRecords;
4
5
use ByTIC\MediaLibrary\Models\MediaProperties\MediaProperty;
6
use Nip\Records\AbstractModels\Record;
7
use Nip\Records\Collections\Collection;
8
use Nip\Records\RecordManager;
9
10
/**
11
 * Class MediaRecords
12
 * @package ByTIC\MediaLibrary\Models\MediaRecords
13
 *
14
 * @method MediaRecord getNew
15
 */
16
class MediaRecords extends RecordManager
17
{
18
19
    /**
20
     * @param Record $model
21
     * @param $collection
22
     * @return MediaProperty[]|Collection
23
     */
24
    public function for(Record $model, $collection)
25
    {
26
        if ($model->hasRelation('Media')) {
27
            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

27
            return $model->getRelation('Media')->/** @scrutinizer ignore-call */ getResults()->filter(
Loading history...
28
                function ($item) use ($collection) {
29
                    return $item->collection_name == $collection;
30
                }
31
            );
32
        }
33
34
        return $this->findByParams([
35
            'where' => [
36
                ['model =?', $model->getManager()->getController()],
37
                ['model_id =?', $model->getPrimaryKey()],
38
                ['collection_name=?', $collection]
39
            ]
40
        ]);
41
    }
42
43
    /**
44
     * @param $file
45
     * @param Record $model
46
     * @param $collection
47
     * @return Record
48
     */
49
    public function createFor($file, Record $model, $collection)
50
    {
51
        $record = $this->getNew();
52
        $record->populateFromFile($file);
53
        $record->populateFromModel($model);
54
        $record->populateFromCollection($collection);
55
        $record->insert();
56
        return $record;
57
    }
58
59
    /**
60
     * @inheritDoc
61
     * @noinspection PhpMissingParentCallCommonInspection
62
     */
63
    protected function generateTable()
64
    {
65
        return 'media-records';
66
    }
67
}
68