PHP code example of azaharizaman / nexus-connector

1. Go to this page and download the library: Download azaharizaman/nexus-connector 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/ */

    

azaharizaman / nexus-connector example snippets


use Nexus\Connector\Contracts\EmailServiceConnectorInterface;

class NotificationService
{
    public function __construct(
        private readonly EmailServiceConnectorInterface $emailConnector
    ) {}

    public function sendWelcomeEmail(string $recipient): void
    {
        $this->emailConnector->sendTransactionalEmail(
            recipient: $recipient,
            subject: 'Welcome to Nexus',
            body: 'Thank you for joining us!'
        );
    }
}

use Nexus\Connector\Services\ConnectorManager;
use Nexus\Connector\ValueObjects\{Endpoint, HttpMethod, RateLimitConfig, IdempotencyKey};

class PaymentService
{
    public function __construct(
        private readonly ConnectorManager $connector
    ) {}

    public function processPayment(float $amount, string $currency, array $paymentData): array
    {
        // Create endpoint with rate limiting and custom timeout
        $endpoint = Endpoint::create('https://api.stripe.com/v1/charges', HttpMethod::POST)
            ->withRateLimit(RateLimitConfig::perSecond(100))
            ->withTimeout(45);

        // Generate idempotency key to prevent duplicate charges
        $idempotencyKey = IdempotencyKey::generate('payment');

        // Execute with automatic retry, circuit breaker, and OAuth refresh
        return $this->connector->execute(
            serviceName: 'stripe',
            endpoint: $endpoint,
            payload: [
                'amount' => (int)($amount * 100),
                'currency' => $currency,
                'source' => $paymentData['token'],
            ],
            idempotencyKey: $idempotencyKey
        );
    }
}

// config/connector.php
return [
    'email_vendor' => env('EMAIL_VENDOR', 'mailchimp'), // or 'sendgrid'
];

// app/Providers/ConnectorServiceProvider.php
$vendor = config('connector.email_vendor');

$adapter = match($vendor) {
    'mailchimp' => MailchimpEmailAdapter::class,
    'sendgrid' => SendGridEmailAdapter::class,
    default => throw new \Exception("Unsupported email vendor: {$vendor}"),
};

$this->app->singleton(EmailServiceConnectorInterface::class, $adapter);

namespace App\Connectors\Adapters;

use Nexus\Connector\Contracts\EmailServiceConnectorInterface;
use Mailchimp\Marketing\ApiClient;

class MailchimpEmailAdapter implements EmailServiceConnectorInterface
{
    public function __construct(
        private readonly ApiClient $client,
        private readonly string $listId
    ) {}

    public function sendTransactionalEmail(
        string $recipient,
        string $subject,
        string $body
    ): bool {
        $response = $this->client->messages->send([
            'message' => [
                'to' => [['email' => $recipient]],
                'subject' => $subject,
                'html' => $body,
            ],
        ]);

        return $response[0]['status'] === 'sent';
    }

    // Implement other interface methods...
}