File events refactoring

This commit is contained in:
Frederic Guillot
2016-07-17 18:47:06 -04:00
parent ec0ecc5b03
commit cbe52e5720
21 changed files with 398 additions and 74 deletions

View File

@@ -151,6 +151,8 @@ use Pimple\Container;
* @property \Kanboard\Core\Filter\LexerBuilder $taskLexer
* @property \Kanboard\Core\Filter\LexerBuilder $projectActivityLexer
* @property \Kanboard\Job\CommentEventJob $commentEventJob
* @property \Kanboard\Job\TaskFileEventJob $taskFileEventJob
* @property \Kanboard\Job\ProjectFileEventJob $projectFileEventJob
* @property \Kanboard\Job\NotificationJob $notificationJob
* @property \Psr\Log\LoggerInterface $logger
* @property \PicoDb\Database $db

View File

@@ -1,7 +0,0 @@
<?php
namespace Kanboard\Event;
class FileEvent extends GenericEvent
{
}

View File

@@ -0,0 +1,7 @@
<?php
namespace Kanboard\Event;
class ProjectFileEvent extends GenericEvent
{
}

View File

@@ -0,0 +1,7 @@
<?php
namespace Kanboard\Event;
class TaskFileEvent extends GenericEvent
{
}

View File

@@ -0,0 +1,50 @@
<?php
namespace Kanboard\EventBuilder;
use Kanboard\Event\ProjectFileEvent;
use Kanboard\Event\GenericEvent;
/**
* Class ProjectFileEventBuilder
*
* @package Kanboard\EventBuilder
* @author Frederic Guillot
*/
class ProjectFileEventBuilder extends BaseEventBuilder
{
protected $fileId = 0;
/**
* Set fileId
*
* @param int $fileId
* @return $this
*/
public function withFileId($fileId)
{
$this->fileId = $fileId;
return $this;
}
/**
* Build event data
*
* @access public
* @return GenericEvent|null
*/
public function build()
{
$file = $this->projectFileModel->getById($this->fileId);
if (empty($file)) {
$this->logger->debug(__METHOD__.': File not found');
return null;
}
return new ProjectFileEvent(array(
'file' => $file,
'project' => $this->projectModel->getById($file['project_id']),
));
}
}

View File

@@ -0,0 +1,50 @@
<?php
namespace Kanboard\EventBuilder;
use Kanboard\Event\TaskFileEvent;
use Kanboard\Event\GenericEvent;
/**
* Class TaskFileEventBuilder
*
* @package Kanboard\EventBuilder
* @author Frederic Guillot
*/
class TaskFileEventBuilder extends BaseEventBuilder
{
protected $fileId = 0;
/**
* Set fileId
*
* @param int $fileId
* @return $this
*/
public function withFileId($fileId)
{
$this->fileId = $fileId;
return $this;
}
/**
* Build event data
*
* @access public
* @return GenericEvent|null
*/
public function build()
{
$file = $this->taskFileModel->getById($this->fileId);
if (empty($file)) {
$this->logger->debug(__METHOD__.': File not found');
return null;
}
return new TaskFileEvent(array(
'file' => $file,
'task' => $this->taskFinderModel->getDetails($file['task_id']),
));
}
}

View File

@@ -70,13 +70,8 @@ class NotificationJob extends BaseJob
$values['subtask'] = $this->subtaskModel->getById($event['id'], true);
$values['task'] = $this->taskFinderModel->getDetails($values['subtask']['task_id']);
break;
case 'Kanboard\Event\FileEvent':
$values['file'] = $event;
$values['task'] = $this->taskFinderModel->getDetails($values['file']['task_id']);
break;
case 'Kanboard\Event\CommentEvent':
default:
$values = $event;
break;
}
return $values;

View File

@@ -0,0 +1,45 @@
<?php
namespace Kanboard\Job;
use Kanboard\EventBuilder\ProjectFileEventBuilder;
/**
* Class ProjectFileEventJob
*
* @package Kanboard\Job
* @author Frederic Guillot
*/
class ProjectFileEventJob extends BaseJob
{
/**
* Set job params
*
* @param int $fileId
* @param string $eventName
* @return $this
*/
public function withParams($fileId, $eventName)
{
$this->jobParams = array($fileId, $eventName);
return $this;
}
/**
* Execute job
*
* @param int $fileId
* @param string $eventName
* @return $this
*/
public function execute($fileId, $eventName)
{
$event = ProjectFileEventBuilder::getInstance($this->container)
->withFileId($fileId)
->build();
if ($event !== null) {
$this->dispatcher->dispatch($eventName, $event);
}
}
}

View File

@@ -0,0 +1,45 @@
<?php
namespace Kanboard\Job;
use Kanboard\EventBuilder\TaskFileEventBuilder;
/**
* Class TaskFileEventJob
*
* @package Kanboard\Job
* @author Frederic Guillot
*/
class TaskFileEventJob extends BaseJob
{
/**
* Set job params
*
* @param int $fileId
* @param string $eventName
* @return $this
*/
public function withParams($fileId, $eventName)
{
$this->jobParams = array($fileId, $eventName);
return $this;
}
/**
* Execute job
*
* @param int $fileId
* @param string $eventName
* @return $this
*/
public function execute($fileId, $eventName)
{
$event = TaskFileEventBuilder::getInstance($this->container)
->withFileId($fileId)
->build();
if ($event !== null) {
$this->dispatcher->dispatch($eventName, $event);
}
}
}

