HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: /home/xedaptot/hi.naniguide.com/app/Models/DeliveryServer.php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Traits\WorkspaceScoped;

class DeliveryServer extends Model
{
    use HasFactory, SoftDeletes, WorkspaceScoped;

    protected $fillable = [
        'customer_id',
        'name',
        'is_primary',
        'type',
        'status',
        'hostname',
        'port',
        'username',
        'password',
        'encryption',
        'from_email',
        'from_name',
        'reply_to_email',
        'timeout',
        'max_connection_messages',
        'second_quota',
        'minute_quota',
        'hourly_quota',
        'daily_quota',
        'monthly_quota',
        'pause_after_send',
        'settings',
        'locked',
        'use_for',
        'use_for_email_to_list',
        'use_for_transactional',
        'bounce_server_id',
        'tracking_domain_id',
        'notes',
        'verification_token',
        'verified_at',
    ];

    protected function casts(): array
    {
        return [
            'is_primary' => 'boolean',
            'settings' => 'array',
            'locked' => 'boolean',
            'use_for' => 'boolean',
            'use_for_email_to_list' => 'boolean',
            'use_for_transactional' => 'boolean',
            'verified_at' => 'datetime',
        ];
    }

    public function trackingDomain(): BelongsTo
    {
        return $this->belongsTo(TrackingDomain::class, 'tracking_domain_id');
    }

    public function customer(): BelongsTo
    {
        return $this->belongsTo(Customer::class);
    }

    public function bounceServer(): BelongsTo
    {
        return $this->belongsTo(BounceServer::class, 'bounce_server_id');
    }

    public function deliveryLogs(): HasMany
    {
        return $this->hasMany(DeliveryServerLog::class);
    }

    public function campaigns(): HasMany
    {
        return $this->hasMany(Campaign::class);
    }

    public function bounceLogs(): HasManyThrough
    {
        return $this->hasManyThrough(
            BounceLog::class,
            BounceServer::class,
            'id',
            'bounce_server_id',
            'bounce_server_id',
            'id'
        );
    }

    public function isActive(): bool
    {
        return $this->status === 'active';
    }

    public function isLocked(): bool
    {
        return $this->locked;
    }

    /**
     * Check if delivery server is verified.
     */
    public function isVerified(): bool
    {
        return $this->verified_at !== null;
    }

    /**
     * Generate and set verification token.
     */
    public function generateVerificationToken(): string
    {
        $token = bin2hex(random_bytes(32));
        $this->update(['verification_token' => $token]);
        return $token;
    }

    /**
     * Mark delivery server as verified.
     */
    public function markAsVerified(): void
    {
        $this->update([
            'verified_at' => now(),
            'verification_token' => null,
        ]);
    }
}