Move events handling to Symfony\EventDispatcher
This commit is contained in:
@@ -1,79 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Event;
|
||||
|
||||
use Pimple\Container;
|
||||
use Core\Listener;
|
||||
use Core\Tool;
|
||||
|
||||
/**
|
||||
* Base Listener
|
||||
*
|
||||
* @package event
|
||||
* @author Frederic Guillot
|
||||
*
|
||||
* @property \Model\Comment $comment
|
||||
* @property \Model\Project $project
|
||||
* @property \Model\ProjectActivity $projectActivity
|
||||
* @property \Model\SubTask $subTask
|
||||
* @property \Model\Task $task
|
||||
* @property \Model\TaskFinder $taskFinder
|
||||
*/
|
||||
abstract class Base implements Listener
|
||||
{
|
||||
/**
|
||||
* Container instance
|
||||
*
|
||||
* @access protected
|
||||
* @var \Pimple\Container
|
||||
*/
|
||||
protected $container;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @access public
|
||||
* @param \Pimple\Container $container
|
||||
*/
|
||||
public function __construct(Container $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return class information
|
||||
*
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return get_called_class();
|
||||
}
|
||||
|
||||
/**
|
||||
* Load automatically models
|
||||
*
|
||||
* @access public
|
||||
* @param string $name Model name
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get($name)
|
||||
{
|
||||
return Tool::loadModel($this->container, $name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get event namespace
|
||||
*
|
||||
* Event = task.close | Namespace = task
|
||||
*
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function getEventNamespace()
|
||||
{
|
||||
$event_name = $this->container['event']->getLastTriggeredEvent();
|
||||
return substr($event_name, 0, strpos($event_name, '.'));
|
||||
}
|
||||
}
|
||||
7
app/Event/CommentEvent.php
Normal file
7
app/Event/CommentEvent.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Event;
|
||||
|
||||
class CommentEvent extends GenericEvent
|
||||
{
|
||||
}
|
||||
7
app/Event/FileEvent.php
Normal file
7
app/Event/FileEvent.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Event;
|
||||
|
||||
class FileEvent extends GenericEvent
|
||||
{
|
||||
}
|
||||
45
app/Event/GenericEvent.php
Normal file
45
app/Event/GenericEvent.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Event;
|
||||
|
||||
use ArrayAccess;
|
||||
use Symfony\Component\EventDispatcher\Event as BaseEvent;
|
||||
|
||||
class GenericEvent extends BaseEvent implements ArrayAccess
|
||||
{
|
||||
private $container = array();
|
||||
|
||||
public function __construct(array $values = array())
|
||||
{
|
||||
$this->container = $values;
|
||||
}
|
||||
|
||||
public function getAll()
|
||||
{
|
||||
return $this->container;
|
||||
}
|
||||
|
||||
public function offsetSet($offset, $value)
|
||||
{
|
||||
if (is_null($offset)) {
|
||||
$this->container[] = $value;
|
||||
} else {
|
||||
$this->container[$offset] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
public function offsetExists($offset)
|
||||
{
|
||||
return isset($this->container[$offset]);
|
||||
}
|
||||
|
||||
public function offsetUnset($offset)
|
||||
{
|
||||
unset($this->container[$offset]);
|
||||
}
|
||||
|
||||
public function offsetGet($offset)
|
||||
{
|
||||
return isset($this->container[$offset]) ? $this->container[$offset] : null;
|
||||
}
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Event;
|
||||
|
||||
/**
|
||||
* Notification listener
|
||||
*
|
||||
* @package event
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class NotificationListener extends Base
|
||||
{
|
||||
/**
|
||||
* Template name
|
||||
*
|
||||
* @accesss private
|
||||
* @var string
|
||||
*/
|
||||
private $template = '';
|
||||
|
||||
/**
|
||||
* Set template name
|
||||
*
|
||||
* @access public
|
||||
* @param string $template Template name
|
||||
*/
|
||||
public function setTemplate($template)
|
||||
{
|
||||
$this->template = $template;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the action
|
||||
*
|
||||
* @access public
|
||||
* @param array $data Event data dictionary
|
||||
* @return bool True if the action was executed or false when not executed
|
||||
*/
|
||||
public function execute(array $data)
|
||||
{
|
||||
$values = $this->getTemplateData($data);
|
||||
$users = $this->notification->getUsersList($values['task']['project_id']);
|
||||
|
||||
if ($users) {
|
||||
$this->notification->sendEmails($this->template, $users, $values);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch data for the mail template
|
||||
*
|
||||
* @access public
|
||||
* @param array $data Event data
|
||||
* @return array
|
||||
*/
|
||||
public function getTemplateData(array $data)
|
||||
{
|
||||
$values = array();
|
||||
|
||||
switch ($this->getEventNamespace()) {
|
||||
case 'task':
|
||||
$values['task'] = $this->taskFinder->getDetails($data['task_id']);
|
||||
break;
|
||||
case 'subtask':
|
||||
$values['subtask'] = $this->subtask->getById($data['id'], true);
|
||||
$values['task'] = $this->taskFinder->getDetails($data['task_id']);
|
||||
break;
|
||||
case 'file':
|
||||
$values['file'] = $data;
|
||||
$values['task'] = $this->taskFinder->getDetails($data['task_id']);
|
||||
break;
|
||||
case 'comment':
|
||||
$values['comment'] = $this->comment->getById($data['id']);
|
||||
$values['task'] = $this->taskFinder->getDetails($values['comment']['task_id']);
|
||||
break;
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Event;
|
||||
|
||||
/**
|
||||
* Project activity listener
|
||||
*
|
||||
* @package event
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class ProjectActivityListener extends Base
|
||||
{
|
||||
/**
|
||||
* Execute the action
|
||||
*
|
||||
* @access public
|
||||
* @param array $data Event data dictionary
|
||||
* @return bool True if the action was executed or false when not executed
|
||||
*/
|
||||
public function execute(array $data)
|
||||
{
|
||||
if (isset($data['task_id'])) {
|
||||
|
||||
$values = $this->getValues($data);
|
||||
|
||||
return $this->projectActivity->createEvent(
|
||||
$values['task']['project_id'],
|
||||
$values['task']['id'],
|
||||
$this->acl->getUserId(),
|
||||
$this->container['event']->getLastTriggeredEvent(),
|
||||
$values
|
||||
);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get event activity data
|
||||
*
|
||||
* @access private
|
||||
* @param array $data Event data dictionary
|
||||
* @return array
|
||||
*/
|
||||
private function getValues(array $data)
|
||||
{
|
||||
$values = array();
|
||||
$values['task'] = $this->taskFinder->getDetails($data['task_id']);
|
||||
|
||||
switch ($this->getEventNamespace()) {
|
||||
case 'subtask':
|
||||
$values['subtask'] = $this->subTask->getById($data['id'], true);
|
||||
break;
|
||||
case 'comment':
|
||||
$values['comment'] = $this->comment->getById($data['id']);
|
||||
break;
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Event;
|
||||
|
||||
/**
|
||||
* Project daily summary listener
|
||||
*
|
||||
* @package event
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class ProjectDailySummaryListener extends Base
|
||||
{
|
||||
/**
|
||||
* Execute the action
|
||||
*
|
||||
* @access public
|
||||
* @param array $data Event data dictionary
|
||||
* @return bool True if the action was executed or false when not executed
|
||||
*/
|
||||
public function execute(array $data)
|
||||
{
|
||||
if (isset($data['project_id'])) {
|
||||
return $this->projectDailySummary->updateTotals($data['project_id'], date('Y-m-d'));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Event;
|
||||
|
||||
/**
|
||||
* Project modification date listener
|
||||
*
|
||||
* Update the "last_modified" field for a project
|
||||
*
|
||||
* @package event
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class ProjectModificationDateListener extends Base
|
||||
{
|
||||
/**
|
||||
* Execute the action
|
||||
*
|
||||
* @access public
|
||||
* @param array $data Event data dictionary
|
||||
* @return bool True if the action was executed or false when not executed
|
||||
*/
|
||||
public function execute(array $data)
|
||||
{
|
||||
if (isset($data['project_id'])) {
|
||||
return $this->project->updateModificationDate($data['project_id']);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
7
app/Event/SubtaskEvent.php
Normal file
7
app/Event/SubtaskEvent.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Event;
|
||||
|
||||
class SubtaskEvent extends GenericEvent
|
||||
{
|
||||
}
|
||||
7
app/Event/TaskEvent.php
Normal file
7
app/Event/TaskEvent.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Event;
|
||||
|
||||
class TaskEvent extends GenericEvent
|
||||
{
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Event;
|
||||
|
||||
/**
|
||||
* Webhook task events
|
||||
*
|
||||
* @package event
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class WebhookListener extends Base
|
||||
{
|
||||
/**
|
||||
* Url to call
|
||||
*
|
||||
* @access private
|
||||
* @var string
|
||||
*/
|
||||
private $url = '';
|
||||
|
||||
/**
|
||||
* Set webhook url
|
||||
*
|
||||
* @access public
|
||||
* @param string $url URL to call
|
||||
*/
|
||||
public function setUrl($url)
|
||||
{
|
||||
$this->url = $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the action
|
||||
*
|
||||
* @access public
|
||||
* @param array $data Event data dictionary
|
||||
* @return bool True if the action was executed or false when not executed
|
||||
*/
|
||||
public function execute(array $data)
|
||||
{
|
||||
$this->webhook->notify($this->url, $data);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user