bytic /
MediaLibrary
| 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
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
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 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
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
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 |