View File

@@ -5,7 +5,6 @@ namespace Kanboard\Model;
use Exception;
use Kanboard\Core\Base;
use Kanboard\Core\Thumbnail;
use Kanboard\Event\FileEvent;
use Kanboard\Core\ObjectStorage\ObjectStorageException;
/**
@@ -44,13 +43,13 @@ abstract class FileModel extends Base
abstract protected function getPathPrefix();
/**
* Get event name
* Fire file creation event
*
* @abstract
* @access protected
* @return string
* @param integer $file_id
*/
abstract protected function getEventName();
abstract protected function fireCreationEvent($file_id);
/**
* Get PicoDb query to get all files
@@ -130,16 +129,16 @@ abstract class FileModel extends Base
* Create a file entry in the database
*
* @access public
* @param integer $id Foreign key
* @param string $name Filename
* @param string $path Path on the disk
* @param integer $size File size
* @param integer $foreign_key_id Foreign key
* @param string $name Filename
* @param string $path Path on the disk
* @param integer $size File size
* @return bool|integer
*/
public function create($id, $name, $path, $size)
public function create($foreign_key_id, $name, $path, $size)
{
$values = array(
$this->getForeignKey() => $id,
$this->getForeignKey() => $foreign_key_id,
'name' => substr($name, 0, 255),
'path' => $path,
'is_image' => $this->isImage($name) ? 1 : 0,
@@ -152,8 +151,7 @@ abstract class FileModel extends Base
if ($result) {
$file_id = (int) $this->db->getLastId();
$event = new FileEvent($values + array('file_id' => $file_id));
$this->dispatcher->dispatch($this->getEventName(), $event);
$this->fireCreationEvent($file_id);
return $file_id;
}

View File

@@ -61,14 +61,13 @@ class ProjectFileModel extends FileModel
}
/**
* Get event name
* Fire file creation event
*
* @abstract
* @access protected
* @return string
* @param integer $file_id
*/
protected function getEventName()
protected function fireCreationEvent($file_id)
{
return self::EVENT_CREATE;
$this->queueManager->push($this->projectFileEventJob->withParams($file_id, self::EVENT_CREATE));
}
}

View File

@@ -60,18 +60,6 @@ class TaskFileModel extends FileModel
return 'tasks';
}
/**
* Get event name
*
* @abstract
* @access protected
* @return string
*/
protected function getEventName()
{
return self::EVENT_CREATE;
}
/**
* Get projectId from fileId
*
@@ -101,4 +89,15 @@ class TaskFileModel extends FileModel
$original_filename = e('Screenshot taken %s', $this->helper->dt->datetime(time())).'.png';
return $this->uploadContent($task_id, $original_filename, $blob);
}
/**
* Fire file creation event
*
* @access protected
* @param integer $file_id
*/
protected function fireCreationEvent($file_id)
{
$this->queueManager->push($this->taskFileEventJob->withParams($file_id, self::EVENT_CREATE));
}
}

View File

@@ -28,6 +28,8 @@ class ClassProvider implements ServiceProviderInterface
),
'Job' => array(
'CommentEventJob',
'TaskFileEventJob',
'ProjectFileEventJob',
'NotificationJob',
),
'Model' => array(

View File

@@ -12,28 +12,4 @@ use Kanboard\Core\Base;
*/
class BaseSubscriber extends Base
{
/**
* Method called
*
* @access private
* @var array
*/
private $called = array();
/**
* Check if a listener has been executed
*
* @access public
* @param string $key
* @return boolean
*/
public function isExecuted($key = '')
{
if (isset($this->called[$key])) {
return true;
}
$this->called[$key] = true;
return false;
}
}

View File

@@ -36,12 +36,10 @@ class NotificationSubscriber extends BaseSubscriber implements EventSubscriberIn
public function handleEvent(GenericEvent $event, $eventName)
{
if (!$this->isExecuted($eventName)) {
$this->logger->debug('Subscriber executed: ' . __METHOD__);
$this->logger->debug('Subscriber executed: ' . __METHOD__);
$this->queueManager->push(NotificationJob::getInstance($this->container)
->withParams($event, $eventName, get_class($event))
);
}
$this->queueManager->push(NotificationJob::getInstance($this->container)
->withParams($event, $eventName, get_class($event))
);
}
}

View File

@@ -22,7 +22,7 @@ class ProjectDailySummarySubscriber extends BaseSubscriber implements EventSubsc
public function execute(TaskEvent $event)
{
if (isset($event['project_id']) && !$this->isExecuted()) {
if (isset($event['project_id'])) {
$this->logger->debug('Subscriber executed: '.__METHOD__);
$this->queueManager->push(ProjectMetricJob::getInstance($this->container)->withParams($event['project_id']));
}

View File

@@ -24,7 +24,7 @@ class ProjectModificationDateSubscriber extends BaseSubscriber implements EventS
public function execute(GenericEvent $event)
{
if (isset($event['project_id']) && !$this->isExecuted()) {
if (isset($event['project_id'])) {
$this->logger->debug('Subscriber executed: '.__METHOD__);
$this->projectModel->updateModificationDate($event['project_id']);
}