Which is the best entrance for a prizing discourse? When reply to a topic, will win a prize by chance, to attract more people to take part in

I am using laminas to build a project: a discourse kind system.

Our PM propose a function that publish a kind of topic(not all topic, just some choosen ones), when users reply to these topics, they may win a prize (a game activation code, etc) by chance.

To me, I want it to be decoupled to the main disourse logic. It should be a plugin-kind function.
It not break into the existing logic. And can be extented more such kind functions, and can be removed easily.

When a reply won the prize, this layer I mean this reply will have a special mark. Others will see the mark and take part in this topic.

I am considering where is the best entrance to place such extral function logic. registering some customed events like after_replied, or do these at the end of each controller action.

Any advises will be very much appreciated. Thank you!

You should use events here. See also this recent topic: Passing config into model

thank you!
I am trying to learn from your link.

Hi, long time no see!

I learned the link you pasted above. And learned that event may be the best way to approach the goal.

And also I have read Using the EventManager - tutorials - Laminas Docs several times.

Still I have problems to practise in my project.

  1. I don’t know where to place the following code, in the Module.php?(I only found attach method in the onBootstrap() of Module.php file)
$events->attach('do', function ($e) {});
  1. the PM said that, we may hold different activities(promotions) onside a topic. I am planning to write different classes for different promotion logics. each class encapsulate a concrete promotion logic. this way will provide flexibility to me. I can write anything inside the class.
    My question is that how to involve the promotion class(as well as promotion class instance) in/with the event stuff?

  2. how to trigger the event after comment done. in another saying, where to place the trigger related code

$events->trigger('do', null, $params);

should I put it inside the CommentTable’s save() method?

You said it should decoupled with controller and action. inside the comment model you suggested. But I don’t know how. I am still looking forward to your example.

By the way, I got error when I followed the tutorial Using the EventManager - tutorials - Laminas Docs
My test code:

<?php
chdir(dirname(__DIR__));
include __DIR__ . '/../vendor/autoload.php';

use Laminas\EventManager\EventManager;
use Laminas\EventManager\EventManagerAwareInterface;
use Laminas\EventManager\EventManagerInterface;
use Laminas\EventManager\SharedEventManager;

class Example implements EventManagerAwareInterface
{
    protected $events;

    public function setEventManager(EventManagerInterface $events)
    {
        $events->setIdentifiers([
            __CLASS__,
            get_class($this),
        ]);
        $this->events = $events;
    }

    public function getEventManager()
    {
        if (! $this->events) {
            $this->setEventManager(new EventManager());
        }
        return $this->events;
    }

    public function doIt($foo, $baz)
    {
        $params = compact('foo', 'baz');
        $this->getEventManager()->trigger(__FUNCTION__, $this, $params);
    }

}

/*$example = new Example();

$example->getEventManager()->attach('doIt', function($e) {
    $event  = $e->getName();
    $target = get_class($e->getTarget()); // "Example"
    $params = $e->getParams();
    printf(
        'Handled event "%s" on target "%s", with parameters %s',
        $event,
        $target,
        json_encode($params)
    );
});

$example->doIt('bar', 'bat');*/

$sharedEvents = new SharedEventManager();
$sharedEvents->attach('Example', 'do', function ($e) {
    $event  = $e->getName();
    $target = get_class($e->getTarget()); // "Example"
    $params = $e->getParams();
    printf(
        'Handled event "%s" on target "%s", with parameters %s',
        $event,
        $target,
        json_encode($params)
    );
});

$example = new Example();
$example->getEventManager()->setSharedManager($sharedEvents);
$example->do('bar', 'bat');

The error I got

Besides, when I wanna to typping the code by myself, I found in this page Quick Start - laminas-eventmanager - Laminas Docs

use Laminas\Log\Factory as LogFactory;

There is no Factory here, right after *Laminas\Log*
The phpstorm raise autocomplete Laminas\Log\Writer\Factory