Files
voltcerto/app/Traits/ResourcesCache.php
Luis Medon e488ae3342 Add feature tests, unit tests, and configuration for Laravel application
- Created ExampleTest and RoutesTest for basic application response and route testing.
- Implemented IsolatedDatabaseTestCase for isolated database testing.
- Added unit tests for various models and services including Contract, CPE, and LeadDetail.
- Developed request validation tests for CreateClientRequest, UpdateClientRequest, and ContractRequest.
- Introduced Vite configuration for asset management in the Laravel application.
2026-10-29 11:30:06 +00:00

131 lines
2.9 KiB
PHP

<?php
namespace App\Traits;
use Illuminate\Support\Facades\Cache;
use App\Models\{
CuiTier, Clients, ContractStatus,
ContractType, PaymentMethod,
PowerTier, Supplier, Typology, User
};
/**
* Trait ResourcesCache
*
* This trait provides cached access to frequently used resources
* such as contract statuses, contract types, payment methods,
* suppliers, and others. It helps to avoid hitting the database
* repeatedly by caching results for a configurable period of time.
*/
trait ResourcesCache
{
/**
* Defines the cache lifetime in seconds.
*
* It retrieves the session lifetime from the configuration
* and converts it into seconds (minutes * 60).
*
* @return int
*/
private function cacheTime(): int
{
return config('session.lifetime') * 60;
}
/**
* Get all contract statuses, cached.
*/
public function _getContractStatuses()
{
return Cache::remember(
'contract_statuses',
$this->cacheTime(),
fn() => ContractStatus::all()
);
}
/**
* Get active contract types, cached.
*/
public function _getContractTypes()
{
return Cache::remember(
'contract_types',
$this->cacheTime(),
fn() => ContractType::where('status', 'A')->get()
);
}
/**
* Get active payment methods, cached.
*/
public function _getPaymentMethods()
{
return Cache::remember(
'payment_methods',
$this->cacheTime(),
fn() => PaymentMethod::where('status', 'A')->get()
);
}
/**
* Get active power tiers, cached.
*/
public function _getPowerTiers()
{
return Cache::remember(
'power_tiers',
$this->cacheTime(),
fn() => PowerTier::where('status', 'A')->get()
);
}
/**
* Get active CUI tiers, cached.
*/
public function _getCuiTiers()
{
return Cache::remember(
'cui_tiers',
$this->cacheTime(),
fn() => CuiTier::where('status', 'A')->get()
);
}
/**
* Get active suppliers, cached.
*/
public function _getSuppliers()
{
return Cache::remember(
'suppliers',
$this->cacheTime(),
fn() => Supplier::where('status', 'A')->get()
);
}
/**
* Get active typologies, cached.
*/
public function _getTypologies()
{
return Cache::remember(
'typologies',
$this->cacheTime(),
fn() => Typology::where('status', 'A')->get()
);
}
/**
* Get all sellers except the one with ID = 1, cached.
*/
public function _getSellers()
{
return Cache::remember(
'sellers',
$this->cacheTime(),
fn() => User::where('id', '!=', 1)->get()
);
}
}