summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederic Guillot <fred@kanboard.net>2016-07-17 18:47:06 -0400
committerFrederic Guillot <fred@kanboard.net>2016-07-17 18:47:06 -0400
commitcbe52e57200a66522d88dfc5d5f46de8eb87ffb2 (patch)
tree5a8b37d64c0ce4142d19c50a716f1ffb9c47ee7d
parentec0ecc5b0387924f061865f4ec12dbfc5b7018fe (diff)
File events refactoring
-rw-r--r--app/Core/Base.php2
-rw-r--r--app/Event/FileEvent.php7
-rw-r--r--app/Event/ProjectFileEvent.php7
-rw-r--r--app/Event/TaskFileEvent.php7
-rw-r--r--app/EventBuilder/ProjectFileEventBuilder.php50
-rw-r--r--app/EventBuilder/TaskFileEventBuilder.php50
-rw-r--r--app/Job/NotificationJob.php7
-rw-r--r--app/Job/ProjectFileEventJob.php45
-rw-r--r--app/Job/TaskFileEventJob.php45
-rw-r--r--app/Model/FileModel.php22
-rw-r--r--app/Model/ProjectFileModel.php9
-rw-r--r--app/Model/TaskFileModel.php23
-rw-r--r--app/ServiceProvider/ClassProvider.php2
-rw-r--r--app/Subscriber/BaseSubscriber.php24
-rw-r--r--app/Subscriber/NotificationSubscriber.php10
-rw-r--r--app/Subscriber/ProjectDailySummarySubscriber.php2
-rw-r--r--app/Subscriber/ProjectModificationDateSubscriber.php2
-rw-r--r--tests/units/EventBuilder/ProjectFileEventBuilderTest.php33
-rw-r--r--tests/units/EventBuilder/TaskFileEventBuilderTest.php36
-rw-r--r--tests/units/Job/ProjectFileEventJobTest.php43
-rw-r--r--tests/units/Job/TaskFileEventJobTest.php46
21 files changed, 398 insertions, 74 deletions
diff --git a/app/Core/Base.php b/app/Core/Base.php
index e413a4ac..09e04456 100644
--- a/app/Core/Base.php
+++ b/app/Core/Base.php
@@ -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
diff --git a/app/Event/FileEvent.php b/app/Event/FileEvent.php
deleted file mode 100644
index 482a4eab..00000000
--- a/app/Event/FileEvent.php
+++ /dev/null
@@ -1,7 +0,0 @@
-<?php
-
-namespace Kanboard\Event;
-
-class FileEvent extends GenericEvent
-{
-}
diff --git a/app/Event/ProjectFileEvent.php b/app/Event/ProjectFileEvent.php
new file mode 100644
index 00000000..5d57e463
--- /dev/null
+++ b/app/Event/ProjectFileEvent.php
@@ -0,0 +1,7 @@
+<?php
+
+namespace Kanboard\Event;
+
+class ProjectFileEvent extends GenericEvent
+{
+}
diff --git a/app/Event/TaskFileEvent.php b/app/Event/TaskFileEvent.php
new file mode 100644
index 00000000..fa3bdde9
--- /dev/null
+++ b/app/Event/TaskFileEvent.php
@@ -0,0 +1,7 @@
+<?php
+
+namespace Kanboard\Event;
+
+class TaskFileEvent extends GenericEvent
+{
+}
diff --git a/app/EventBuilder/ProjectFileEventBuilder.php b/app/EventBuilder/ProjectFileEventBuilder.php
new file mode 100644
index 00000000..70514a99
--- /dev/null
+++ b/app/EventBuilder/ProjectFileEventBuilder.php
@@ -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']),
+ ));
+ }
+}
diff --git a/app/EventBuilder/TaskFileEventBuilder.php b/app/EventBuilder/TaskFileEventBuilder.php
new file mode 100644
index 00000000..7f1ce3b3
--- /dev/null
+++ b/app/EventBuilder/TaskFileEventBuilder.php
@@ -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']),
+ ));
+ }
+}
diff --git a/app/Job/NotificationJob.php b/app/Job/NotificationJob.php
index cfd0699d..ed568e47 100644
--- a/app/Job/NotificationJob.php
+++ b/app/Job/NotificationJob.php
@@ -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;
diff --git a/app/Job/ProjectFileEventJob.php b/app/Job/ProjectFileEventJob.php
new file mode 100644
index 00000000..d68949c5
--- /dev/null
+++ b/app/Job/ProjectFileEventJob.php
@@ -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);
+ }
+ }
+}
diff --git a/app/Job/TaskFileEventJob.php b/app/Job/TaskFileEventJob.php
new file mode 100644
index 00000000..de2c40db
--- /dev/null
+++ b/app/Job/TaskFileEventJob.php
@@ -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);
+ }
+ }
+}
diff --git a/app/Model/FileModel.php b/app/Model/FileModel.php
index 8cdea9a0..98032f9d 100644
--- a/app/Model/FileModel.php
+++ b/app/Model/FileModel.php
@@ -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;
}
diff --git a/app/Model/ProjectFileModel.php b/app/Model/ProjectFileModel.php
index b464bb2a..4de4d66d 100644
--- a/app/Model/ProjectFileModel.php
+++ b/app/Model/ProjectFileModel.php
@@ -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));
}
}
diff --git a/app/Model/TaskFileModel.php b/app/Model/TaskFileModel.php
index 7603019a..0163da28 100644
--- a/app/Model/TaskFileModel.php
+++ b/app/Model/TaskFileModel.php
@@ -61,18 +61,6 @@ class TaskFileModel extends FileModel
}
/**
- * Get event name
- *
- * @abstract
- * @access protected
- * @return string
- */
- protected function getEventName()
- {
- return self::EVENT_CREATE;
- }
-
- /**
* Get projectId from fileId
*
* @access public
@@ -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));
+ }
}
diff --git a/app/ServiceProvider/ClassProvider.php b/app/ServiceProvider/ClassProvider.php
index 43ade56e..428a8015 100644
--- a/app/ServiceProvider/ClassProvider.php
+++ b/app/ServiceProvider/ClassProvider.php
@@ -28,6 +28,8 @@ class ClassProvider implements ServiceProviderInterface
),
'Job' => array(
'CommentEventJob',
+ 'TaskFileEventJob',
+ 'ProjectFileEventJob',
'NotificationJob',
),
'Model' => array(
diff --git a/app/Subscriber/BaseSubscriber.php b/app/Subscriber/BaseSubscriber.php
index fdea29f6..92441962 100644
--- a/app/Subscriber/BaseSubscriber.php
+++ b/app/Subscriber/BaseSubscriber.php
@@ -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;
- }
}
diff --git a/app/Subscriber/NotificationSubscriber.php b/app/Subscriber/NotificationSubscriber.php
index 6cd7675c..47222f73 100644
--- a/app/Subscriber/NotificationSubscriber.php
+++ b/app/Subscriber/NotificationSubscriber.php
@@ -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))
+ );
}
}
diff --git a/app/Subscriber/ProjectDailySummarySubscriber.php b/app/Subscriber/ProjectDailySummarySubscriber.php
index 6971a121..7e3c11c3 100644
--- a/app/Subscriber/ProjectDailySummarySubscriber.php
+++ b/app/Subscriber/ProjectDailySummarySubscriber.php
@@ -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']));
}
diff --git a/app/Subscriber/ProjectModificationDateSubscriber.php b/app/Subscriber/ProjectModificationDateSubscriber.php
index fee04eaa..97923af9 100644
--- a/app/Subscriber/ProjectModificationDateSubscriber.php
+++ b/app/Subscriber/ProjectModificationDateSubscriber.php
@@ -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']);
}
diff --git a/tests/units/EventBuilder/ProjectFileEventBuilderTest.php b/tests/units/EventBuilder/ProjectFileEventBuilderTest.php
new file mode 100644
index 00000000..bfe22719
--- /dev/null
+++ b/tests/units/EventBuilder/ProjectFileEventBuilderTest.php
@@ -0,0 +1,33 @@
+<?php
+
+use Kanboard\EventBuilder\ProjectFileEventBuilder;
+use Kanboard\Model\ProjectFileModel;
+use Kanboard\Model\ProjectModel;
+
+require_once __DIR__.'/../Base.php';
+
+class ProjectFileEventBuilderTest extends Base
+{
+ public function testWithMissingFile()
+ {
+ $projectFileEventBuilder = new ProjectFileEventBuilder($this->container);
+ $projectFileEventBuilder->withFileId(42);
+ $this->assertNull($projectFileEventBuilder->build());
+ }
+
+ public function testBuild()
+ {
+ $projectModel = new ProjectModel($this->container);
+ $projectFileModel = new ProjectFileModel($this->container);
+ $projectFileEventBuilder = new ProjectFileEventBuilder($this->container);
+
+ $this->assertEquals(1, $projectModel->create(array('name' => 'test1')));
+ $this->assertEquals(1, $projectFileModel->create(1, 'Test', '/tmp/test', 123));
+
+ $event = $projectFileEventBuilder->withFileId(1)->build();
+
+ $this->assertInstanceOf('Kanboard\Event\ProjectFileEvent', $event);
+ $this->assertNotEmpty($event['file']);
+ $this->assertNotEmpty($event['project']);
+ }
+}
diff --git a/tests/units/EventBuilder/TaskFileEventBuilderTest.php b/tests/units/EventBuilder/TaskFileEventBuilderTest.php
new file mode 100644
index 00000000..c253b913
--- /dev/null
+++ b/tests/units/EventBuilder/TaskFileEventBuilderTest.php
@@ -0,0 +1,36 @@
+<?php
+
+use Kanboard\EventBuilder\TaskFileEventBuilder;
+use Kanboard\Model\ProjectModel;
+use Kanboard\Model\TaskCreationModel;
+use Kanboard\Model\TaskFileModel;
+
+require_once __DIR__.'/../Base.php';
+
+class TaskFileEventBuilderTest extends Base
+{
+ public function testWithMissingFile()
+ {
+ $taskFileEventBuilder = new TaskFileEventBuilder($this->container);
+ $taskFileEventBuilder->withFileId(42);
+ $this->assertNull($taskFileEventBuilder->build());
+ }
+
+ public function testBuild()
+ {
+ $taskFileModel = new TaskFileModel($this->container);
+ $taskCreationModel = new TaskCreationModel($this->container);
+ $projectModel = new ProjectModel($this->container);
+ $taskFileEventBuilder = new TaskFileEventBuilder($this->container);
+
+ $this->assertEquals(1, $projectModel->create(array('name' => 'test1')));
+ $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1)));
+ $this->assertEquals(1, $taskFileModel->create(1, 'Test', '/tmp/test', 123));
+
+ $event = $taskFileEventBuilder->withFileId(1)->build();
+
+ $this->assertInstanceOf('Kanboard\Event\TaskFileEvent', $event);
+ $this->assertNotEmpty($event['file']);
+ $this->assertNotEmpty($event['task']);
+ }
+}
diff --git a/tests/units/Job/ProjectFileEventJobTest.php b/tests/units/Job/ProjectFileEventJobTest.php
new file mode 100644
index 00000000..f266d293
--- /dev/null
+++ b/tests/units/Job/ProjectFileEventJobTest.php
@@ -0,0 +1,43 @@
+<?php
+
+use Kanboard\Job\ProjectFileEventJob;
+use Kanboard\Model\ProjectFileModel;
+use Kanboard\Model\ProjectModel;
+
+require_once __DIR__.'/../Base.php';
+
+class ProjectFileEventJobTest extends Base
+{
+ public function testJobParams()
+ {
+ $projectFileEventJob = new ProjectFileEventJob($this->container);
+ $projectFileEventJob->withParams(123, 'foobar');
+
+ $this->assertSame(array(123, 'foobar'), $projectFileEventJob->getJobParams());
+ }
+
+ public function testWithMissingFile()
+ {
+ $this->container['dispatcher']->addListener(ProjectFileModel::EVENT_CREATE, function() {});
+
+ $projectFileEventJob = new ProjectFileEventJob($this->container);
+ $projectFileEventJob->execute(42, ProjectFileModel::EVENT_CREATE);
+
+ $called = $this->container['dispatcher']->getCalledListeners();
+ $this->assertEmpty($called);
+ }
+
+ public function testTriggerEvents()
+ {
+ $this->container['dispatcher']->addListener(ProjectFileModel::EVENT_CREATE, function() {});
+
+ $projectModel = new ProjectModel($this->container);
+ $projectFileModel = new ProjectFileModel($this->container);
+
+ $this->assertEquals(1, $projectModel->create(array('name' => 'test1')));
+ $this->assertEquals(1, $projectFileModel->create(1, 'Test', '/tmp/test', 123));
+
+ $called = $this->container['dispatcher']->getCalledListeners();
+ $this->assertArrayHasKey(ProjectFileModel::EVENT_CREATE.'.closure', $called);
+ }
+}
diff --git a/tests/units/Job/TaskFileEventJobTest.php b/tests/units/Job/TaskFileEventJobTest.php
new file mode 100644
index 00000000..921fe801
--- /dev/null
+++ b/tests/units/Job/TaskFileEventJobTest.php
@@ -0,0 +1,46 @@
+<?php
+
+use Kanboard\Job\TaskFileEventJob;
+use Kanboard\Model\ProjectModel;
+use Kanboard\Model\TaskCreationModel;
+use Kanboard\Model\TaskFileModel;
+
+require_once __DIR__.'/../Base.php';
+
+class TaskFileEventJobTest extends Base
+{
+ public function testJobParams()
+ {
+ $taskFileEventJob = new TaskFileEventJob($this->container);
+ $taskFileEventJob->withParams(123, 'foobar');
+
+ $this->assertSame(array(123, 'foobar'), $taskFileEventJob->getJobParams());
+ }
+
+ public function testWithMissingFile()
+ {
+ $this->container['dispatcher']->addListener(TaskFileModel::EVENT_CREATE, function() {});
+
+ $taskFileEventJob = new TaskFileEventJob($this->container);
+ $taskFileEventJob->execute(42, TaskFileModel::EVENT_CREATE);
+
+ $called = $this->container['dispatcher']->getCalledListeners();
+ $this->assertEmpty($called);
+ }
+
+ public function testTriggerEvents()
+ {
+ $this->container['dispatcher']->addListener(TaskFileModel::EVENT_CREATE, function() {});
+
+ $taskFileModel = new TaskFileModel($this->container);
+ $taskCreationModel = new TaskCreationModel($this->container);
+ $projectModel = new ProjectModel($this->container);
+
+ $this->assertEquals(1, $projectModel->create(array('name' => 'test1')));
+ $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1)));
+ $this->assertEquals(1, $taskFileModel->create(1, 'Test', '/tmp/test', 123));
+
+ $called = $this->container['dispatcher']->getCalledListeners();
+ $this->assertArrayHasKey(TaskFileModel::EVENT_CREATE.'.closure', $called);
+ }
+}