lasthourcloud/app/Models/Team.php

117 lines
3.3 KiB
PHP
Raw Normal View History

<?php
namespace App\Models;
2023-06-01 10:15:33 +00:00
use App\Notifications\Channels\SendsEmail;
2023-05-25 16:27:52 +00:00
use App\Notifications\Channels\SendsDiscord;
use Illuminate\Database\Eloquent\Builder;
2023-06-15 07:15:41 +00:00
use Illuminate\Database\Eloquent\Model;
2023-05-25 16:27:52 +00:00
use Illuminate\Notifications\Notifiable;
use Spatie\SchemalessAttributes\Casts\SchemalessAttributes;
2023-06-20 17:08:43 +00:00
use Spatie\SchemalessAttributes\SchemalessAttributesTrait;
2023-05-25 16:27:52 +00:00
2023-06-15 07:15:41 +00:00
class Team extends Model implements SendsDiscord, SendsEmail
{
2023-06-20 17:08:43 +00:00
use Notifiable, SchemalessAttributesTrait;
2023-05-25 16:27:52 +00:00
2023-06-20 17:08:43 +00:00
protected $schemalessAttributes = [
'smtp',
'discord',
'smtp_notifications',
'discord_notifications',
];
protected $casts = [
2023-06-20 17:08:43 +00:00
'smtp' => SchemalessAttributes::class,
'discord' => SchemalessAttributes::class,
'smtp_notifications' => SchemalessAttributes::class,
'discord_notifications' => SchemalessAttributes::class,
'personal_team' => 'boolean',
];
2023-06-20 17:08:43 +00:00
public function scopeWithSmtp(): Builder
{
return $this->smtp->modelScope();
}
public function scopeWithDiscord(): Builder
{
return $this->discord->modelScope();
}
public function scopeWithSmtpNotifications(): Builder
{
return $this->smtp_notifications->modelScope();
}
public function scopeWithDiscordNotifications(): Builder
{
return $this->discord_notifications->modelScope();
}
protected $fillable = [
'id',
'name',
2023-06-16 13:01:58 +00:00
'description',
2023-05-25 16:27:52 +00:00
'personal_team',
2023-06-20 17:08:43 +00:00
'smtp',
'discord'
];
2023-05-25 16:27:52 +00:00
public function routeNotificationForDiscord()
{
2023-06-20 17:08:43 +00:00
return $this->discord->get('webhook_url');
2023-05-25 16:27:52 +00:00
}
2023-06-20 17:08:43 +00:00
public function routeNotificationForEmail(string $attribute = 'recipients')
2023-05-25 16:27:52 +00:00
{
2023-06-20 17:08:43 +00:00
$recipients = $this->smtp->get($attribute, '');
2023-06-01 11:24:20 +00:00
if (is_null($recipients) || $recipients === '') {
return [];
}
return explode(',', $recipients);
2023-05-25 16:27:52 +00:00
}
2023-06-20 17:08:43 +00:00
2023-07-13 13:07:42 +00:00
public function subscription()
{
return $this->hasOne(Subscription::class);
}
public function projects()
{
return $this->hasMany(Project::class);
}
2023-05-25 16:27:52 +00:00
public function servers()
{
2023-03-27 12:31:42 +00:00
return $this->hasMany(Server::class);
}
2023-05-25 16:27:52 +00:00
public function applications()
{
2023-03-30 17:50:27 +00:00
return $this->hasManyThrough(Application::class, Project::class);
}
2023-05-25 16:27:52 +00:00
2023-05-03 10:38:57 +00:00
public function privateKeys()
{
return $this->hasMany(PrivateKey::class);
}
2023-06-02 10:34:45 +00:00
public function members()
{
2023-06-09 13:55:21 +00:00
return $this->belongsToMany(User::class, 'team_user', 'team_id', 'user_id')->withPivot('role');
}
public function invitations()
{
return $this->hasMany(TeamInvitation::class);
2023-06-02 10:34:45 +00:00
}
2023-06-12 20:02:10 +00:00
public function sources()
{
$sources = collect([]);
$github_apps = $this->hasMany(GithubApp::class)->whereisPublic(false)->get();
$gitlab_apps = $this->hasMany(GitlabApp::class)->whereisPublic(false)->get();
// $bitbucket_apps = $this->hasMany(BitbucketApp::class)->get();
$sources = $sources->merge($github_apps)->merge($gitlab_apps);
return $sources;
}
2023-06-16 13:56:25 +00:00
public function isEmpty()
{
if ($this->projects()->count() === 0 && $this->servers()->count() === 0 && $this->privateKeys()->count() === 0 && $this->sources()->count() === 0) {
return true;
}
return false;
}
}