Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
23.53% covered (danger)
23.53%
4 / 17
33.33% covered (danger)
33.33%
2 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
ImageGallery
23.53% covered (danger)
23.53%
4 / 17
33.33% covered (danger)
33.33%
2 / 6
130.48
0.00% covered (danger)
0.00%
0 / 1
 getPreviewImageUrl
33.33% covered (danger)
33.33%
2 / 6
0.00% covered (danger)
0.00%
0 / 1
5.67
 hasPreviewImage
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getStatusDisplay
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 getStatusClass
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 getImageCount
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 getTotalFileSize
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2declare(strict_types=1);
3
4namespace App\Model\Entity;
5
6use Cake\ORM\Behavior\Translate\TranslateTrait;
7use Cake\ORM\Entity;
8
9/**
10 * ImageGallery Entity
11 *
12 * @property string $id
13 * @property string $name
14 * @property string $slug
15 * @property string|null $description
16 * @property string|null $preview_image
17 * @property bool $is_published
18 * @property string|null $meta_title
19 * @property string|null $meta_description
20 * @property string|null $meta_keywords
21 * @property string|null $facebook_description
22 * @property string|null $linkedin_description
23 * @property string|null $instagram_description
24 * @property string|null $twitter_description
25 * @property \Cake\I18n\DateTime|null $created
26 * @property \Cake\I18n\DateTime|null $modified
27 * @property string|null $created_by
28 * @property string|null $modified_by
29 *
30 * @property \App\Model\Entity\Image[] $images
31 */
32class ImageGallery extends Entity
33{
34    use SeoEntityTrait;
35    use TranslateTrait;
36
37    /**
38     * Fields that can be mass assigned using newEntity() or patchEntity().
39     *
40     * Note that when '*' is set to true, this allows all unspecified fields to
41     * be mass assigned. For security purposes, it is advised to set '*' to false
42     * (or remove it), and explicitly make individual fields accessible as needed.
43     *
44     * @var array<string, bool>
45     */
46    protected array $_accessible = [
47        'name' => true,
48        'slug' => true,
49        'description' => true,
50        'preview_image' => true,
51        'is_published' => true,
52        // SEO fields (managed by SeoEntityTrait)
53        'meta_title' => true,
54        'meta_description' => true,
55        'meta_keywords' => true,
56        'facebook_description' => true,
57        'linkedin_description' => true,
58        'instagram_description' => true,
59        'twitter_description' => true,
60        'created' => true,
61        'modified' => true,
62        'created_by' => true,
63        'modified_by' => true,
64        'images' => true,
65    ];
66
67    /**
68     * Get the preview image URL for this gallery
69     *
70     * @return string|null Preview image URL or null if no preview exists
71     */
72    public function getPreviewImageUrl(): ?string
73    {
74        if (!$this->preview_image) {
75            return null;
76        }
77
78        $previewPath = WWW_ROOT . 'files' . DS . 'ImageGalleries' . DS . 'preview' . DS . $this->preview_image;
79        if (!file_exists($previewPath)) {
80            return null;
81        }
82
83        return '/files/ImageGalleries/preview/' . $this->preview_image;
84    }
85
86    /**
87     * Check if this gallery has a preview image available
88     *
89     * @return bool True if preview image exists and is accessible
90     */
91    public function hasPreviewImage(): bool
92    {
93        return $this->getPreviewImageUrl() !== null;
94    }
95
96    /**
97     * Get the status display name
98     *
99     * @return string Published or Un-Published
100     */
101    public function getStatusDisplay(): string
102    {
103        return $this->is_published ? __('Published') : __('Un-Published');
104    }
105
106    /**
107     * Get the status CSS class for badges
108     *
109     * @return string CSS class for status badge
110     */
111    public function getStatusClass(): string
112    {
113        return $this->is_published ? 'bg-success' : 'bg-warning';
114    }
115
116    /**
117     * Get the number of images in this gallery
118     *
119     * @return int Number of images
120     */
121    public function getImageCount(): int
122    {
123        return is_array($this->images) ? count($this->images) : 0;
124    }
125
126    /**
127     * Get the total file size of all images in this gallery
128     *
129     * @return int Total file size in bytes
130     */
131    public function getTotalFileSize(): int
132    {
133        if (!is_array($this->images) || empty($this->images)) {
134            return 0;
135        }
136
137        $totalSize = 0;
138        foreach ($this->images as $image) {
139            if (isset($image->file_size) && is_numeric($image->file_size)) {
140                $totalSize += (int)$image->file_size;
141            }
142        }
143
144        return $totalSize;
145    }
146}