Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
PlatformAccount
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 6
110
0.00% covered (danger)
0.00%
0 / 1
 password
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 apiToken
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 getActive
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 setAsActive
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 channels
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 activeChannels
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace App\Models;
4
5use Database\Factories\PlatformAccountFactory;
6use Illuminate\Database\Eloquent\Factories\HasFactory;
7use Illuminate\Database\Eloquent\Model;
8use Illuminate\Database\Eloquent\Casts\Attribute;
9use Illuminate\Database\Eloquent\Collection;
10use Illuminate\Database\Eloquent\Relations\BelongsToMany;
11use Illuminate\Support\Carbon;
12use Illuminate\Support\Facades\Crypt;
13use App\Enums\PlatformEnum;
14
15/**
16 * @property int $id
17 * @property PlatformEnum $platform
18 * @property string $instance_url
19 * @property string $username
20 * @property string $password
21 * @property string $settings
22 * @property bool $is_active
23 * @property Carbon $last_tested_at
24 * @property string $status
25 * @property Carbon $created_at
26 * @property Carbon $updated_at
27 * @property Collection<int, PlatformChannel> $activeChannels
28 * @method static where(string $string, PlatformEnum $platform)
29 * @method static orderBy(string $string)
30 * @method static create(array<string, mixed> $validated)
31 */
32class PlatformAccount extends Model
33{
34    /** @use HasFactory<PlatformAccountFactory> */
35    use HasFactory;
36
37    protected $fillable = [
38        'platform',
39        'instance_url',
40        'username',
41        'password',
42        'api_token',
43        'settings',
44        'is_active',
45        'last_tested_at',
46        'status'
47    ];
48
49    protected $casts = [
50        'platform' => PlatformEnum::class,
51        'settings' => 'array',
52        'is_active' => 'boolean',
53        'last_tested_at' => 'datetime'
54    ];
55
56    // Encrypt password when storing
57    /**
58     * @return Attribute<string|null, string|null>
59     */
60    protected function password(): Attribute
61    {
62        return Attribute::make(
63            get: fn ($value) => $value ? Crypt::decryptString($value) : null,
64            set: fn ($value) => $value ? Crypt::encryptString($value) : null,
65        );
66    }
67
68    // Encrypt API token when storing
69    /**
70     * @return Attribute<string|null, string|null>
71     */
72    protected function apiToken(): Attribute
73    {
74        return Attribute::make(
75            get: fn ($value) => $value ? Crypt::decryptString($value) : null,
76            set: fn ($value) => $value ? Crypt::encryptString($value) : null,
77        );
78    }
79
80    // Get the active accounts for a platform (returns collection)
81    /**
82     * @return Collection<int, PlatformAccount>
83     */
84    public static function getActive(PlatformEnum $platform): Collection
85    {
86        return static::where('platform', $platform)
87            ->where('is_active', true)
88            ->get();
89    }
90
91    // Set this account as active (deactivates others for same platform)
92    public function setAsActive(): void
93    {
94        // Deactivate other accounts for this platform
95        static::where('platform', $this->platform)
96            ->where('id', '!=', $this->id)
97            ->update(['is_active' => false]);
98
99        // Activate this account
100        $this->update(['is_active' => true]);
101    }
102
103    /**
104     * @return BelongsToMany<PlatformChannel, $this>
105     */
106    public function channels(): BelongsToMany
107    {
108        return $this->belongsToMany(PlatformChannel::class, 'platform_account_channels')
109            ->withPivot(['is_active', 'priority'])
110            ->withTimestamps();
111    }
112
113    /**
114     * @return BelongsToMany<PlatformChannel, $this>
115     */
116    public function activeChannels(): BelongsToMany
117    {
118        return $this->channels()
119            ->wherePivot('is_active', true)
120            ->orderByPivot('priority', 'desc');
121    }
122}