PHP code example of tourze / async-service-call-bundle

1. Go to this page and download the library: Download tourze/async-service-call-bundle 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/ */

    

tourze / async-service-call-bundle example snippets


return [
    // ...
    Tourze\AsyncServiceCallBundle\AsyncServiceCallBundle::class => ['all' => true],
];

use Tourze\AsyncServiceCallBundle\Message\ServiceCallMessage;

$message = new ServiceCallMessage();
$message->setServiceId('my.service.id');
$message->setMethod('processData');
$message->setParams(['param1', 'param2']);
$message->setMaxRetryCount(3);
$message->setRetryCount(3);

// Dispatch to message queue
$messageBus->dispatch($message);

use Tourze\AsyncServiceCallBundle\Message\ServiceCallMessage;
use Symfony\Component\Messenger\MessageBusInterface;

class MyController
{
    public function __construct(
        private MessageBusInterface $messageBus
    ) {}

    public function sendAsyncCall(): void
    {
        $message = new ServiceCallMessage();
        $message->setServiceId('email.service');
        $message->setMethod('sendEmail');
        $message->setParams(['[email protected]', 'Subject', 'Body']);
        $message->setMaxRetryCount(5);
        $message->setRetryCount(5);

        $this->messageBus->dispatch($message);
    }
}

use Tourze\AsyncServiceCallBundle\Message\ServiceCallMessage;

// Pass entity objects
$user = $entityManager->find(User::class, 1);
$message = new ServiceCallMessage();
$message->setServiceId('user.service');
$message->setMethod('updateProfile');
$message->setParams([$user, ['name' => 'New Name']]);

$this->messageBus->dispatch($message);

enum Status: string
{
    case ACTIVE = 'active';
    case INACTIVE = 'inactive';
}

$message = new ServiceCallMessage();
$message->setServiceId('status.service');
$message->setMethod('updateStatus');
$message->setParams([Status::ACTIVE]);

$this->messageBus->dispatch($message);

use Tourze\AsyncServiceCallBundle\MessageHandler\ServiceCallHandler;
use Tourze\AsyncServiceCallBundle\Message\ServiceCallMessage;

class CustomServiceCallHandler extends ServiceCallHandler
{
    public function __invoke(ServiceCallMessage $message): void
    {
        // Custom pre-processing
        $this->logger->info('Processing custom service call');
        
        // Call parent handler
        parent::__invoke($message);
        
        // Custom post-processing
        $this->logger->info('Custom service call completed');
    }
}

use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;

class CustomObjectNormalizer implements NormalizerInterface, DenormalizerInterface
{
    public function normalize(mixed $object, ?string $format = null, array $context = []): array
    {
        if ($object instanceof MyCustomObject) {
            return [
                'id' => $object->getId(),
                'data' => $object->serialize(),
            ];
        }
        
        throw new \InvalidArgumentException('Unsupported object type');
    }
    
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
    {
        return $data instanceof MyCustomObject;
    }
    
    // Implement denormalization methods...
}

use Tourze\AsyncServiceCallBundle\Exception\InvalidParameterException;

try {
    $message = new ServiceCallMessage();
    $message->setParams(['invalid' => new \stdClass()]);
    $messageBus->dispatch($message);
} catch (InvalidParameterException $e) {
    // Handle serialization errors
    $logger->error('Serialization failed', ['error' => $e->getMessage()]);
}

use Tourze\AsyncServiceCallBundle\Service\Serializer;

class MyCustomSerializer extends Serializer
{
    // Custom serialization logic
}