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/ListSegment.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\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class ListSegment extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = [
        'list_id',
        'name',
        'description',
        'rules',
        'subscribers_count',
        'is_active',
    ];

    protected function casts(): array
    {
        return [
            'rules' => 'array',
            'is_active' => 'boolean',
        ];
    }

    public function emailList(): BelongsTo
    {
        return $this->belongsTo(EmailList::class, 'list_id');
    }

    public function subscribers(): BelongsToMany
    {
        // This would be a dynamic relationship based on rules
        // For now, we'll implement it as a method that queries based on rules
        return $this->belongsToMany(ListSubscriber::class, 'list_segment_subscriber', 'segment_id', 'subscriber_id');
    }

    public function getSubscribersCountAttribute($value): int
    {
        $pivotCount = \Illuminate\Support\Facades\DB::table('list_segment_subscriber')
            ->where('segment_id', $this->id)
            ->count();

        if ($pivotCount > 0) {
            return (int) $pivotCount;
        }

        $rules = is_array($this->rules) ? $this->rules : [];
        if (empty($rules['conditions'] ?? [])) {
            return (int) $value;
        }

        $emailList = $this->emailList;
        if (!$emailList) {
            return (int) $value;
        }

        $service = app(\App\Services\ListSubscriberService::class);
        return (int) $service->query($emailList, ['segment_id' => $this->id])->count();
    }
}