DatabaseConfiguration   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 38
rs 10
c 1
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfigurationAttribute() 0 7 2
A setConfigurationAttribute() 0 13 3
A scopeConfiguredDatabase() 0 3 1
1
<?php
2
3
namespace Ikechukwukalu\Dynamicdatabaseconfig\Models;
4
5
use Illuminate\Database\Eloquent\Factories\HasFactory;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Facades\Auth;
8
use Illuminate\Support\Facades\Crypt;
9
10
class DatabaseConfiguration extends Model
11
{
12
    use HasFactory;
13
14
    protected $fillable = [
15
        'ref',
16
        'name',
17
        'database',
18
        'configuration'
19
    ];
20
21
    public function scopeConfiguredDatabase($query, mixed $ref)
22
    {
23
        return $query->where('ref', $ref);
24
    }
25
26
    public function setConfigurationAttribute($value)
27
    {
28
        if (!is_array($value)) {
29
            $value = [];
30
        }
31
32
        $value = json_encode($value);
33
34
        if (config('dynamicdatabaseconfig.hash', true)) {
35
            return $this->attributes['configuration'] = Crypt::encryptString($value);
36
        }
37
38
        return $value;
39
    }
40
41
    public function getConfigurationAttribute($value)
42
    {
43
        if (config('dynamicdatabaseconfig.hash', true)) {
44
            return json_decode(Crypt::decryptString($value), true);
45
        }
46
47
        return json_decode($value, true);
48
    }
49
}
50