Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
ImagesTable
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
4 / 4
4
100.00% covered (success)
100.00%
1 / 1
 initialize
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
 validationDefault
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 validationCreate
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 validationUpdate
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2declare(strict_types=1);
3
4namespace App\Model\Table;
5
6use App\Model\Behavior\ImageValidationTrait;
7use Cake\Log\LogTrait;
8use Cake\ORM\Table;
9use Cake\Validation\Validator;
10
11/**
12 * Images Model
13 *
14 * @method \App\Model\Entity\Image newEmptyEntity()
15 * @method \App\Model\Entity\Image newEntity(array $data, array $options = [])
16 * @method array<\App\Model\Entity\Image> newEntities(array $data, array $options = [])
17 * @method \App\Model\Entity\Image get(mixed $primaryKey, array|string $finder = 'all', \Psr\SimpleCache\CacheInterface|string|null $cache = null, \Closure|string|null $cacheKey = null, mixed ...$args)
18 * @method \App\Model\Entity\Image findOrCreate($search, ?callable $callback = null, array $options = [])
19 * @method \App\Model\Entity\Image patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
20 * @method array<\App\Model\Entity\Image> patchEntities(iterable $entities, array $data, array $options = [])
21 * @method \App\Model\Entity\Image|false save(\Cake\Datasource\EntityInterface $entity, array $options = [])
22 * @method \App\Model\Entity\Image saveOrFail(\Cake\Datasource\EntityInterface $entity, array $options = [])
23 * @method iterable<\App\Model\Entity\Image>|\Cake\Datasource\ResultSetInterface<\App\Model\Entity\Image>|false saveMany(iterable $entities, array $options = [])
24 * @method iterable<\App\Model\Entity\Image>|\Cake\Datasource\ResultSetInterface<\App\Model\Entity\Image> saveManyOrFail(iterable $entities, array $options = [])
25 * @method iterable<\App\Model\Entity\Image>|\Cake\Datasource\ResultSetInterface<\App\Model\Entity\Image>|false deleteMany(iterable $entities, array $options = [])
26 * @method iterable<\App\Model\Entity\Image>|\Cake\Datasource\ResultSetInterface<\App\Model\Entity\Image> deleteManyOrFail(iterable $entities, array $options = [])
27 * @mixin \Cake\ORM\Behavior\TimestampBehavior
28 */
29class ImagesTable extends Table
30{
31    use ImageValidationTrait;
32    use LogTrait;
33    use QueueableJobsTrait;
34
35    /**
36     * Initialize method
37     *
38     * @param array<string, mixed> $config The configuration for the Table.use Cake\Event\EventInterface;
39     * @return void
40     */
41    public function initialize(array $config): void
42    {
43        parent::initialize($config);
44
45        $this->setTable('images');
46        $this->setDisplayField('name');
47        $this->setPrimaryKey('id');
48
49        $this->addBehavior('QueueableImage', [
50            'folder_path' => 'files/Images/image/',
51            'field' => 'image',
52        ]);
53
54        $this->addBehavior('Timestamp');
55
56        $this->belongsToMany('ImageGalleries', [
57            'foreignKey' => 'image_id',
58            'targetForeignKey' => 'image_gallery_id',
59            'joinTable' => 'image_galleries_images',
60            'through' => 'ImageGalleriesImages',
61        ]);
62    }
63
64    /**
65     * Default validation rules.
66     *
67     * @param \Cake\Validation\Validator $validator Validator instance.
68     * @return \Cake\Validation\Validator
69     */
70    public function validationDefault(Validator $validator): Validator
71    {
72        $validator->notEmptyString('name', 'Name cannot be empty');
73
74        return $validator;
75    }
76
77    /**
78     * Validation rules for creating a new image.
79     *
80     * Extends the default validation rules and adds specific requirements for image creation:
81     * - Requires the presence of an image file
82     * - Validates the image file mime type (JPEG, PNG, or GIF)
83     *
84     * @param \Cake\Validation\Validator $validator Validator instance.
85     * @return \Cake\Validation\Validator
86     */
87    public function validationCreate(Validator $validator): Validator
88    {
89        $validator = $this->validationDefault($validator);
90
91        return $this->addRequiredImageValidation($validator, 'image', [
92            'messages' => [
93                'mimeType' => 'Please upload only jpeg, png, or gif images.',
94                'fileSize' => 'Image must be less than 10MB.',
95                'required' => 'An image file is required',
96            ],
97        ]);
98    }
99
100    /**
101     * Validation rules for updating an existing image.
102     *
103     * Extends the default validation rules and adds specific requirements for image updates:
104     * - Allows the image file to be empty (no change)
105     * - If a new image is provided, validates the mime type (JPEG, PNG, or GIF)
106     * - Mime type validation only occurs when a new file is successfully uploaded
107     *
108     * @param \Cake\Validation\Validator $validator Validator instance.
109     * @return \Cake\Validation\Validator
110     */
111    public function validationUpdate(Validator $validator): Validator
112    {
113        $validator = $this->validationDefault($validator);
114
115        return $this->addOptionalImageValidation($validator, 'image', [
116            'messages' => [
117                'mimeType' => 'Please upload only jpeg, png, or gif images.',
118                'fileSize' => 'Image must be less than 10MB.',
119            ],
120        ]);
121    }
122}