GalleryImagesParameters::save()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 9
ccs 0
cts 7
cp 0
crap 2
rs 10
1
<?php
2
3
namespace App\Http\Livewire;
4
5
use App\Models\Post;
6
use Livewire\Component;
7
use Spatie\MediaLibrary\MediaCollections\Models\Media;
8
9
class GalleryImagesParameters extends Component
10
{
11
    public $model;
12
    public $image;
13
14
    public $image_description;
15
    public $image_video_url;
16
    public $image_caption; // String shown on image hover
17
    public $image_gallery; // The name of the gallery to assign the image to
18
    public $snippet; //the snippet to copy\paste in blog articles to show the image
19
20
    public $showModal = false;
21
22
    protected $rules = [
23
        'image_description' => 'string',
24
        'image_video_url' => 'string',
25
        'image_caption' => 'string',
26
        'image_gallery' => 'string',
27
        'snippet' => 'string',
28
    ];
29
30
    public function mount($model)
31
    {
32
        $this->model = $model;
33
    }
34
35
    public function render()
36
    {
37
        $galleries = self::getGalleryNames();
0 ignored issues
show
Bug Best Practice introduced by
The method App\Http\Livewire\Galler...ters::getGalleryNames() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
        /** @scrutinizer ignore-call */ 
38
        $galleries = self::getGalleryNames();
Loading history...
38
39
        return view('livewire.gallery-images-parameters', ['galleries' => $galleries]);
40
    }
41
42
    public function edit($imageId)
43
    {
44
        $this->showModal = true;
45
        $this->image = Media::find($imageId);
46
47
        $this->image_description = $this->image->getCustomProperty('image_description');
0 ignored issues
show
Bug introduced by
The method getCustomProperty() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
        /** @scrutinizer ignore-call */ 
48
        $this->image_description = $this->image->getCustomProperty('image_description');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
48
        $this->image_video_url = $this->image->getCustomProperty('image_video_url');
49
        $this->image_caption = $this->image->getCustomProperty('image_caption');
50
        $this->image_gallery = $this->image->getCustomProperty('image_gallery');
51
        $this->snippet = self::getImageSnippet($imageId);
0 ignored issues
show
Bug Best Practice introduced by
The method App\Http\Livewire\Galler...ters::getImageSnippet() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
        /** @scrutinizer ignore-call */ 
52
        $this->snippet = self::getImageSnippet($imageId);
Loading history...
52
    }
53
54
    public function save()
55
    {
56
        $this->image->setCustomProperty('image_description', $this->image_description);
57
        $this->image->setCustomProperty('image_video_url', $this->image_video_url);
58
        $this->image->setCustomProperty('image_caption', $this->image_caption);
59
        $this->image->setCustomProperty('image_gallery', lcfirst($this->image_gallery));
60
        $this->image->save();
61
62
        $this->showModal = false;
63
    }
64
65
    public function close()
66
    {
67
        $this->showModal = false;
68
    }
69
70
71
    /**
72
     * Return the array with the gallery names.
73
     * The gallery names are set as an image property.
74
     *
75
     * @return array
76
     */
77
    public function getGalleryNames()
78
    {
79
        $galleries = [];
80
        foreach ($this->model->getMedia('images') as $image) {
81
            $galleries[$image->getCustomProperty('image_gallery')] = $image->getCustomProperty('image_gallery');
82
        }
83
        $ret = array_values($galleries);
84
85
        return $ret;
86
    }
87
88
    /**
89
     * Create the image snippet string
90
     *
91
     * The width is expressed with a Tailwind class.
92
     *
93
     * @param int $imageId
94
     *
95
     * @return string
96
     */
97
    public function getImageSnippet(int $imageId): string
98
    {
99
        return "{# image id=[" . $imageId . "] alignment=[left] width=[w-48] show_caption=[true] zoom=[true] #}";
100
    }
101
102
}
103