Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
GroupOlxPublicationLogModel
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
7 / 7
8
100.00% covered (success)
100.00%
1 / 1
 booted
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 casts
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 triggeredBy
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 errorList
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 warningList
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 stringList
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 newFactory
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace ImovelZapAi\Publishing\Connectors\GroupOlx\Infrastructure\Persistence\Eloquent;
6
7use App\Models\User;
8use Database\Factories\GroupOlxPublicationLogFactory;
9use Illuminate\Database\Eloquent\Factories\HasFactory;
10use Illuminate\Database\Eloquent\Model;
11use Illuminate\Database\Eloquent\Relations\BelongsTo;
12use ImovelZapAi\Publishing\Connectors\GroupOlx\Domain\Exceptions\GroupOlxPublicationLogImmutableException;
13use ImovelZapAi\Shared\Infrastructure\Persistence\Concerns\BelongsToTenant;
14use ImovelZapAi\Shared\Infrastructure\Persistence\Concerns\HasUuidPrimaryKey;
15
16/**
17 * PublicationLog do feed VRSync (Grupo OLX) — append-only.
18 */
19class GroupOlxPublicationLogModel extends Model
20{
21    use BelongsToTenant;
22
23    /** @use HasFactory<GroupOlxPublicationLogFactory> */
24    use HasFactory;
25    use HasUuidPrimaryKey;
26
27    public $timestamps = false;
28
29    protected $table = 'group_olx_publication_logs';
30
31    /** @var list<string> */
32    protected $fillable = [
33        'tenant_id',
34        'triggered_by',
35        'listing_count',
36        'duration_ms',
37        'success',
38        'version',
39        'content_hash',
40        'errors',
41        'warnings',
42        'generated_at',
43    ];
44
45    protected static function booted(): void
46    {
47        static::deleting(function (): void {
48            throw GroupOlxPublicationLogImmutableException::cannotDelete();
49        });
50    }
51
52    /** @return array<string, string> */
53    protected function casts(): array
54    {
55        return [
56            'listing_count' => 'integer',
57            'duration_ms' => 'integer',
58            'success' => 'boolean',
59            'errors' => 'array',
60            'warnings' => 'array',
61            'generated_at' => 'datetime',
62        ];
63    }
64
65    public function triggeredBy(): BelongsTo
66    {
67        return $this->belongsTo(User::class, 'triggered_by');
68    }
69
70    /** @return list<string> */
71    public function errorList(): array
72    {
73        return $this->stringList($this->errors);
74    }
75
76    /** @return list<string> */
77    public function warningList(): array
78    {
79        return $this->stringList($this->warnings);
80    }
81
82    /**
83     * @param  mixed  $value
84     * @return list<string>
85     */
86    private function stringList(mixed $value): array
87    {
88        if (! is_array($value)) {
89            return [];
90        }
91
92        return array_values(array_filter(array_map('strval', $value), fn (string $item): bool => $item !== ''));
93    }
94
95    protected static function newFactory(): GroupOlxPublicationLogFactory
96    {
97        return GroupOlxPublicationLogFactory::new();
98    }
99}