ContentBlock::updateWithAttributes()   B
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 6
nop 1
dl 0
loc 30
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Database\Eloquent\Model;
7
use Spatie\Translatable\HasTranslations;
8
use Spatie\Blender\Model\Traits\HasMedia;
9
use Spatie\Blender\Model\Traits\Draftable;
10
use Spatie\EloquentSortable\SortableTrait;
11
use Illuminate\Database\Eloquent\Relations\MorphTo;
12
use Spatie\MediaLibrary\HasMedia\Interfaces\HasMediaConversions;
13
14
class ContentBlock extends Model implements HasMediaConversions
15
{
16
    use Draftable, SortableTrait, HasTranslations, HasMedia;
17
18
    public $translatable = ['name', 'text'];
19
    protected $guarded = [];
20
21
    public function subject(): MorphTo
22
    {
23
        return $this->morphTo('model');
24
    }
25
26
    public function registerMediaConversions()
27
    {
28
        $this->addMediaConversion('admin')
29
            ->setWidth(368)
30
            ->setHeight(232)
31
            ->nonQueued();
32
33
        $this->addMediaConversion('thumb')
34
            ->setWidth(368)
35
            ->setHeight(232);
36
37
        $this->addMediaConversion('detail')
38
            ->setWidth(740);
39
    }
40
41
    public function mediaLibraryCollections(): array
42
    {
43
        return $this->subject->getContentBlockMediaLibraryCollections();
0 ignored issues
show
Documentation introduced by
The property subject does not exist on object<App\Models\ContentBlock>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
44
    }
45
46
    public function mediaLibraryCollectionType(string $name): string
47
    {
48
        return $this->subject->getContentBlockMediaLibraryCollections()[$name];
0 ignored issues
show
Documentation introduced by
The property subject does not exist on object<App\Models\ContentBlock>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
49
    }
50
51
    public function updateWithAttributes(array $values)
52
    {
53
        $this->draft = false;
0 ignored issues
show
Documentation introduced by
The property draft does not exist on object<App\Models\ContentBlock>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
54
        $this->type = $values['type'];
0 ignored issues
show
Documentation introduced by
The property type does not exist on object<App\Models\ContentBlock>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
55
56
        foreach ($this->translatable as $attribute) {
57
            $this->setTranslations($attribute, $values[$attribute] ?? []);
58
        }
59
60
        foreach ($this->mediaLibraryCollections() as $collection) {
61
            if (! isset($values[$collection])) {
62
                continue;
63
            }
64
65
            $media = array_filter($values[$collection], function ($media) {
66
                return ($media['markedForRemoval'] ?? false) !== true;
67
            });
68
69
            $updatedMedia = $this->updateMedia($media, $collection);
70
71
            $this->media()
72
                ->whereCollectionName($collection)
73
                ->whereNotIn('id', Arr::pluck($updatedMedia, 'id'))
74
                ->delete();
75
        }
76
77
        $this->save();
78
79
        return $this;
80
    }
81
82
    public function setOrder(int $i)
83
    {
84
        $this->order_column = $i;
0 ignored issues
show
Documentation introduced by
The property order_column does not exist on object<App\Models\ContentBlock>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
85
86
        $this->save();
87
88
        return $this;
89
    }
90
}
91