StaticPageService::getStaticImageHtml()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 12
rs 10
1
<?php
2
3
namespace App\Services;
4
5
use App\Models\Post;
6
use App\Services\Snippets\GalleryMasonryService;
7
use App\Services\Snippets\ImageService;
8
use Spatie\MediaLibrary\MediaCollections\Models\Media;
9
10
class StaticPageService
11
{
12
    private GalleryMasonryService $galleryMasonryService;
13
    private ImageService $imageService;
14
15
    /**
16
     * StaticPageService constructor.
17
     *
18
     * @param \App\Services\Snippets\GalleryMasonryService $galleryMasonryService
19
     * @param \App\Services\Snippets\ImageService $imageService
20
     */
21
    public function __construct(
22
        GalleryMasonryService $galleryMasonryService,
23
        ImageService $imageService
24
    ) {
25
        $this->galleryMasonryService = $galleryMasonryService;
26
        $this->imageService = $imageService;
27
    }
28
29
    /**
30
     * Return the static gallery HTML
31
     *
32
     * To show this in a blade view:
33
     * - Load in the controller method end pass it to the view:
34
     *      $gallery1Html = $this->staticPageService->getStaticGalleryHtml('contact improvisation', true);
35
     * - Then in the blade view:
36
     *      {!! $gallery1Html !!}
37
     *
38
     * @param string $galleryName
39
     * @param bool $animate
40
     *
41
     * @return string
42
     * @throws \Spatie\ModelStatus\Exceptions\InvalidStatus
43
     */
44
    public function getStaticGalleryHtml(string $galleryName, bool $animate): string
45
    {
46
        $post = Post::firstOrCreate([
47
            'title->en' => 'Static pages images',
48
            'intro_text->en' => 'Static pages images',
49
            'body->en' => 'Static pages images',
50
            'category_id' => 1,
51
            'user_id' => 1,
52
        ]);
53
        $post->setStatus('published');
54
55
        $galleryImages = $this->galleryMasonryService->createImagesArray($post, $galleryName);
56
57
        if (empty($galleryImages)) {
58
            return "<div class='bg-yellow-200 p-5'>There is not a gallery called <b>$galleryName</b>. <br> Please define it in the Media manager</div>";
59
        }
60
61
        return $this->galleryMasonryService->prepareGalleryHtml($galleryImages, $animate);
62
    }
63
64
    /**
65
     * Return the static Image HTML
66
     *
67
     * To show this in a blade view:
68
     * - Load in the controller method end pass it to the view:
69
     *      $image1Html = $this->staticPageService->getStaticImageHtml('1');
70
     * - Then in the blade view:
71
     *      {!! $image1Html !!}
72
     *
73
     * @param int $id
74
     *
75
     * @return string
76
     */
77
    public function getStaticImageHtml(int $id): string
78
    {
79
        $image = Media::find($id);
80
81
        $parameters = [];
82
        $parameters['image_id'] = $id;
83
        $parameters['alignment'] = 'right';
84
        $parameters['width'] = 'w-48';
85
        $parameters['show_caption'] = true;
86
        $parameters['zoom'] = true;
87
88
        return $this->imageService->prepareImageHtml($image, $parameters);
89
    }
90
91
92
}
93