protected function optimisticCheck(StreamId $streamId, string $expectedEtag, array $context): void
    {
        $slice = $this->getStreamSlice($streamId, null, 1, false, true, $context);

        if (!$slice->count()) {
            throw new OptimisticCheckFailed(
                sprintf(
                    'The DynamoDb table [%s] has no events in stream [%s].',
                    $this->getTableNameForRead($context),
                    $streamId
                )
            );
        }

        $event = $slice->getIterator()->current();

        // todo: review this etag strategy (need to make this more explicit/obvious)
        $eventId = (string)$event->get('event_id');
        if ($eventId === $expectedEtag || md5($eventId) === $expectedEtag) {
            return;
        }

        throw new OptimisticCheckFailed(
            sprintf(
                'The last event [%s:%s] in DynamoDb table [%s] from stream [%s] doesn\'t match expected etag [%s].',
                $eventId,
                md5($eventId),
                $this->getTableNameForRead($context),
                $streamId,
                $expectedEtag
            )
        );
    }
protected function optimisticCheck(StreamId $streamId, string $expectedEtag, array $context): void
    {
        $slice = $this->getStreamSlice($streamId, null, 1, false, true, $context);

        if (!$slice->count()) {
            throw new OptimisticCheckFailed(
                sprintf(
                    'The DynamoDb table [%s] has no events in stream [%s].',
                    $this->getTableNameForRead($context),
                    $streamId
                )
            );
        }

        $event = $slice->getIterator()->current();

        // todo: review this etag strategy (need to make this more explicit/obvious)
        $eventId = (string)$event->get('event_id');
        if ($eventId === $expectedEtag || md5($eventId) === $expectedEtag) {
            return;
        }

        throw new OptimisticCheckFailed(
            sprintf(
                'The last event [%s:%s] in DynamoDb table [%s] from stream [%s] doesn\'t match expected etag [%s].',
                $eventId,
                md5($eventId),
                $this->getTableNameForRead($context),
                $streamId,
                $expectedEtag
            )
        );
    }
protected function optimisticCheck ( StreamId $ streamId , string $ expectedEtag , array $ context ) : void { $ slice = $ this -> getStreamSlice ( $ streamId , null , 1 , false , true , $ context ) ; if ( ! $ slice -> count ( ) ) { throw new OptimisticCheckFailed ( sprintf ( 'The DynamoDb table [%s] has no events in stream [%s].' , $ this -> getTableNameForRead ( $ context ) , $ streamId ) ) ; } $ event = $ slice -> getIterator ( ) -> current ( ) ; // todo: review this etag strategy (need to make this more explicit/obvious) $ eventId = ( string ) $ event -> get ( 'event_id' ) ; if ( $ eventId === $ expectedEtag || md5 ( $ eventId ) === $ expectedEtag ) { return ; } throw new OptimisticCheckFailed ( sprintf ( 'The last event [%s:%s] in DynamoDb table [%s] from stream [%s] doesn\'t match expected etag [%s].' , $ eventId , md5 ( $ eventId ) , $ this -> getTableNameForRead ( $ context ) , $ streamId , $ expectedEtag ) ) ; }
When an expected etag is provided we can check the head of the stream to see if it's
at the expected state before appending events.

@param StreamId $streamId
@param string   $expectedEtag
@param array    $context

@throws OptimisticCheckFailed
When an expected etag is provided we can check the head of the stream to see if it s at the expected state before appending events .