wip
This commit is contained in:
67
app/Repositories/System/Cache/CacheRepository.php
Normal file
67
app/Repositories/System/Cache/CacheRepository.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\System\Cache;
|
||||
|
||||
class CacheRepository
|
||||
{
|
||||
/**
|
||||
* Cache name
|
||||
*/
|
||||
protected string $name;
|
||||
|
||||
/**
|
||||
* Value
|
||||
*/
|
||||
protected mixed $value;
|
||||
|
||||
/**
|
||||
* Time
|
||||
*/
|
||||
protected int $time;
|
||||
|
||||
/**
|
||||
* Cache repo
|
||||
*/
|
||||
public function __construct(string $name, mixed $value, int $time = 60)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->value = $value;
|
||||
$this->time = $time;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cache Repo
|
||||
*
|
||||
* @param string $name key
|
||||
* @param mixed $value value
|
||||
* @param int|int $time time in seconds
|
||||
*/
|
||||
public static function make(string $name, mixed $value, int $time = 600): mixed
|
||||
{
|
||||
$repo = new self($name, $value, $time);
|
||||
|
||||
return $repo->handle();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle cache
|
||||
*/
|
||||
public function handle(): mixed
|
||||
{
|
||||
return cache()->has($this->name)
|
||||
? cache($this->name)
|
||||
: cache()->remember(
|
||||
key: $this->name,
|
||||
ttl: $this->time,
|
||||
callback: fn () => is_callable($this->value) ? call_user_func($this->value) : $this->value
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Forget the key from cache
|
||||
*/
|
||||
public static function forget(string $name): void
|
||||
{
|
||||
cache()->forget($name);
|
||||
}
|
||||
}
|
||||
37
app/Repositories/System/Payments/PaymentTypeRepository.php
Normal file
37
app/Repositories/System/Payments/PaymentTypeRepository.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\System\Payments;
|
||||
|
||||
use App\Models\System\Settings\Payments\PaymentType;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class PaymentTypeRepository
|
||||
{
|
||||
protected string $model;
|
||||
|
||||
protected $queryBuilder;
|
||||
|
||||
/**
|
||||
* Payment Type Repository
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->model = PaymentType::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Syntatic sugar for new class
|
||||
*/
|
||||
public static function make(): self
|
||||
{
|
||||
return new self();
|
||||
}
|
||||
|
||||
/**
|
||||
* Pluck data
|
||||
*/
|
||||
public function pluck(): Collection|array
|
||||
{
|
||||
return PaymentType::pluck('name', 'id');
|
||||
}
|
||||
}
|
||||
23
app/Repositories/System/Role/RoleRepository.php
Normal file
23
app/Repositories/System/Role/RoleRepository.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\System\Role;
|
||||
|
||||
use App\Repositories\System\Cache\CacheRepository;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Laravel\Nova\Makeable;
|
||||
|
||||
class RoleRepository
|
||||
{
|
||||
use Makeable;
|
||||
|
||||
/**
|
||||
* Vendor id
|
||||
*/
|
||||
public function vendorId(): int
|
||||
{
|
||||
return CacheRepository::make(
|
||||
name: 'role-vendor-id',
|
||||
value: fn () => DB::table('roles')->where('name', 'vendor')->first(['id', 'name'])->id
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user