ExportConfig::getTable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace NovaExportConfiguration\Models;
4
5
use Illuminate\Database\Eloquent\Factories\HasFactory;
6
use Illuminate\Database\Eloquent\Model;
7
use JsonFieldCast\Casts\SimpleJsonField;
8
use NovaExportConfiguration\Casts\ExportConfigFilters;
9
use NovaExportConfiguration\Helpers\QueryString;
10
use NovaExportConfiguration\NovaExportConfig;
11
use NovaExportConfiguration\Repositories\ExportRepository;
12
13
/**
14
 * @property \NovaExportConfiguration\Casts\Json\ExportConfigFilters $filters
15
 * @property \JsonFieldCast\Json\SimpleJsonField $meta
16
 */
17
class ExportConfig extends Model
18
{
19
    use HasFactory;
20
21
    protected $guarded = [];
22
23
    protected $hidden = [
24
        'sql_query',
25
    ];
26
27
    protected $casts = [
28
        'last_export_at' => 'datetime',
29
        'filters'        => ExportConfigFilters::class,
30
        'meta'           => SimpleJsonField::class,
31
    ];
32
33
    public function getTable(): string
34
    {
35
        return config('nova-export-configuration.tables.export_configs');
36
    }
37
38
    protected static function newFactory()
39
    {
40
        return \NovaExportConfiguration\Database\Factories\ExportConfigFactory::new();
41
    }
42
43
    protected static function booted(): void
44
    {
45
        static::saving(function (self $model) {
46
            if ($model->isDirty('filters')) {
47
                $model->updateConfigurationContent();
48
            }
49
        });
50
        static::created(fn (self $model) => $model->updateConfigurationContent());
51
    }
52
53
    public function exportRepository(): ?ExportRepository
54
    {
55
        return NovaExportConfig::getRepositories()->getByName($this->attributes['type']??null);
56
    }
57
58
    public function updateConfigurationContent(bool $persist = false): static
59
    {
60
        if ($repo = $this->exportRepository()) {
61
            $this->fill([
62
                'sql_query' => QueryString::readableSqlQuery($repo->exportQuery($this)->query()),
63
            ]);
64
            $repo->regenerateConfigurationData($this, $persist);
65
66
            if ($persist) {
67
                $this->save();
68
            }
69
        }
70
71
        return $this;
72
    }
73
74
    public function setAttribute($key, $value)
75
    {
76
        if (($repo = $this->exportRepository()) && $repo->isFilterKey($key)) {
77
            return $repo->modelSetAttribute($this, $key, $value);
78
        }
79
80
        return parent::setAttribute($key, $value);
81
    }
82
83
    public function getAttribute($key)
84
    {
85
        if (($repo = $this->exportRepository()) && $repo->isFilterKey($key)) {
86
            return $repo->modelGetAttribute($this, $key);
87
        }
88
89
        return parent::getAttribute($key);
90
    }
91
92
    public function scopeType($query, string $type)
93
    {
94
        return $query->where('type', $type);
95
    }
96
}
97