Áp dụng Builder (Manager) Pattern trong Laravel để quản lý bên thứ 3 cung cấp dịch vụ

Nội dung

Bài toán đặt ra

Trong rất nhiều ứng dụng chúng ta sẽ phải sử dụng dịch vụ của bên thứ 3. Ở đây tôi ví dụ dịch vụ gửi OTP qua SMS chẳng hạn. Để đảm bảo hệ thống chạy ngon lành và không phụ thuộc vào sự cố của nhà cung cấp, chúng ta nên sử dụng ít nhất 02 nhà cung cấp dịch vụ. Trong bài viết này, tôi giả định có 3 nhà cung cấp OTP qua SMS là Abenla, Esms và Nexmo. Làm thế nào để quản lý các nhà cung cấp dịch vụ này trong Laravel?

Giải pháp

Trong Laravel vấn đề trên có thể được giải quyết thông qua Builder (Manager) Pattern. Các bạn tham khảo thêm từ bài viết Builder (Manager) Pattern.

Triển khai

Bước 1: Tạo thư mục quản lý các dịch vụ

Tạo một class OtpManager theo đường dẫn app/Managers/OtpManager.php để quản lý OTP. Nội dung file OtpManager.php

<?php

namespace App\Managers;

use App\Services\Otp\AbenlaOtpService;
use App\Services\Otp\EsmsOtpService;
use App\Services\Otp\NexmoOtpService;
use Illuminate\Support\Manager;

class OtpManager extends Manager
{

    /**
     * @inheritDoc
     */
    public function getDefaultDriver()
    {
        // TODO: Implement getDefaultDriver() method.
        return 'abenla';
    }

    /**
     * Driver Abenla
     *
     * @return AbenlaOtpService
     */
    public function createAbenlaDriver()
    {
        return new AbenlaOtpService();
    }

    /**
     * Driver esms
     *
     * @return EsmsOtpService
     */
    public function createEsmsDriver()
    {
        return new EsmsOtpService();
    }

    /**
     * Driver nexmo
     *
     * @return NexmoOtpService
     */
    public function createNexmoDriver()
    {
        return new NexmoOtpService();
    }
}

Trong đó, hàm getDefaultDriver() sẽ trả về nhà cung cấp mặc định khi ta không truyền vào. Các hàm createAbenlaDriver(), createEsmsDriver() và createNexmoDriver() sẽ được tự động kích hoạt khi ta gọi nhà cung cấp tương ứng. Như vậy, trong mỗi hàm trên sẽ xử lý nghiệp vụ tương ứng với từng nhà cung cấp. Khi có nhà cung cấp mới, các bạn bổ sung thêm hàm vào để giao nhiệm vụ cho class tương ứng xử lý. Cụ thể:

  • Khi bạn muốn gửi OTP bởi nhà cung cấp Abenla thì class AbenlaOtpService sẽ đảm nhiệm
  • Khi bạn muốn gửi OTP bởi nhà cung cấp Esms thì class EsmsOtpService sẽ đảm nhiệm
  • Khi bạn muốn gửi OTP bởi nhà cung cấp Nexmo thì class NexmoOtpService sẽ đảm nhiệm

Thêm nhà cung cấp mới theo mẫu hàm create[Mã nhà cung cấp]Driver().

Bước 2: Tạo class xử lý nghiệp vụ theo từng nhà cung cấp

Các bạn có thể tổ chức thư mục để quản lý như ảnh minh họa

Trong đó, tôi có tạo 1 interface để quản lý các hàm nghiệp vụ chung. Nội dung file app/Services/Otp/OtpInterface.php tham khảo

<?php

namespace App\Services\Otp;

interface OtpInterface
{
    public function sendOtpByVoice($mobile, $otp, $options);
    public function sendOtpBySms($mobile, $otp, $options);
}

Class xử lý khi kết nối với nhà cung cấp Abenla, xem file app/Services/Otp/AbenlaOtpService.php

<?php

namespace App\Services\Otp;

class AbenlaOtpService implements OtpInterface
{

    public function sendOtpByVoice($mobile, $otp, $options)
    {
        // TODO: Implement sendOtpByVoice() method.
        return 'AbenlaOtpService:sendOtpByVoice:' . $mobile;
    }

    public function sendOtpBySms($mobile, $otp, $options)
    {
        // TODO: Implement sendOtpBySms() method.
        return 'AbenlaOtpService:sendOtpBySms:' . $mobile;
    }
}

Class xử lý khi kết nối với nhà cung cấp Esms app/Services/Otp/EsmsOtpService.php

<?php

namespace App\Services\Otp;

class EsmsOtpService implements OtpInterface
{

    public function sendOtpByVoice($mobile, $otp, $options)
    {
        // TODO: Implement sendOtpByVoice() method.
        return 'EsmsOtpService:sendOtpByVoice:'.$mobile;
    }

