Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
37 / 37 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
1 / 1 |
| SenderModel | |
100.00% |
37 / 37 |
|
100.00% |
8 / 8 |
8 | |
100.00% |
1 / 1 |
| baseQuery | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| getByChannelProvider | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getDefault | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| unsetDefault | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| setDefault | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| existsForChannelProvider | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getAllWithProvider | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| getSenderById | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Modules\ContactModule\Models; |
| 4 | |
| 5 | use CodeIgniter\Model; |
| 6 | use App\Modules\ContactModule\Entities\SenderEntity; |
| 7 | |
| 8 | class SenderModel extends Model |
| 9 | { |
| 10 | protected $table = 'sender'; |
| 11 | protected $primaryKey = 'id'; |
| 12 | |
| 13 | protected $allowedFields = [ |
| 14 | 'name', |
| 15 | 'channel', |
| 16 | 'provider_id', |
| 17 | 'value', |
| 18 | 'status', |
| 19 | 'is_default', |
| 20 | ]; |
| 21 | |
| 22 | protected $useTimestamps = true; |
| 23 | protected $returnType = SenderEntity::class; |
| 24 | |
| 25 | /* |
| 26 | |-------------------------------------------------------------------------- |
| 27 | | BASE QUERY (DRY CORE) |
| 28 | |-------------------------------------------------------------------------- |
| 29 | */ |
| 30 | |
| 31 | private function baseQuery(string $channel, int $providerId) |
| 32 | { |
| 33 | return $this->where([ |
| 34 | 'channel' => $channel, |
| 35 | 'provider_id' => $providerId, |
| 36 | 'status' => 1 |
| 37 | ]); |
| 38 | } |
| 39 | |
| 40 | /* |
| 41 | |-------------------------------------------------------------------------- |
| 42 | | READ METHODS |
| 43 | |-------------------------------------------------------------------------- |
| 44 | */ |
| 45 | |
| 46 | public function getByChannelProvider(string $channel, int $providerId): array |
| 47 | { |
| 48 | return $this->baseQuery($channel, $providerId) |
| 49 | ->findAll(); |
| 50 | } |
| 51 | |
| 52 | public function getDefault(string $channel, int $providerId): ?SenderEntity |
| 53 | { |
| 54 | return $this->baseQuery($channel, $providerId) |
| 55 | ->where('is_default', 1) |
| 56 | ->first(); |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | |-------------------------------------------------------------------------- |
| 61 | | BUSINESS RULE: DEFAULT MANAGEMENT |
| 62 | |-------------------------------------------------------------------------- |
| 63 | */ |
| 64 | |
| 65 | public function unsetDefault(string $channel, int $providerId): bool |
| 66 | { |
| 67 | return $this->where([ |
| 68 | 'channel' => $channel, |
| 69 | 'provider_id' => $providerId, |
| 70 | 'is_default' => 1 |
| 71 | ]) |
| 72 | ->set(['is_default' => 0]) |
| 73 | ->update(); |
| 74 | } |
| 75 | |
| 76 | public function setDefault(int $senderId, string $channel, int $providerId): bool |
| 77 | { |
| 78 | $this->db->transStart(); |
| 79 | |
| 80 | // Reset ancien default |
| 81 | $this->unsetDefault($channel, $providerId); |
| 82 | |
| 83 | // Définir nouveau default |
| 84 | $this->update($senderId, [ |
| 85 | 'is_default' => 1 |
| 86 | ]); |
| 87 | |
| 88 | $this->db->transComplete(); |
| 89 | |
| 90 | return $this->db->transStatus(); |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | |-------------------------------------------------------------------------- |
| 95 | | OPTIONAL HELPERS (scalabilité) |
| 96 | |-------------------------------------------------------------------------- |
| 97 | */ |
| 98 | |
| 99 | public function existsForChannelProvider(int $senderId, string $channel, int $providerId): bool |
| 100 | { |
| 101 | return $this->baseQuery($channel, $providerId) |
| 102 | ->where('id', $senderId) |
| 103 | ->countAllResults() > 0; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Récupère tous les expéditeurs avec le nom du provider joint. |
| 108 | * Retourne des stdClass avec la propriété provider_name. |
| 109 | */ |
| 110 | public function getAllWithProvider(): array |
| 111 | { |
| 112 | return $this->select('sender.*, cp.name as provider_name') |
| 113 | ->join('channel_provider cp', 'cp.id = sender.provider_id', 'left') |
| 114 | ->orderBy('channel', 'ASC') |
| 115 | ->orderBy('is_default', 'DESC') |
| 116 | ->orderBy('name', 'ASC') |
| 117 | ->asArray() // ← retourne des tableaux au lieu d'entités |
| 118 | ->findAll(); |
| 119 | } |
| 120 | |
| 121 | public function getSenderById(int $id): ?object |
| 122 | { |
| 123 | return $this->asObject(\stdClass::class) |
| 124 | ->where('id', $id) |
| 125 | ->first(); |
| 126 | } |
| 127 | } |