Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #4 from codeliner/patch-3
Browse files Browse the repository at this point in the history
Patch-3: Add ProophActionEventDispatcher
  • Loading branch information
codeliner committed May 22, 2015
2 parents c6461d7 + e35e350 commit 27cc974
Show file tree
Hide file tree
Showing 8 changed files with 753 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Event/ActionEvent.php
Expand Up @@ -71,7 +71,7 @@ public function setTarget($target);
/**
* Set event parameters
*
* @param string $params
* @param array|\ArrayAccess $params
* @return void
*/
public function setParams($params);
Expand Down
178 changes: 178 additions & 0 deletions src/Event/DefaultActionEvent.php
@@ -0,0 +1,178 @@
<?php
/*
* This file is part of the prooph/common.
* (c) 2014-2015 prooph software GmbH <contact@prooph.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Date: 5/22/15 - 6:59 PM
*/
namespace Prooph\Common\Event;

/**
* Class DefaultActionEvent
*
* Default implementation of ActionEvent
*
* @package Prooph\Common\Event
* @author Alexander Miertsch <kontakt@codeliner.ws>
*/
class DefaultActionEvent implements ActionEvent
{
/**
* @var string
*/
protected $name;

/**
* @var mixed
*/
protected $target;

/**
* @var array|\ArrayAccess
*/
protected $params;

/**
* @var boolean
*/
protected $stopPropagation = false;

/**
* @param string $name
* @param mixed|null $target
* @param array|\ArrayAccess|null $params
*/
public function __construct($name, $target = null, $params = null)
{
$this->setName($name);

$this->setTarget($target);

if ($params === null) {
$params = [];
}

$this->setParams($params);
}

/**
* Get event name
*
* @return string
*/
public function getName()
{
return $this->name;
}

/**
* Get target/context from which event was triggered
*
* @return null|string|object
*/
public function getTarget()
{
return $this->target;
}

/**
* Get parameters passed to the event
*
* @return array|\ArrayAccess
*/
public function getParams()
{
return $this->params;
}

/**
* Get a single parameter by name
*
* @param string $name
* @param mixed $default Default value to return if parameter does not exist
* @return mixed
*/
public function getParam($name, $default = null)
{
return isset($this->params[$name])? $this->params[$name] : $default;
}

/**
* Set the event name
*
* @param string $name
* @throws \InvalidArgumentException
* @return void
*/
public function setName($name)
{
if (! is_string($name)) {
throw new \InvalidArgumentException("Event name is invalid. Expected string. Got " . gettype($name));
}

$this->name = $name;
}

/**
* Set the event target/context
*
* @param null|string|object $target
* @return void
*/
public function setTarget($target)
{
$this->target = $target;
}

/**
* Set event parameters
*
* @param array|\ArrayAccess $params
* @throws \InvalidArgumentException
* @return void
*/
public function setParams($params)
{
if (! is_array($params) && ! $params instanceof \ArrayAccess) {
throw new \InvalidArgumentException("Event params are invalid. Expected type is array or \\ArrayAccess. Got " . gettype($params));
}

$this->params = $params;
}

/**
* Set a single parameter by key
*
* @param string $name
* @param mixed $value
* @return void
*/
public function setParam($name, $value)
{
$this->params[$name] = $value;
}

/**
* Indicate whether or not the parent ActionEventDispatcher should stop propagating events
*
* @param bool $flag
* @return void
*/
public function stopPropagation($flag = true)
{
$this->stopPropagation = $flag;
}

/**
* Has this event indicated event propagation should stop?
*
* @return bool
*/
public function propagationIsStopped()
{
return $this->stopPropagation;
}
}
46 changes: 46 additions & 0 deletions src/Event/DefaultListenerHandler.php
@@ -0,0 +1,46 @@
<?php
/*
* This file is part of the prooph/common.
* (c) 2014-2015 prooph software GmbH <contact@prooph.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Date: 5/22/15 - 8:27 PM
*/
namespace Prooph\Common\Event;

/**
* Class DefaultListenerHandler
*
* @package Prooph\Common\Event
* @author Alexander Miertsch <kontakt@codeliner.ws>
*/
final class DefaultListenerHandler implements ListenerHandler
{
/**
* @var callable|ActionEventListener
*/
private $listener;

/**
* @param callable|ActionEventListener $listener
* @throws \InvalidArgumentException
*/
public function __construct($listener)
{
if (! $listener instanceof ActionEventListener && !is_callable($listener)) {
throw new \InvalidArgumentException('Given parameter listener should be callable or an instance of ActionEventListener. Got ' . (is_object($listener)? get_class($listener) : gettype($listener)));
}

$this->listener = $listener;
}

/**
* @return callable|ActionEventListener
*/
public function getActionEventListener()
{
return $this->listener;
}
}
2 changes: 1 addition & 1 deletion src/Event/ListenerHandler.php
Expand Up @@ -20,7 +20,7 @@
interface ListenerHandler
{
/**
* @return ActionEventListener
* @return callable|ActionEventListener
*/
public function getActionEventListener();
}

0 comments on commit 27cc974

Please sign in to comment.