Issue #1 - Mediator calls listeners in wrong order.

@param MockListener $listener
@param Event        $event

@throws \DomainException
@throws \InvalidArgumentException
340862
public function it_should_call_listeners_for_their_events_in_correct_priority_order_when_event_is_triggered(
        MockListener $listener,
        Event $event
    ) {
        $event->hasBeenHandled()
              ->willReturn(\false);
        $this->addListener('test1', [$listener, 'method1']);
        $this->addListener('test1', [$listener, 'method2']);
        $this->addListener('test1', [$listener, 'method1'], 'first');
        $this->addListener('test1', [$listener, 'method1'], 'last');
        $this->getListeners()
             ->shouldHaveKey('test1');
        $listener->method1($event, 'test1', $this)
                 ->shouldBeCalled();
        $listener->method1($event, 'test1', $this)
                 ->willReturn($event);
        $listener->method2($event, 'test1', $this)
                 ->shouldBeCalled();
        $listener->method2($event, 'test1', $this)
                 ->willReturn($event);
        $this->trigger('test1', $event);
        $expected = [
            1 => [[$listener, 'method1']],
            0 => [[$listener, 'method1'], [$listener, 'method2']],
            -1 => [[$listener, 'method1']]
        ];
        $this->getListeners('test1')
             ->shouldReturn($expected);
    }
40665
public function add($type, $listener)
    {
        if (!array_key_exists($type, $this->_eventListeners)) {
            $this->_eventListeners [$type] = [];
        }
        array_push($this->_eventListeners[$type], $listener);
    }
315304
public function getListenerPriority($eventType, $listener)
    {
        if (isset($this->listeners[$eventType])) {
            foreach ($this->listeners[$eventType] as $priority => $listeners) {
                if (false !== ($key = array_search($listener, $listeners, true))) {
                    return $priority;
                }
            }
        }

        return null;
    }
791970
public function unregister(ListenerInterface $listener)
    {
        if (!isset($this->storage[$listener])) {
            throw new InvalidArgumentException('Listener is not registered.');
        }

        foreach ($this->storage[$listener] as $event => $EElistener) {
            $listener->removeListener($event, $EElistener);
        }

        $this->storage->detach($listener);

        return $this;
    }
98161
public function registerEventListeners(DispatcherContract $events)
    {
        foreach ($this->listen as $event => $listeners) {
            $defaultPriority = 0;
            if (!is_array($listeners)) {
                $events->listen($event, $listeners);
                continue;
            }
            foreach ($listeners as $listener => $priority) {
                if (is_numeric($listener)) {
                    $listener = $priority;
                    $priority = $defaultPriority;
                }
                if (is_array($listener)) {
                    $priority = is_numeric(current($listener)) ? current($listener) : $defaultPriority;
                    $listener = key($listener);
                }
                $events->listen($event, $listener, $priority);
            }
        }
        foreach ($this->subscribe as $subscriber) {
            $events->subscribe($subscriber);
        }
    }
1244810
public function createClassListener($listener)
    {
        return function ($event, $payload) use ($listener) {
            return call_user_func_array($this->createClassCallable($listener), $payload);
        };
    }
932761
public function removeListener(
        string $event,
        callable $listener
    )
    {
        if ($this->_events[$event] ?? false) {

            $key = array_search(
                $listener,
                $this->_events[$event],
                true
            );

            if ($key !== false) {

                array_splice(
                    $this->_events[$event],
                    $key,
                    1
                );
            }
        }

        return $this;
    }
1111178
public function listen($events, $listener, $priority = 0)
    {
        //$priority = sprintf('p:%s', str_pad($priority, 6, '0', STR_PAD_LEFT));

        foreach ((array) $events as $event) {
            $this->listeners[$event][$priority][] = $this->makeListener($listener);
        }
    }
50868
public function onAfter($event, callable $listener)
    {
        $this->checkEvent($event);

        if (!isset($this->afterListeners[$event])) {
            $this->afterListeners[$event] = [];
        }
        $this->afterListeners[$event][] = $listener;

        return $this;
    }
972054
public function addListener( $event, EventListener $listener ) {

		if ( !is_string( $event ) ) {
			throw new InvalidArgumentException( "Expected a string" );
		}

		$event = strtolower( $event );

		if ( !isset( $this->dispatchableListeners[$event] ) ) {
			$this->dispatchableListeners[$event] = array();
		}

		$this->dispatchableListeners[$event][] = $listener;
	}
1145752
public function it_should_call_service_listeners_for_their_events_when_event_is_triggered(
        MockListener $listener,
        Event $event,
        Container $container
    ) {
        $event->hasBeenHandled()
              ->willReturn(\false);
        $this->addServiceListener('test1', ['ContainerID1', 'method1']);
        $this->getServiceListeners()
             ->shouldHaveKey('test1');
        $container->offsetGet('ContainerID1')
                  ->willReturn($listener);
        $this->setServiceContainer($container);
        $listener->method1($event, 'test1', $this)
                 ->shouldBeCalled();
        $this->trigger('test1', $event);
    }
1008384
private function handleListenersForEvent(EventInterface $event, $listeners)
    {
        $return = array();

        foreach ($listeners as $listener) {
            $return[] = $listener->handle($event);
        }

        return $return;
    }
1019365
public function removeListener($event, $listener)
    {
        // shortcut to only remove a listener if they exist
        // having this allows us to simplify calling code
        if( !isset($listener) ) {
            return $this;
        }

        // listeners
        if (isset($this->listeners[$event])) {
            if (false !== ($key = array_search($listener, $this->listeners[$event]))) {
                unset($this->listeners[$event][$key]);
            }
            if( !$this->listeners[$event] ) {
                unset( $this->listeners[$event] );
            }
        }
        // listeners once
        if (isset($this->listenersOnce[$event])) {
            if (false !== ($key = array_search($listener, $this->listenersOnce[$event]))) {
                unset($this->listenersOnce[$event][$key]);
            }
            if( !$this->listenersOnce[$event] ) {
                unset( $this->listenersOnce[$event] );
            }
        }
        return $this;
    }
1200418
public function on($event, callable $listener)
    {
        if ($this->markFilters) {
            $listener = $this->wrap($listener, '*', $this->markFilters);
        }

        $this->root->on($event, $listener);

        return $this;
    }
772233
public function handle(EventListenerInterface $listener)
    {
        if (!$listener->getListeners()) {
            return;
        }

        $content = $this->request->getContent();

        try {
            $options = Serializer::parse($content);
        } catch (\InvalidArgumentException $e) {
            $options = [];
        }

        foreach ($listener->getListeners() as $namespace => $callable) {
            $event = new $namespace($options);
            if ($event->isValid()) {
                $listener->trigger($namespace, $event);
                break;
            }
        }
    }
347288
public function setListener($event, callable $listener)
    {
        if (!is_string($event) || empty($event)) {
            throw new \InvalidArgumentException('The event name is invalid');
        }

        $this->listeners[$event] = $listener;
    }
437817
public function addListener(string $event, $listener)
    {
        if (!isset($this->listeners[$event])) {
            $this->listeners[$event] = [];
        }
        $this->listeners[$event][] = $listener;
    }