Skip to content

Services

Ringkasan

Services berisi business logic yang diekstrak dari controllers dan models. Berlokasi di app/Services/, diorganisir per domain.


Struktur Folder

app/Services/
├── Asset/          # Upload file + CDN (ImageKitService, CompressService, dll)
├── Codegen/        # Barcode + QR (GenerateBarcode, GenerateQRCode)
├── Common/         # Lintas domain (IdentifierAppService)
├── Otp/            # OTP (Generate, Verify)
└── Platform/       # Pipeline orchestration

Pattern

Single-Action Service

Satu class untuk satu aksi. Pakai method handle().

php
class Generate
{
    public static function handle(string $email, array $meta = []): Otp { ... }
}
// Panggil: Generate::handle($email)

Multi-Method Service

Satu class untuk beberapa operasi terkait.

php
class ImageKitService
{
    public function prepare(...) { ... }
    public function upload(...) { ... }
}
// Panggil: app(ImageKitService::class)->prepare(...)

Aturan

  • No HTTP concerns — service tidak sentuh request/response/session
  • Stateless — semua state di models atau stores
  • Injectable — constructor injection untuk dependencies
  • Testable — bisa di-test tanpa Laravel bootstrapping

File Penting

FileTujuan
app/Services/Asset/ImageKitService.phpUpload + CDN
app/Services/Otp/Generate.phpGenerate OTP
app/Services/Common/IdentifierAppService.phpPengecekan state aplikasi