<?php

namespace Openguru\OpenCartFramework\Events;

use Openguru\OpenCartFramework\Container\Container;

class EventDispatcher
{
    private $listeners = [];
    private $container;

    public function __construct(Container $container)
    {
        $this->container = $container;
    }

    public function registerListener(string $eventClass, string $listenerClass): void
    {
        $this->listeners[$eventClass][] = $listenerClass;
    }

    public function dispatch(Event $event): void
    {
        $eventClass = get_class($event);
        if (isset($this->listeners[$eventClass])) {
            foreach ($this->listeners[$eventClass] as $listenerClass) {
                $listener = $this->container->get($listenerClass);
                $listener->handle($event);
            }
        }
    }
}
