PHP code example of aybimyazilim / dataport-sms-notification

1. Go to this page and download the library: Download aybimyazilim/dataport-sms-notification library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

aybimyazilim / dataport-sms-notification example snippets




namespace App\Notifications;

use AybimYazilim\DataPortSms\Notifications\DataPortSmsNotification;

class OrderConfirmation extends DataPortSmsNotification
{
    private $order;

    public function __construct($order)
    {
        $this->order = $order;
    }

    public function toDataPortSms($notifiable): array
    {
        return [
            'text' => "Siparişiniz #{$this->order->id} onaylandı!",
            'phone' => $notifiable->phone, // Opsiyonel
        ];
    }
}



namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;

class OrderConfirmation extends Notification implements ShouldQueue
{
    use Queueable;

    private $order;

    public function __construct($order)
    {
        $this->order = $order;
    }

    public function via($notifiable)
    {
        return ['dataport-sms'];
    }

    public function toDataPortSms($notifiable): array
    {
        return [
            'text' => "Siparişiniz #{$this->order->id} onaylandı! Toplam: {$this->order->total}₺",
            'phone' => $notifiable->phone, // Opsiyonel
        ];
    }
}

// User model üzerinden
$user = User::find(1);
$user->notify(new OrderConfirmation($order));

// Notification facade ile
use Illuminate\Support\Facades\Notification;

Notification::send($users, new OrderConfirmation($order));

// Belirli bir telefon numarasına
Notification::route('dataport-sms', '905551234567')
    ->notify(new OrderConfirmation($order));

class User extends Authenticatable
{
    // Telefon numarası için özel routing
    public function routeNotificationForDataPortSms()
    {
        return $this->phone_number;
    }
    
    // Veya telefon alanı için accessor
    public function getPhoneAttribute()
    {
        return $this->mobile ?? $this->telephone;
    }

    // SMS loglarına erişim
    public function smsLogs()
    {
        return $this->morphMany(SmsLog::class, 'notifiable');
    }
}

use AybimYazilim\DataPortSms\Messages\DataPortSmsMessage;

public function toDataPortSms($notifiable): array
{
    return (new DataPortSmsMessage('Mesaj içeriği'))
        ->to('905551234567')
        ->toArray();
}

// Queue konfigürasyonu
public function __construct($order)
{
    $this->order = $order;
    $this->onQueue('sms'); // Özel queue
    $this->delay(now()->addMinutes(5)); // Gecikme
}

use AybimYazilim\DataPortSms\Tests\TestCase;
use Illuminate\Support\Facades\Http;

class MyNotificationTest extends TestCase
{
    /** @test */
    public function it_sends_sms_notification()
    {
        Http::fake([
            'api.dataport.com.tr/*' => Http::response(['success' => true])
        ]);

        $user = new User(['phone' => '905551234567']);
        $user->notify(new MyNotification());

        Http::assertSent(function ($request) {
            return str_contains($request->url(), 'dataport.com.tr');
        });
    }
}

use AybimYazilim\DataPortSms\Exceptions\DataPortSmsException;
use AybimYazilim\DataPortSms\Exceptions\InvalidPhoneNumberException;
use AybimYazilim\DataPortSms\Exceptions\QuotaExceededException;

try {
    $user->notify(new OrderConfirmation($order));
} catch (InvalidPhoneNumberException $e) {
    Log::error('Geçersiz telefon numarası: ' . $e->getMessage());
} catch (QuotaExceededException $e) {
    Log::error('SMS kotası dolmuş: ' . $e->getMessage());
} catch (DataPortSmsException $e) {
    Log::error('SMS gönderilemedi: ' . $e->getMessage());
}

use AybimYazilim\DataPortSms\Models\SmsLog;

// Başarılı SMS'ler
$successfulSms = SmsLog::successful()->get();

// Başarısız SMS'ler
$failedSms = SmsLog::failed()->get();

// Bekleyen SMS'ler
$pendingSms = SmsLog::pending()->get();

// Belirli bir kullanıcının SMS'leri
$userSms = SmsLog::where('notifiable_type', User::class)
    ->where('notifiable_id', $userId)
    ->get();
bash
php artisan vendor:publish --tag=dataport-sms-config
bash
php artisan vendor:publish --tag=dataport-sms-migrations
php artisan migrate
bash
php artisan dataport:test-sms 905551234567 "Test mesajı"