php-ddd icon indicating copy to clipboard operation
php-ddd copied to clipboard

How to upcast events with Prooph

Open webdevilopers opened this issue 4 years ago • 1 comments

Came from:

  • https://twitter.com/webdevilopers/status/1347197290981818370

Symfony DI integeration:

  • https://gist.github.com/webdevilopers/38f1f18576251c99b1cd9aad3753c176
services:
  Prooph\EventSourcing\EventStoreIntegration\AggregateTranslator: null

  # Metadata enrichers
  Acme\Common\Infrastructure\Prooph\EventStore\IPAddressMetadataEnricher:
    tags:
      - { name: 'prooph_event_store.default.metadata_enricher' }
    arguments:
      - '@request_stack'
  
  # Upcasters
  Acme\Common\Infrastructure\Prooph\EventStore\Upcaster: ~

  Prooph\EventStore\Plugin\UpcastingPlugin:
    arguments: ['@Acme\Common\Infrastructure\Prooph\EventStore\Upcaster']
    tags:
      - { name: 'prooph_event_store.default.plugin' }

The Upcaster implementation:

<?php

namespace Acme\Common\Infrastructure\Prooph\EventStore;

use Prooph\Common\Messaging\Message;
use Prooph\EventStore\Upcasting\SingleEventUpcaster;

final class Upcaster extends SingleEventUpcaster
{
    public function upcast(Message $message): array
    {
        dd($message);
    }

    protected function canUpcast(Message $message): bool
    {
        dd(__METHOD__);
    }

    protected function doUpcast(Message $message): array
    {
        dd(__METHOD__);
    }
}

Using the class already provided by Prooph:

  • https://github.com/prooph/event-store/blob/7.x/src/Plugin/UpcastingPlugin.php

webdevilopers avatar Jan 08 '21 09:01 webdevilopers

Related: https://github.com/prooph/event-store-symfony-bundle/issues/73

Registered Upcaster Plugin is not recognized by Projection

webdevilopers avatar Mar 30 '21 09:03 webdevilopers