Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 62
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
ContactListController
0.00% covered (danger)
0.00%
0 / 62
0.00% covered (danger)
0.00%
0 / 8
240
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 index
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
20
 create
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 store
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 edit
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 update
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 delete
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 validateInput
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace App\Modules\ContactModule\Controllers;
4
5use App\Controllers\BaseController;
6use App\Modules\ContactModule\Models\ContactListModel;
7use CodeIgniter\Exceptions\PageNotFoundException;
8
9class ContactListController extends BaseController
10{
11    protected ContactListModel $model;
12
13    public function __construct()
14    {
15        $this->model = new ContactListModel();
16    }
17
18    /**
19     * LISTE
20     */
21    public function index()
22    {
23        $query = $this->model->orderBy('id', 'DESC');
24
25        // FILTRES PRÉPARÉS (scalable)
26        $search  = $this->request->getGet('search');
27        $channel = $this->request->getGet('channel');
28        $status  = $this->request->getGet('status');
29
30        if ($search) {
31            $query->like('name', $search);
32        }
33
34        if ($channel) {
35            $query->where('channel', $channel);
36        }
37
38        if ($status) {
39            $query->where('status', $status);
40        }
41
42        $lists = $query->findAll();
43
44        return view(
45            'App\Modules\ContactModule\Views\lists\index',
46            [
47                'lists' => $lists,
48                'filters' => [
49                    'search' => $search,
50                    'channel' => $channel,
51                    'status'  => $status,
52                ]
53            ]
54        );
55    }
56
57    /**
58     * CREATE FORM
59     */
60    public function create()
61    {
62        return view('App\Modules\ContactModule\Views\lists\create');
63    }
64
65    /**
66     * STORE
67     */
68    public function store()
69    {
70        $data = $this->validateInput();
71
72        $this->model->insert($data);
73
74        return redirect()->to(base_url('dashboard/contact-listes'));
75    }
76
77    /**
78     * EDIT FORM
79     */
80    public function edit($id)
81    {
82        $list = $this->model->find($id);
83
84        if (!$list) {
85            throw new PageNotFoundException("Liste introuvable");
86        }
87
88        return view(
89            'App\Modules\ContactModule\Views\lists\edit',
90            ['list' => $list]
91        );
92    }
93
94    /**
95     * UPDATE
96     */
97    public function update($id)
98    {
99        if (!$this->model->find($id)) {
100            throw new PageNotFoundException("Liste introuvable");
101        }
102
103        $data = $this->validateInput();
104
105        $this->model->update($id, $data);
106
107        return redirect()->to(base_url('dashboard/contact-listes'));
108    }
109
110    /**
111     * DELETE sécurisé (POST uniquement)
112     */
113    public function delete($id)
114    {
115        if ($this->request->getMethod() !== 'post') {
116            throw new \CodeIgniter\Exceptions\PageNotFoundException();
117        }
118
119        $this->model->delete($id);
120
121        return redirect()->to(base_url('dashboard/contact-listes'));
122    }
123
124    /**
125     * NORMALISATION + VALIDATION
126     */
127    private function validateInput(): array
128    {
129        $rules = [
130            'name'        => 'required|min_length[2]|max_length[150]',
131            'channel'     => 'required|in_list[sms,email,whatsapp]',
132            'description' => 'permit_empty|max_length[255]',
133            'source'      => 'permit_empty|max_length[100]'
134        ];
135
136        if (!$this->validate($rules)) {
137            throw new \RuntimeException(
138                json_encode($this->validator->getErrors())
139            );
140        }
141
142        return [
143            'name'        => $this->request->getPost('name'),
144            'channel'     => $this->request->getPost('channel'),
145            'description' => $this->request->getPost('description'),
146            'status'      => 'active',
147            'metadata'    => [
148                'source' => $this->request->getPost('source')
149            ]
150        ];
151    }
152}