controller refactoring part 1

This commit is contained in:
Peter Papp
2021-07-20 08:58:20 +02:00
parent 29d1b68dd5
commit d6db2f3a7c
25 changed files with 717 additions and 546 deletions

View File

@@ -1,215 +0,0 @@
<?php
namespace Domain\Browsing\Controllers;
use App\Users\Models\User;
use Illuminate\Http\Request;
use Domain\Files\Models\File;
use Domain\Sharing\Models\Share;
use Domain\Folders\Models\Folder;
use Illuminate\Support\Collection;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Domain\Items\Requests\SearchRequest;
class BrowseController extends Controller
{
/**
* Get directory with files
*
* @param Request $request
* @param $id
* @return Collection
*/
public function folder(Request $request, $id)
{
$root_id = $id === 'undefined' ? null : $id;
// Get folder trash items
if ($request->query('trash')) {
// Get folders and files
$folders = Folder::onlyTrashed()
->with('parent')
->where('parent_id', $root_id)
->sortable()
->get();
$files = File::onlyTrashed()
->with('parent')
->where('folder_id', $root_id)
->sortable()
->get();
// Collect folders and files to single array
return collect([$folders, $files])->collapse();
}
// Get folders and files
$folders = Folder::with(['parent:id,name', 'shared:token,id,item_id,permission,is_protected,expire_in'])
->where('parent_id', $root_id)
->where('user_id', Auth::id())
->sortable()
->get();
$files = File::with(['parent:id,name', 'shared:token,id,item_id,permission,is_protected,expire_in'])
->where('folder_id', $root_id)
->where('user_id', Auth::id())
->sortable()
->get();
// Collect folders and files to single array
return collect([$folders, $files])
->collapse();
}
/**
* Get latest user uploads
*
* @return mixed
*/
public function latest()
{
$user = User::with(['latest_uploads' => function ($query) {
$query->sortable(['created_at' => 'desc']);
}])
->where('id', Auth::id())
->first();
return $user->latest_uploads;
}
/**
* Get trashed files
*
* @return Collection
*/
public function trash()
{
$user_id = Auth::id();
// Get folders and files
$folders_trashed = Folder::onlyTrashed()
->with(['trashed_folders', 'parent'])
->where('user_id', $user_id)
->get(['parent_id', 'id', 'name']);
$folders = Folder::onlyTrashed()
->with(['parent'])
->where('user_id', $user_id)
->whereIn('id', filter_folders_ids($folders_trashed))
->sortable()
->get();
// Get files trashed
$files_trashed = File::onlyTrashed()
->with(['parent'])
->where('user_id', $user_id)
->where(function ($query) use ($folders_trashed) {
$query->whereNull('folder_id');
$query->orWhereNotIn('folder_id', array_values(array_unique(recursiveFind($folders_trashed->toArray(), 'id'))));
})
->sortable()
->get();
// Collect folders and files to single array
return collect([$folders, $files_trashed])
->collapse();
}
/**
* Get user shared items
*
* @return Collection
*/
public function shared()
{
$user_id = Auth::id();
// Get shared folders and files
$folder_ids = Share::where('user_id', $user_id)
->where('type', 'folder')
->pluck('item_id');
$file_ids = Share::where('user_id', $user_id)
->where('type', '!=', 'folder')
->pluck('item_id');
// Get folders and files
$folders = Folder::with(['parent', 'shared:token,id,item_id,permission,is_protected,expire_in'])
->where('user_id', $user_id)
->whereIn('id', $folder_ids)
->sortable()
->get();
$files = File::with(['parent', 'shared:token,id,item_id,permission,is_protected,expire_in'])
->where('user_id', $user_id)
->whereIn('id', $file_ids)
->sortable()
->get();
// Collect folders and files to single array
return collect([$folders, $files])
->collapse();
}
/**
* Get participant uploads
*
* @return mixed
*/
public function participant_uploads()
{
return File::with(['parent'])
->where('user_id', Auth::id())
->whereAuthor('visitor')
->sortable()
->get();
}
/**
* Get user folder tree
*
* @return array
*/
public function navigation_tree()
{
$folders = Folder::with('folders:id,parent_id,id,name')
->where('parent_id', null)
->where('user_id', Auth::id())
->sortable()
->get(['id', 'parent_id', 'id', 'name']);
return [
[
'name' => __t('home'),
'location' => 'base',
'folders' => $folders,
],
];
}
/**
* Search files
*
* @param SearchRequest $request
* @return Collection
*/
public function search(SearchRequest $request)
{
$user_id = Auth::id();
$query = remove_accents($request->input('query'));
// Search files id db
$searched_files = File::search($query)
->where('user_id', $user_id)
->get();
$searched_folders = Folder::search($query)
->where('user_id', $user_id)
->get();
// Collect folders and files to single array
return collect([$searched_folders, $searched_files])
->collapse();
}
}

