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/be.naniguide.com/app/ActivityLog/BaseActivity.php
<?php

namespace App\ActivityLog;

use App\Model\ActivityLog;
use App\Model\User;

abstract class BaseActivity implements ActivityInterface
{
    protected int $customerId;
    protected ?int $userId = null;
    protected int $subjectId;
    protected string $subjectName;
    protected array $data = [];

    /**
     * When true, ActivityLog model's created hook mirrors the row into the
     * `notifications` table (customer-facing bell). Default false — most
     * activity log writes are internal/admin audit trail and do NOT warrant
     * a customer notification. Override on subclasses that surface to the
     * customer's bell (e.g. SubscriptionEvent).
     */
    protected static bool $notify = false;

    public static function notify(): bool
    {
        return static::$notify;
    }

    /**
     * Severity for the notification mirror (info | success | warning | error).
     * Default `info`. Subclasses override based on $log->type to flag failures
     * as `warning` / `error` so the customer's bell highlights appropriately.
     */
    public static function level(ActivityLog $log): string
    {
        return \App\Notifications\AppNotification::SEVERITY_INFO;
    }

    public function record(): ActivityLog
    {
        return ActivityLog::create([
            'customer_id' => $this->customerId,
            'user_id' => $this->userId,
            'type' => static::type(),
            'subject_type' => static::subjectType(),
            'subject_id' => $this->subjectId,
            'subject_name' => $this->subjectName,
            'data' => $this->data,
        ]);
    }

    /**
     * Set actor and infer customer from user.
     */
    protected function setActor(User $user): void
    {
        $this->userId = $user->id;
        $this->customerId = $user->customer->id;
    }

    /**
     * Render a link to the subject, or bold name if entity is deleted.
     */
    protected static function subjectLink(ActivityLog $log, ?string $url = null): string
    {
        if ($url && $log->subject_id) {
            return '<a href="' . e($url) . '">' . e($log->subject_name) . '</a>';
        }

        return '<strong>' . e($log->subject_name ?? '(deleted)') . '</strong>';
    }

    /**
     * Render actor name.
     */
    protected static function actorName(ActivityLog $log): string
    {
        return $log->user ? e($log->user->displayName(false)) : 'System';
    }

    public static function toPlainText(ActivityLog $log): string
    {
        return strip_tags(static::toHtml($log));
    }
}