npo/message-resource-bundle

Symfony bundle for npo/message-resource

11 2024-09-04 07:22 UTC

This package is not auto-updated.

Last update: 2024-11-13 08:28:18 UTC


README

composer require npo/message-resource-bundle

This package provides a configured factory for message-resource messages aliased as @npo_message_resource.message_factory.

Usage

Add to config/bundles.php:

NpoMessage\ResourceBundle\MessageResourceBundle::class => ['all' => true],

Example in services.yaml:

App\MyMessageHandler:
  arguments: 
    $messageFactory: '@npo_message_resource.message_factory'

Example in App\MyMessageHandler

<php

namespace App;

use NpoMessage\Resource\Domain\Factory\IChainMessageFactory;

class MyMessageHandler
{
    public function __construct(
        private IChainMessageFactory $factory
    ){}
    
    public function handleMessage(string $message): void
    {
        if(!$this->factory->supports($message)) {
            return;
        }
        
        try {
            $messageAbstract = $this->factory->create($message);
        } catch (UnsupportedMessageException $exception) {
            // handle unsupported message
        }
    }
}