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;
}
}