    public function sendOtpBySms($mobile, $otp, $options)
    {
        // TODO: Implement sendOtpBySms() method.
        return 'EsmsOtpService:sendOtpBySms:'.$mobile;
    }
}

Class xử lý khi kết nối với nhà cung cấp Nexmo app/Services/Otp/NexmoOtpService.php

<?php

namespace App\Services\Otp;

class NexmoOtpService implements OtpInterface
{

    public function sendOtpByVoice($mobile, $otp, $options)
    {
        // TODO: Implement sendOtpByVoice() method.
        return 'NexmoOtpService:sendOtpByVoice:'.$mobile;
    }

    public function sendOtpBySms($mobile, $otp, $options)
    {
        // TODO: Implement sendOtpBySms() method.
        return 'NexmoOtpService:sendOtpBySms:'.$mobile;
    }
}

Bước 3: Sử dụng

Bạn có thể tạo một artisan command để test theo đường dẫn app/Console/Commands/Dev/TestCommand.php. Trong hàm handle, ta viết như sau:

public function handle(OtpManager $otpManager)
    {
        // Manager
        //$result = $otpManager->driver('abenla')->sendOtpBySms('0912345678', '113115', []);
        //$result = $otpManager->driver('esms')->sendOtpBySms('0912345678', '113115', []);
        $result = $otpManager->driver('nexmo')->sendOtpBySms('0912345678', '113115', []);

        print_r($result);

    }

Khi dùng dịch vụ của nhà cung cấp nào Bạn chỉ cần truyền qua driver('nexmo') là xong.

Lưu ý: khi Bạn gọi driver('nexmo') thì Laravel sẽ tự động gọi đến hàm createNexmoDriver() trong file app/Managers/OtpManager.php để xử lý. Hàm này sẽ giao cho class NexmoOtpService() để xử lý nghiệp vụ cụ thể.

Hy vọng bài viết sẽ góp ích cho các Bạn!
Nguồn: Viblo

Anthony Nguyễn

Cây bút chính tại VietnamTutor

Bài viết cùng chuyên mục

Skills trong Claude Code: biến quy trình thành lệnh tái dùng

Hướng dẫn dùng skills trong Claude Code: cấu trúc SKILL.md, vị trí lưu cho cá nhân và team, kiểm soát ai được gọi skill, và các

Plan mode Claude Code: bắt AI lập kế hoạch trước khi viết code

Bài giải thích plan mode trong Claude Code: cách bật bằng Shift+Tab hoặc /plan, Claude được làm gì khi chỉ-đọc, checklist đọc plan trước khi duyệt,

Checkpoint trong Claude Code: quay lại khi AI sửa hỏng code

Bài giải thích cơ chế checkpoint của Claude Code, cách dùng /rewind để khôi phục code và hội thoại, những trường hợp checkpoint không cứu được,

Permission mode trong Claude Code: chọn mức quyền an toàn

Bài giải thích 6 permission mode của Claude Code, cách đổi chế độ bằng Shift+Tab và cờ khởi động, cùng nguyên tắc chọn mức quyền an

Sửa code bằng Claude Code: quy trình an toàn cho người mới

Bài hướng dẫn quy trình sửa code bằng Claude Code an toàn cho người mới: cách viết yêu cầu, duyệt quyền, kiểm tra thay đổi, kiểm

CLAUDE.md là gì? Cách ghi quy tắc dự án để Claude Code làm đúng

Bài giải thích CLAUDE.md là gì, file này load từ đâu, nên ghi nội dung nào và cách bảo trì để Claude Code làm đúng quy

7 lệnh Claude Code cần biết: /help, /clear, /compact và cách dùng

Bài hướng dẫn 7 lệnh Claude Code quan trọng nhất cho người mới bắt đầu, kèm ví dụ thực tế và thời điểm nên dùng từng

Claude Code là gì? Hướng dẫn cài đặt và chạy dự án đầu tiên

Bài giải thích Claude Code là gì, cách cài đặt đúng trên macOS, Windows và Linux, cùng quy trình chạy dự án đầu tiên an toàn

Prompt vibe coding: mẫu brief để AI hiểu rõ sản phẩm

Prompt vibe coding hiệu quả bắt đầu từ một brief rõ, không phải một câu lệnh dài. Mẫu này giúp bạn mô tả sản phẩm, giới

WordPress cho developer 2026: những thay đổi cần kiểm tra

Bài viết tóm tắt các thay đổi WordPress cho developer 2026 đáng chú ý trong tháng 5: Gutenberg, REST API, block editor, theme và kiểm thử

@wordpress/build là gì? Tooling mới cho plugin WordPress

@wordpress/build là hướng tooling mới cho plugin WordPress: build nhanh hơn, convention-based, hỗ trợ script modules và tự tạo file PHP registration.

Docker sandbox AI coding agent: cấu hình an toàn

Bài viết hướng dẫn lập trình viên cấu hình Docker sandbox AI coding agent để giảm rủi ro khi agent đọc repo, sửa file và chạy