Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
crazyfactory/kpi / src / NotifierManager.php
Size: Mime:
<?php

namespace CrazyFactory\Kpi;

abstract class NotifierManager
{

    /**
     * @param AggregatedEmitterStateChange $aggregatedEmitterStateChange
     *
     * @throws \Exception
     */
    public function notify(AggregatedEmitterStateChange $aggregatedEmitterStateChange)
    {
        $map = $this->getNotifierMap();

        foreach ($map as $name => $classNameOrInstance) {
            // Sensors should really exist. This is a rare case where a complete stop is welcome.
            if (!is_object($classNameOrInstance) && !class_exists($classNameOrInstance)) {
                throw new \Exception('notifier class ' . $name . ' not found');
            }

            /* @var \CrazyFactory\Kpi\NotifierInterface $notifier */
            $notifier = $classNameOrInstance instanceof NotifierInterface
                ? $classNameOrInstance
                : new $classNameOrInstance();

            $notifier->notify($aggregatedEmitterStateChange);
        }
    }

    /**
     * @return string|object[]
     */
    protected function getNotifierMap()
    {
        $map = array();
        $classes = $this->getNotifiers();
        foreach ($classes as $classNameOrInstance) {
            $className = $classNameOrInstance instanceof NotifierInterface
                ? get_class($classNameOrInstance)
                : $classNameOrInstance;

            $name = substr($className, strrpos($className, '\\') + 1, -strlen('Notifier'));
            $map[$name] = $classNameOrInstance;
        }

        return $map;
    }

    /**
     * @return string[]|object[]
     */
    abstract protected function getNotifiers();
}