Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 16
CRAP
0.00% covered (danger)
0.00%
0 / 1
NotificationRuleEntity
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 16
342
0.00% covered (danger)
0.00%
0 / 1
 isActive
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 isDraft
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isInactive
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isBefore
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isAfter
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isOnDate
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 hasMultipleDates
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 hasConditions
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 allConditions
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 allTemplates
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 templateFor
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 hasTemplate
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 channels
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 hasChannel
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getMeta
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 setMeta
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace App\Modules\NotificationModule\Entities;
4
5use CodeIgniter\Entity\Entity;
6
7class NotificationRuleEntity extends Entity
8{
9    protected $attributes = [
10
11        /**
12         * =========================
13         * IDENTIFICATION
14         * =========================
15         */
16        'id'        => null,
17        'rule_name' => null,
18
19        /**
20         * =========================
21         * LOGIQUE REGLE
22         * =========================
23         */
24        'conditions' => [],
25
26        /**
27         * =========================
28         * TEMPLATES MULTI-CANAUX
29         * =========================
30         */
31        'template' => [],
32
33        /**
34         * =========================
35         * TIMING / SCHEDULING
36         * =========================
37         */
38        'delay_type'  => 'before',
39        'delay_value' => 0,
40        'on_date'     => null,
41        'on_dates'    => [],
42
43        /**
44         * =========================
45         * ETAT
46         * =========================
47         */
48        'status'    => 'draft',
49        'is_active' => 1,
50
51        /**
52         * =========================
53         * EXTENSIBILITE
54         * =========================
55         */
56        'metadata' => [],
57    ];
58
59    protected $casts = [
60
61        'id'          => 'integer',
62        'is_active'   => 'boolean',
63        'delay_value' => 'integer',
64
65        'conditions' => 'array',
66        'template'   => 'array',
67        'on_dates'   => 'array',
68        'metadata'   => 'array',
69    ];
70
71    /**
72     * =========================
73     * STATUS
74     * =========================
75     */
76    public function isActive(): bool
77    {
78        return $this->status === 'active' && (bool) $this->is_active;
79    }
80
81    public function isDraft(): bool
82    {
83        return $this->status === 'draft';
84    }
85
86    public function isInactive(): bool
87    {
88        return $this->status === 'inactive';
89    }
90
91    /**
92     * =========================
93     * SCHEDULING
94     * =========================
95     */
96    public function isBefore(): bool
97    {
98        return $this->delay_type === 'before';
99    }
100
101    public function isAfter(): bool
102    {
103        return $this->delay_type === 'after';
104    }
105
106    public function isOnDate(): bool
107    {
108        return $this->delay_type === 'on_date';
109    }
110
111    public function hasMultipleDates(): bool
112    {
113        return !empty($this->on_dates);
114    }
115
116    /**
117     * =========================
118     * CONDITIONS
119     * =========================
120     */
121    public function hasConditions(): bool
122    {
123        return !empty($this->conditions);
124    }
125
126    /**
127     * Renommé allConditions() pour éviter le conflit avec getConditions() → $rule->conditions
128     */
129    public function allConditions(): array
130    {
131        return $this->conditions ?? [];
132    }
133
134    /**
135     * =========================
136     * TEMPLATES MULTI-CANAUX
137     * =========================
138     * Format attendu:
139     * [
140     *   "sms"   => template_id,
141     *   "email" => template_id
142     * ]
143     *
144     * IMPORTANT : ne pas nommer getTemplate() ni getTemplates()
145     * car CI4 intercepte get<Attribute>() pour les attributs
146     * 'template' et déclencherait un appel sans argument.
147     */
148
149    /**
150     * Renommé allTemplates() pour éviter le conflit avec getTemplates() → $rule->templates
151     */
152    public function allTemplates(): array
153    {
154        return $this->template ?? [];
155    }
156
157    /**
158     * Renommé templateFor() pour éviter le conflit avec getTemplate() → $rule->template
159     */
160    public function templateFor(string $channel): mixed
161    {
162        return $this->template[$channel] ?? null;
163    }
164
165    public function hasTemplate(string $channel): bool
166    {
167        return isset($this->template[$channel]);
168    }
169
170    /**
171     * =========================
172     * CHANNELS (depuis metadata)
173     * =========================
174     */
175    public function channels(): array
176    {
177        return $this->metadata['channels'] ?? ['sms'];
178    }
179
180    public function hasChannel(string $channel): bool
181    {
182        return in_array($channel, $this->channels(), true);
183    }
184
185    /**
186     * =========================
187     * METADATA
188     * =========================
189     */
190    public function getMeta(?string $key = null): mixed
191    {
192        if ($key === null) {
193            return $this->metadata ?? [];
194        }
195
196        return $this->metadata[$key] ?? null;
197    }
198
199    public function setMeta(string $key, mixed $value): static
200    {
201        $meta        = $this->metadata ?? [];
202        $meta[$key]  = $value;
203        $this->metadata = $meta;
204
205        return $this;
206    }
207}