Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.70% covered (success)
90.70%
39 / 43
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ChannelProviderModel
90.70% covered (success)
90.70%
39 / 43
66.67% covered (warning)
66.67%
2 / 3
3.01
0.00% covered (danger)
0.00%
0 / 1
 getByChannel
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getVisible
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
1
 getByUserContext
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Modules\ChannelModule\Models;
4
5use CodeIgniter\Model;
6use App\Modules\ChannelModule\Entities\ChannelProviderEntity;
7
8class ChannelProviderModel extends Model
9{
10    protected $table      = 'channel_provider';
11    protected $primaryKey = 'id';
12
13    protected $allowedFields = [
14        'channel',
15        'name',
16        'code',
17        'status',
18        'priority',
19        'scope',         // global | country
20        'country_code',  // ISO 3166-1 alpha-2, null si scope = global
21    ];
22
23    protected $useTimestamps = true;
24    protected $returnType     = ChannelProviderEntity::class;
25
26    /**
27     * Providers actifs par canal
28     */
29    public function getByChannel(string $channel)
30    {
31        return $this->where('channel', $channel)
32                    ->where('status', 1)
33                    ->orderBy('priority', 'ASC')
34                    ->findAll();
35    }
36
37    /**
38     * Providers visibles selon scope :
39     * - tous les providers global
40     * - providers country dont le country_code correspond au pays
41     */
42 public function getVisible(string $channel, ?string $country_code = null)
43{
44    $builder = $this->db->table('channel_provider cp')
45        ->select('cp.*, 
46                  COALESCE(cps.is_active, 0) AS is_active,
47                  COALESCE(cps.is_default, 0) AS is_default,
48                  cps.country_code')
49
50        ->join(
51            'channel_provider_setting cps',
52            'cps.channel_provider_id = cp.id 
53             AND cps.country_code = ' . $this->db->escape($country_code),
54            'left'
55        )
56
57        ->where('cp.channel', $channel)
58        ->where('cp.status', 1);
59
60    $builder->groupStart()
61        ->where('cp.scope', 'global')
62        ->orWhere('cps.id IS NOT NULL', null, false)
63    ->groupEnd();
64
65    return $builder
66        ->orderBy('cp.priority', 'ASC')
67        ->get()
68        ->getResult();
69}
70
71
72public function getByUserContext(string $channel, string $country_code)
73{
74    return $this->db->table('channel_provider cp')
75        ->select('
76            cp.id,
77            cp.name,
78            cp.code,
79            cp.channel,
80            cp.priority,
81            cps.is_active,
82            cps.is_default
83        ')
84
85        ->join(
86            'channel_provider_setting cps',
87            'cps.channel_provider_id = cp.id 
88             AND cps.channel = cp.channel
89             AND cps.country_code = ' . $this->db->escape($country_code),
90            'inner'
91        )
92
93        ->where('cp.channel', $channel)
94        ->where('cp.status', 1)
95
96        // scope logique
97        ->groupStart()
98            ->where('cp.scope', 'global')
99            ->orWhere('cp.country_code', $country_code)
100        ->groupEnd()
101
102        ->where('cps.is_active', 1)
103
104        ->orderBy('cp.priority', 'ASC')
105        ->get()
106        ->getResult();
107}
108}