<?php
namespace Fifa\ConnectId\Sdk\Registration\Event;

use Exception;
use Fifa\ConnectId\Sdk\Model\PersonDetails;
use Fifa\ConnectId\Sdk\Model\PersonDetailsRequest;
use Fifa\ConnectId\Sdk\Registration\Event\Handler\EventHandlerInterface;
use Fifa\ConnectServiceBus\Sdk\Message\Message;

/**
 * Class EventHandlerCollection
 * @package Fifa\ConnectId\Sdk\Registration\Event
 */
class EventHandlerCollection
{
    /**
     * @var EventHandlerInterface|string|array
     */
    private $collection = array();

    /**
     * String representation of the object
     *
     * @return string
     */
    public function __toString()
    {
        return 'Collection size: ' . (is_array($this->collection) ? sizeof($this->collection) : 0);
    }

    /**
     * Adds event handler to collection
     *
     * @param EventHandlerInterface|string|array $callback
     */
    public function addEventHandler($callback)
    {
        $this->collection[] = $callback;
    }

    /**
     * Executes the handlers for event
     *
     * @param PersonDetails|PersonDetailsRequest|Message|Exception $eventData
     */
    public function raiseEvent($eventData)
    {
        foreach ($this->collection as $callback) {

            if ($callback instanceof EventHandlerInterface) {
                $callback->execute($eventData);
            }
            elseif (is_string($callback) || is_array($callback)) {
                call_user_func($callback, $eventData);
            }
        }
    }

    /**
     * @return array|EventHandlerInterface|string
     */
    public function getCollection()
    {
        return $this->collection;
    }
}