mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-07-17 06:15:20 +00:00
The app/Services/ directory had grown into 22 flat files at the root plus 7 uneven subdirectories — finding anything required scrolling through an alphabetical mix of small CRUD services, infrastructure drivers, and install-time utilities. This commit groups services by domain, folds Backup into a new Storage namespace, and moves framework-infrastructure and install-time helpers out of Services and into Support where they belong. New Services layout: Documents/ (Invoice, Estimate, RecurringInvoice, Payment, Expense, Transaction, DocumentItem, SerialNumber, Currency — matches the 'Documents' navigation group); Company/ (Company, Member, Invitation); Mail/ (MailConfiguration, CompanyMailConfig); Storage/ (FileDisk, plus Backup folded in). ExchangeRateProviderService moves next to its drivers in ExchangeRate/; FontService moves into Pdf/ where it belongs. CustomerService, ItemService, CustomFieldService stay at the Services root as standalone single-file domains. Moves to Support/: Hashids/ (library wrapper — not business logic); Setup/ (one-shot install-time utilities — stateless helpers); Pdf/ (ImageUtils, PdfTemplateUtils, plus the existing PdfHtmlSanitizer consolidated into the same subdir). These are all framework infrastructure and stateless utilities — the 'service' label never really fit them. Namespace declarations in 29 moved files updated to match new paths. 62 consumer files (controllers, other services, tests, database factories, seeders, routes, bootstrap/providers.php) have their use statements rewritten via a literal-string replacement script — no regex meant no risk of half-matching. Three Documents services needed an explicit 'use App\Services\Mail\CompanyMailConfigService' added because the same-namespace short reference they relied on no longer resolves after the split. Verified: composer dump-autoload, 350 tests pass (850 assertions), vendor/bin/pint clean, npm run build succeeds.
156 lines
4.3 KiB
PHP
156 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Services\Storage\FileDiskService;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class FileDisk extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
public const DISK_TYPE_SYSTEM = 'SYSTEM';
|
|
|
|
public const DISK_TYPE_REMOTE = 'REMOTE';
|
|
|
|
protected $guarded = [
|
|
'id',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'set_as_default' => 'boolean',
|
|
];
|
|
}
|
|
|
|
public function setCredentialsAttribute(mixed $value): void
|
|
{
|
|
$this->attributes['credentials'] = json_encode($value);
|
|
}
|
|
|
|
/**
|
|
* Decode credentials, handling double-encoded JSON from legacy data.
|
|
*/
|
|
public function getDecodedCredentials(): Collection
|
|
{
|
|
$decoded = json_decode($this->credentials, true);
|
|
|
|
// Handle double-encoded JSON (string inside string)
|
|
if (is_string($decoded)) {
|
|
$decoded = json_decode($decoded, true);
|
|
}
|
|
|
|
return collect($decoded ?? []);
|
|
}
|
|
|
|
public function scopeWhereOrder($query, $orderByField, $orderBy)
|
|
{
|
|
$query->orderBy($orderByField, $orderBy);
|
|
}
|
|
|
|
public function scopeFileDisksBetween($query, $start, $end)
|
|
{
|
|
return $query->whereBetween(
|
|
'file_disks.created_at',
|
|
[$start->format('Y-m-d'), $end->format('Y-m-d')]
|
|
);
|
|
}
|
|
|
|
public function scopeWhereSearch($query, $search)
|
|
{
|
|
foreach (explode(' ', $search) as $term) {
|
|
$query->where('name', 'LIKE', '%'.$term.'%')
|
|
->orWhere('driver', 'LIKE', '%'.$term.'%');
|
|
}
|
|
}
|
|
|
|
public function scopePaginateData($query, $limit)
|
|
{
|
|
if ($limit == 'all') {
|
|
return $query->get();
|
|
}
|
|
|
|
return $query->paginate($limit);
|
|
}
|
|
|
|
public function scopeApplyFilters($query, array $filters)
|
|
{
|
|
$filters = collect($filters);
|
|
if ($filters->get('search')) {
|
|
$query->whereSearch($filters->get('search'));
|
|
}
|
|
|
|
if ($filters->get('from_date') && $filters->get('to_date')) {
|
|
$start = Carbon::createFromFormat('Y-m-d', $filters->get('from_date'));
|
|
$end = Carbon::createFromFormat('Y-m-d', $filters->get('to_date'));
|
|
$query->fileDisksBetween($start, $end);
|
|
}
|
|
|
|
if ($filters->get('orderByField') || $filters->get('orderBy')) {
|
|
$field = $filters->get('orderByField') ? $filters->get('orderByField') : 'sequence_number';
|
|
$orderBy = $filters->get('orderBy') ? $filters->get('orderBy') : 'asc';
|
|
$query->whereOrder($field, $orderBy);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Apply this disk's credentials to the filesystem configuration at runtime.
|
|
*
|
|
* @deprecated Use FileDiskService::registerDisk() instead — setConfig() mutates filesystems.default.
|
|
*/
|
|
public function setConfig(): void
|
|
{
|
|
$service = app(FileDiskService::class);
|
|
$diskName = $service->registerDisk($this);
|
|
config(['filesystems.default' => $diskName]);
|
|
}
|
|
|
|
/**
|
|
* Determine whether this disk is configured as the default storage disk.
|
|
*/
|
|
public function setAsDefault(): bool
|
|
{
|
|
return $this->set_as_default;
|
|
}
|
|
|
|
/**
|
|
* Register a dynamic filesystem disk in the runtime configuration using the given credentials.
|
|
*
|
|
* @deprecated Use FileDisk::find($id)->registerDisk() instead.
|
|
*/
|
|
public static function setFilesystem(Collection $credentials, string $driver): void
|
|
{
|
|
$prefix = env('DYNAMIC_DISK_PREFIX', 'temp_');
|
|
|
|
config(['filesystems.default' => $prefix.$driver]);
|
|
|
|
$disks = config('filesystems.disks.'.$driver);
|
|
|
|
foreach ($disks as $key => $value) {
|
|
if ($credentials->has($key)) {
|
|
$disks[$key] = $credentials[$key];
|
|
}
|
|
}
|
|
|
|
if ($driver === 'local' && isset($disks['root']) && ! str_starts_with($disks['root'], '/')) {
|
|
$disks['root'] = storage_path('app/'.$disks['root']);
|
|
}
|
|
|
|
config(['filesystems.disks.'.$prefix.$driver => $disks]);
|
|
}
|
|
|
|
public function isSystem(): bool
|
|
{
|
|
return $this->type === self::DISK_TYPE_SYSTEM;
|
|
}
|
|
|
|
public function isRemote(): bool
|
|
{
|
|
return $this->type === self::DISK_TYPE_REMOTE;
|
|
}
|
|
}
|