Skip to content

Commit c3986e4

Browse files
committed
- (API) Added the new Queue Service to handle sending items to the queue
1 parent 99d7125 commit c3986e4

4 files changed

Lines changed: 99 additions & 0 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace Craft;
3+
4+
class Postmaster_QueueController extends BaseController
5+
{
6+
public function actionMarshal()
7+
{
8+
craft()->postmaster_queue->marshal();
9+
}
10+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
namespace Craft;
3+
4+
class m141212_003800_postmaster_queue_table extends BaseMigration
5+
{
6+
public function safeUp()
7+
{
8+
$this->createTable('postmasterqueue', array(
9+
'model' => array(AttributeType::Mixed, 'column' => ColumnType::LongText),
10+
'sendDate' => array(AttributeType::Mixed, 'column' => ColumnType::LongText),
11+
));
12+
13+
return true;
14+
}
15+
}

records/Postmaster_QueueRecord.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
namespace Craft;
3+
4+
class Postmaster_QueueRecord extends BaseRecord
5+
{
6+
public function getTableName()
7+
{
8+
return 'postmasterqueue';
9+
}
10+
11+
protected function defineAttributes()
12+
{
13+
return array(
14+
'recordId' => array(AttributeType::Number, 'column' => ColumnType::Int),
15+
'recordType' => array(AttributeType::String, 'column' => ColumnType::Text),
16+
'model' => array(AttributeType::Mixed, 'column' => ColumnType::LongText),
17+
'sendDate' => array(AttributeType::Mixed, 'column' => ColumnType::LongText),
18+
);
19+
}
20+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
namespace Craft;
3+
4+
class Postmaster_QueueService extends BaseApplicationComponent
5+
{
6+
public function query()
7+
{
8+
$record = new Postmaster_QueueRecord();
9+
10+
return craft()->db->createCommand()
11+
->select('*')
12+
->from($record->getTableName());
13+
}
14+
15+
public function remove($id)
16+
{
17+
return (new Postmaster_QueueRecord())->deleteByPk($id);
18+
}
19+
20+
public function marshal()
21+
{
22+
foreach($this->expired() as $model)
23+
{
24+
craft()->postmaster->send($model);
25+
}
26+
}
27+
28+
public function expired()
29+
{
30+
$data = $this->query()->where('sendDate <= :date', array(':date' => new DateTime()));
31+
32+
return $this->_populateModelsFromArray($data->queryAll());
33+
}
34+
35+
protected function _populateModelsFromArray(Array $array)
36+
{
37+
$models = array();
38+
39+
foreach($array as $row)
40+
{
41+
$model = json_decode($row['model'], true);
42+
$model = ModelHelper::expandModelsInArray($model);
43+
44+
$class = $model['service']['__class__'];
45+
46+
$model->service = new $class($model->service);
47+
$model->queueId = $row['id'];
48+
49+
$models[] = new Postmaster_TransportModel($model);
50+
}
51+
52+
return $models;
53+
}
54+
}

0 commit comments

Comments
 (0)