Image   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 2
dl 0
loc 35
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A posts() 0 4 1
A isSafeForDelete() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ReliqArts\GuidedImage\Examples;
6
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Support\Collection;
9
use ReliqArts\GuidedImage\Contracts\GuidedImage;
10
use ReliqArts\GuidedImage\Concerns\Guided;
11
12
/**
13
 *  Image model.
14
 *
15
 * @property Collection $posts
16
 */
17
class Image extends Model implements GuidedImage
18
{
19
    use Guided;
20
21
    /**
22
     *  Images table.
23
     */
24
    protected $table = 'images';
25
26
    /**
27
     *  Set guard.
28
     */
29
    protected $guarded = ['id', 'created_at', 'updated_at'];
30
31
    /**
32
     *  Posts.
33
     */
34
    public function posts()
35
    {
36
        return $this->morphedByMany('App\Post', 'imageable');
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function isSafeForDelete(int $safeAmount = 1): bool
43
    {
44
        /** @noinspection PhpUndefinedClassInspection */
45
        $posts = Post::withTrashed()->where('image_id', $this->id)->get();
46
        $posts = $this->posts->merge($posts);
47
        $usage = $posts->count();
48
49
        return $usage <= $safeAmount;
50
    }
51
}
52