Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 11 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| ContactSegmentModel | |
0.00% |
0 / 11 |
|
0.00% |
0 / 5 |
56 | |
0.00% |
0 / 1 |
| beforeInsert | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| beforeUpdate | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| encodeRules | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| getActiveSegments | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| updateContactsCount | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Modules\ContactModule\Models; |
| 4 | |
| 5 | use CodeIgniter\Model; |
| 6 | use App\Modules\ContactModule\Entities\ContactSegmentEntity; |
| 7 | |
| 8 | class ContactSegmentModel extends Model |
| 9 | { |
| 10 | protected $table = 'contact_segment'; |
| 11 | protected $primaryKey = 'id'; |
| 12 | |
| 13 | protected $returnType = ContactSegmentEntity::class; |
| 14 | |
| 15 | protected $useAutoIncrement = true; |
| 16 | protected $useTimestamps = true; |
| 17 | |
| 18 | protected $allowedFields = [ |
| 19 | 'name', |
| 20 | 'rules', |
| 21 | 'contacts_count', |
| 22 | 'status', |
| 23 | ]; |
| 24 | |
| 25 | /** |
| 26 | * ========================= |
| 27 | * AUTO JSON HANDLING |
| 28 | * ========================= |
| 29 | */ |
| 30 | protected function beforeInsert(array $data) |
| 31 | { |
| 32 | return $this->encodeRules($data); |
| 33 | } |
| 34 | |
| 35 | protected function beforeUpdate(array $data) |
| 36 | { |
| 37 | return $this->encodeRules($data); |
| 38 | } |
| 39 | |
| 40 | private function encodeRules(array $data) |
| 41 | { |
| 42 | if (isset($data['data']['rules']) && is_array($data['data']['rules'])) { |
| 43 | $data['data']['rules'] = json_encode($data['data']['rules']); |
| 44 | } |
| 45 | |
| 46 | return $data; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * ========================= |
| 51 | * GET ACTIVE SEGMENTS |
| 52 | * ========================= |
| 53 | */ |
| 54 | public function getActiveSegments(): array |
| 55 | { |
| 56 | return $this->where('status', 'active') |
| 57 | ->orderBy('id', 'DESC') |
| 58 | ->findAll(); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * ========================= |
| 63 | * UPDATE COUNTS (CACHE) |
| 64 | * ========================= |
| 65 | */ |
| 66 | public function updateContactsCount(int $segmentId, int $count): bool |
| 67 | { |
| 68 | return $this->update($segmentId, [ |
| 69 | 'contacts_count' => $count |
| 70 | ]); |
| 71 | } |
| 72 | } |