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/ai.naniguide.com/app/Services/AssetService.php
<?php

namespace App\Services;

use App\Model\Customer;
use App\Model\Campaign;
use App\Model\AutomationEmail;
use App\Model\Attachment;
use App\Model\Asset;
use Symfony\Component\HttpFoundation\File\File;
use App\Library\Storage\StoragePathResolver; // Resolves the storage path, for example: "customers/{id}/attachments/{filename}"

/**
 * This service encapsulates CREATE operations related to media and attachment storage.
 *
 * Architectural note:
 * - In alignment with a DDD-inspired Laravel architecture, only CREATE operations
 *   should reside here.
 * - READ and UPDATE operations should belong to the entity models themselves,
 *   although they might depend on infrastructure components (e.g., file storage engines).
 */
class AssetService
{
    /**
     * Upload an attachment for a given campaign or email.
     *
     * The file is stored under the corresponding customer's directory, for example:
     * storage/app/customers/{id}/home/attachments/{filename}
     *
     * @param  Campaign|AutomationEmail  $campaignOrEmail
     * @param  File            $file
     * @return Attachment
     */
    public function uploadAttachment(Campaign|AutomationEmail $campaignOrEmail, File $file): Attachment
    {
        // Step 1: Create the database record
        $attachment = Attachment::makeForCampaignOrEmail($campaignOrEmail, $file);
        $attachment->save();

        // Step 2: Store the physical file
        $attachment->store($file);

        return $attachment;
    }

    /**
     * Upload a media asset belonging to a specific customer.
     *
     * The file is stored under the customer's folder, for example:
     * storage/app/customers/{id}/home/media/{filename}
     *
     * @param  Customer  $customer
     * @param  File      $file
     * @return Asset
     */
    public function uploadMedia(Customer $customer, File $file): Asset
    {
        // Step 1: Create the database record
        $media = Asset::makeMedia($file)->forCustomer($customer);
        $media->save();

        // Step 2: Store the physical file
        $media->store($file);

        return $media;
    }

    /**
     * Upload a media asset that is shared application-wide.
     *
     * The file is stored under the public media directory, for example:
     * storage/app/public/media/{filename}
     *
     * @param  File  $file
     * @return Asset
     */
    public function uploadApplicationMedia(File $file): Asset
    {
        // Step 1: Create the database record
        $media = Asset::makeMedia($file)->forApplication();
        $media->save();

        // Step 2: Store the physical file
        $media->store($file);

        return $media;
    }
}