View File

@@ -0,0 +1,54 @@
<?php
namespace Domain\Browsing\Controllers;
use Illuminate\Http\Request;
use Domain\Files\Models\File;
use Domain\Folders\Models\Folder;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
class BrowseFolderContentController
{
public function __invoke(
Request $request,
string $id,
): Collection {
$root_id = $id === 'undefined' ? null : $id;
// Get folder trash items
if ($request->query('trash')) {
// Get folders and files
$folders = Folder::onlyTrashed()
->with('parent')
->where('parent_id', $root_id)
->sortable()
->get();
$files = File::onlyTrashed()
->with('parent')
->where('folder_id', $root_id)
->sortable()
->get();
// Collect folders and files to single array
return collect([$folders, $files])->collapse();
}
// Get folders and files
$folders = Folder::with(['parent:id,name', 'shared:token,id,item_id,permission,is_protected,expire_in'])
->where('parent_id', $root_id)
->where('user_id', Auth::id())
->sortable()
->get();
$files = File::with(['parent:id,name', 'shared:token,id,item_id,permission,is_protected,expire_in'])
->where('folder_id', $root_id)
->where('user_id', Auth::id())
->sortable()
->get();
// Collect folders and files to single array
return collect([$folders, $files])
->collapse();
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace Domain\Browsing\Controllers;
use App\Users\Models\User;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
class BrowseLatestFilesController
{
public function __invoke(): Collection
{
$user = User::with([
'latestUploads' => fn ($query) => $query->sortable(['created_at' => 'desc'])
])
->where('id', Auth::id())
->first();
return $user->latestUploads;
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Domain\Browsing\Controllers;
use Domain\Files\Models\File;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
class BrowseParticipantsUploadsController
{
public function __invoke(): Collection
{
return File::with(['parent'])
->where('user_id', Auth::id())
->whereAuthor('visitor')
->sortable()
->get();
}
}

View File

@@ -0,0 +1,42 @@
<?php
namespace Domain\Browsing\Controllers;
use Domain\Files\Models\File;
use Domain\Sharing\Models\Share;
use Domain\Folders\Models\Folder;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
class BrowseSharedItemsController
{
public function __invoke(): Collection
{
$user_id = Auth::id();
// Get shared folders and files
$folder_ids = Share::where('user_id', $user_id)
->where('type', 'folder')
->pluck('item_id');
$file_ids = Share::where('user_id', $user_id)
->where('type', '!=', 'folder')
->pluck('item_id');
// Get folders and files
$folders = Folder::with(['parent', 'shared:token,id,item_id,permission,is_protected,expire_in'])
->where('user_id', $user_id)
->whereIn('id', $folder_ids)
->sortable()
->get();
$files = File::with(['parent', 'shared:token,id,item_id,permission,is_protected,expire_in'])
->where('user_id', $user_id)
->whereIn('id', $file_ids)
->sortable()
->get();
// Collect folders and files to single array
return collect([$folders, $files])
->collapse();
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace Domain\Browsing\Controllers;
use Domain\Files\Models\File;
use Domain\Folders\Models\Folder;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
class BrowseTrashContentController
{
public function __invoke(): Collection
{
$user_id = Auth::id();
// Get folders and files
$folders_trashed = Folder::onlyTrashed()
->with(['trashedFolders', 'parent'])
->where('user_id', $user_id)
->get(['parent_id', 'id', 'name']);
$folders = Folder::onlyTrashed()
->with(['parent'])
->where('user_id', $user_id)
->whereIn('id', filter_folders_ids($folders_trashed))
->sortable()
->get();
// Get files trashed
$files_trashed = File::onlyTrashed()
->with(['parent'])
->where('user_id', $user_id)
->where(function ($query) use ($folders_trashed) {
$query->whereNull('folder_id');
$query->orWhereNotIn('folder_id', array_values(array_unique(recursiveFind($folders_trashed->toArray(), 'id'))));
})
->sortable()
->get();
// Collect folders and files to single array
return collect([$folders, $files_trashed])
->collapse();
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace Domain\Browsing\Controllers;
use Domain\Files\Models\File;
use Domain\Folders\Models\Folder;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Domain\Items\Requests\SearchRequest;
class SearchFilesAndFoldersController
{
public function __invoke(
SearchRequest $request
): Collection {
$user_id = Auth::id();
$query = remove_accents(
$request->input('query')
);
// Search files id db
$searched_files = File::search($query)
->where('user_id', $user_id)
->get();
$searched_folders = Folder::search($query)
->where('user_id', $user_id)
->get();
// Collect folders and files to single array
return collect([$searched_folders, $searched_files])
->collapse();
}
}

View File

@@ -79,7 +79,7 @@ class FileAccessController extends Controller
}*/
// Store user download size
$request->user()->record_download(
$request->user()->recordDownload(
(int) $file->getRawOriginal('filesize')
);
@@ -102,7 +102,7 @@ class FileAccessController extends Controller
$zip
->user
->record_download(
->recordDownload(
$disk->size("zip/$zip->basename")
);

View File

@@ -12,6 +12,8 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use TeamTNT\TNTSearch\Indexer\TNTIndexer;
use \Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;
/**
@@ -20,9 +22,12 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
*/
class File extends Model
{
use Searchable, SoftDeletes, Sortable, HasFactory;
use Searchable;
use SoftDeletes;
use Sortable;
use HasFactory;
public $public_access = null;
public ?string $public_access = null;
protected $guarded = [
'id',
@@ -40,11 +45,6 @@ class File extends Model
'author_id',
];
/**
* Sortable columns
*
* @var string[]
*/
public $sortable = [
'name',
'created_at',
@@ -61,54 +61,50 @@ class File extends Model
/**
* Set routes with public access
*
* @param $token
*/
public function setPublicUrl($token)
public function setPublicUrl(string $token)
{
$this->public_access = $token;
}
/**
* Format created at date
*
* @return string
*/
public function getCreatedAtAttribute()
public function getCreatedAtAttribute(): string
{
return format_date(set_time_by_user_timezone($this->attributes['created_at']), __t('time'));
return format_date(
set_time_by_user_timezone($this->attributes['created_at']),
__t('time')
);
}
/**
* Form\a\t created at date reformat
*
* @return string|null
* Format deleted at date reformat
*/
public function getDeletedAtAttribute()
public function getDeletedAtAttribute(): string | null
{
if (! $this->attributes['deleted_at']) {
return null;
}
return format_date(set_time_by_user_timezone($this->attributes['deleted_at']), __t('time'));
return format_date(
set_time_by_user_timezone($this->attributes['deleted_at']),
__t('time')
);
}
/**
* Format fileSize
*
* @return string
*/
public function getFilesizeAttribute()
public function getFilesizeAttribute(): string
{
return Metric::bytes($this->attributes['filesize'])->format();
}
/**
* Format thumbnail url
*
* @return string|null
*/
public function getThumbnailAttribute()
public function getThumbnailAttribute(): string | null
{
// Get thumbnail from external storage
if ($this->attributes['thumbnail'] && ! is_storage_driver(['local'])) {
@@ -132,10 +128,8 @@ class File extends Model
/**
* Format file url
*
* @return string
*/
public function getFileUrlAttribute()
public function getFileUrlAttribute(): string
{
// Get file from external storage
if (! is_storage_driver(['local'])) {
@@ -166,10 +160,8 @@ class File extends Model
/**
* Index file
*
* @return array
*/
public function toSearchableArray()
public function toSearchableArray(): array
{
$array = $this->toArray();
$name = Str::slug($array['name'], ' ');
@@ -181,33 +173,21 @@ class File extends Model
];
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function parent()
public function parent(): BelongsTo
{
return $this->belongsTo(Folder::class, 'folder_id', 'id');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function folder()
public function folder(): HasOne
{
return $this->hasOne(Folder::class, 'id', 'folder_id');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function shared()
public function shared(): HasOne
{
return $this->hasOne(Share::class, 'item_id', 'id');
}
/**
* Model events
*/
protected static function boot()
{
parent::boot();

View File

@@ -0,0 +1,25 @@
<?php
namespace Domain\Folders\Controllers;
use Domain\Folders\Models\Folder;
use Illuminate\Support\Facades\Auth;
class NavigationFolderTreeController
{
public function __invoke(): array
{
$folders = Folder::with('folders:id,parent_id,id,name')
->where('parent_id', null)
->where('user_id', Auth::id())
->sortable()
->get(['id', 'parent_id', 'id', 'name']);
return [
[
'name' => __t('home'),
'location' => 'base',
'folders' => $folders,
],
];
}
}

View File

@@ -10,6 +10,9 @@ use Database\Factories\FolderFactory;
use Illuminate\Database\Eloquent\Model;
use TeamTNT\TNTSearch\Indexer\TNTIndexer;
use \Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;
/**
@@ -37,11 +40,6 @@ class Folder extends Model
'author_id',
];
/**
* Sortable columns
*
* @var string[]
*/
public $sortable = [
'name',
'created_at',
@@ -56,17 +54,15 @@ class Folder extends Model
return FolderFactory::new();
}
public function getTypeAttribute()
public function getTypeAttribute(): string
{
return 'folder';
}
/**
* Index folder
*
* @return array
*/
public function toSearchableArray()
public function toSearchableArray(): array
{
$array = $this->toArray();
$name = Str::slug($array['name'], ' ');
@@ -80,135 +76,130 @@ class Folder extends Model
/**
* Counts how many folder have items
*
* @return int
*/
public function getItemsAttribute()
public function getItemsAttribute(): int
{
$folders = $this->folders()->count();
$files = $this->files()->count();
$folders = $this->folders()
->count();
$files = $this->files()
->count();
return $folders + $files;
}
/**
* Counts how many folder have items
*
* @return int
*/
public function getTrashedItemsAttribute()
public function getTrashedItemsAttribute(): int
{
$folders = $this->trashed_folders()->count();
$files = $this->trashed_files()->count();
$folders = $this->trashedFolders()
->count();
$files = $this->trashedFiles()
->count();
return $folders + $files;
}
/**
* Format created at date reformat
*
* @return string
*/
public function getCreatedAtAttribute()
public function getCreatedAtAttribute(): string
{
return format_date(set_time_by_user_timezone($this->attributes['created_at']), __t('time'));
return format_date(
set_time_by_user_timezone($this->attributes['created_at']),
__t('time')
);
}
/**
* Format created at date reformat
*
* @return string|null
* Format deleted at date reformat
*/
public function getDeletedAtAttribute()
public function getDeletedAtAttribute(): string | null
{
if (! $this->attributes['deleted_at']) {
return null;
}
return format_date(set_time_by_user_timezone($this->attributes['deleted_at']), __t('time'));
return format_date(
set_time_by_user_timezone($this->attributes['deleted_at']),
__t('time')
);
}
/**
* Get parent
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function parent()
public function parent(): BelongsTo
{
return $this->belongsTo(Folder::class, 'parent_id', 'id');
}
public function folderIds()
{
return $this->children()->with('folderIds')->select(['id', 'parent_id']);
return $this->children()
->with('folderIds')
->select(['id', 'parent_id']);
}
/**
* Get all files
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function files()
public function files(): HasMany
{
return $this->hasMany(File::class, 'folder_id', 'id');
}
/**
* Get all trashed files
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function trashed_files()
public function trashedFiles(): HasMany
{
return $this->hasMany(File::class, 'folder_id', 'id')->withTrashed();
return $this->hasMany(File::class, 'folder_id', 'id')
->withTrashed();
}
/**
* Get all folders
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function folders()
public function folders(): HasMany
{
return $this->children()->with('folders');
}
/**
* Get all trashed folders
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function trashed_folders()
public function trashedFolders(): HasMany
{
return $this->children()->with('trashed_folders')->withTrashed()->select(['parent_id', 'id', 'name']);
return $this->children()
->with('trashedFolders')
->withTrashed()
->select(['parent_id', 'id', 'name']);
}
/**
* Get childrens
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
* Get children
*/
public function children()
public function children(): HasMany
{
return $this->hasMany(Folder::class, 'parent_id', 'id');
}
/**
* Get trashed childrens
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
* Get trashed children
*/
public function trashed_children()
public function trashedChildren(): HasMany
{
return $this->hasMany(Folder::class, 'parent_id', 'id')->withTrashed();
return $this->hasMany(Folder::class, 'parent_id', 'id')
->withTrashed();
}
/**
* Get sharing attributes
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function shared()
public function shared(): HasOne
{
return $this->hasOne(Share::class, 'item_id', 'id');
}
@@ -224,7 +215,7 @@ class Folder extends Model
static::deleting(function ($item) {
if ($item->isForceDeleting()) {
$item->trashed_children()->each(function ($folder) {
$item->trashedChildren()->each(function ($folder) {
$folder->forceDelete();
});
} else {
@@ -240,12 +231,12 @@ class Folder extends Model
static::restoring(function ($item) {
// Restore children folders
$item->trashed_children()->each(function ($folder) {
$item->trashedChildren()->each(function ($folder) {
$folder->restore();
});
// Restore children files
$item->trashed_files()->each(function ($files) {
$item->trashedFiles()->each(function ($files) {
$files->restore();
});
});

View File

@@ -45,7 +45,7 @@ class BrowseShareController extends Controller
// Store user download size
$shared
->user
->record_download(
->recordDownload(
(int) $image->getRawOriginal('filesize')
);

View File

@@ -32,7 +32,7 @@ class FileSharedAccessController extends Controller
$zip
->user
->record_download(
->recordDownload(
$disk->size("zip/$zip->basename")
);
@@ -69,7 +69,7 @@ class FileSharedAccessController extends Controller
// Store user download size
$shared
->user
->record_download(
->recordDownload(
(int) $file->getRawOriginal('filesize')
);
@@ -99,7 +99,7 @@ class FileSharedAccessController extends Controller
// Store user download size
$shared
->user
->record_download(
->recordDownload(
(int) $file->getRawOriginal('filesize')
);

View File

@@ -0,0 +1,44 @@
<?php
namespace Domain\Subscriptions\Traits;
use App\Users\Models\UserSettings;
use Domain\Subscriptions\Services\StripeService;
trait Subscription
{
/**
* Get tax rate id for user
*/
public function userTaxRates(): array
{
// Get tax rates
$rates = collect(
resolve(StripeService::class)->getTaxRates()
);
// Find tax rate
$user_tax_rate = $rates->first(function ($item) {
return $item['country'] === $this->settings->country && $item['active'];
});
return $user_tax_rate ? [$user_tax_rate['id']] : [];
}
/**
* Set user billing info into user settings table
*/
public function setBilling($billing): UserSettings
{
$this->settings()->update([
'address' => $billing['billing_address'],
'city' => $billing['billing_city'],
'country' => $billing['billing_country'],
'name' => $billing['billing_name'],
'phone_number' => $billing['billing_phone_number'],
'postal_code' => $billing['billing_postal_code'],
'state' => $billing['billing_state'],
]);
return $this->settings;
}
}

View File

@@ -5,6 +5,9 @@ use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
/**
* @method static whereYear(string $string, string $string1, int $year)
*/
class Traffic extends Model
{
use HasFactory;
@@ -19,9 +22,6 @@ class Traffic extends Model
protected $keyType = 'string';
/**
* Model events
*/
protected static function boot()
{
parent::boot();