Skip to content

Commit 341136a

Browse files
committed
- (Feature) Added new craft()->postmaster->transportResponses()
- (Feature) Added craft.postmaster.transportResponse() to the template variables - (API) Added new Postmaster_TransportResponseService - (API) Added new Postmaster_TransportResponseCriteria
1 parent 0842cff commit 341136a

File tree

8 files changed

+486
-0
lines changed

8 files changed

+486
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
namespace Craft;
3+
4+
class Postmaster_TransportResponseCriteriaModel extends BaseModel
5+
{
6+
protected $_cachedIds;
7+
8+
protected $_cachedTotal;
9+
10+
protected $_matchedResponses;
11+
12+
protected $_matchedResponsesAtOffsets;
13+
14+
public function find($attributes = null)
15+
{
16+
$this->setAttributes($attributes);
17+
18+
if (!isset($this->_matchedResponses))
19+
{
20+
$parcels = craft()->postmaster_transportResponse->find($this);
21+
22+
$this->setMatchedParcels($parcels);
23+
}
24+
25+
return $this->_matchedResponses;
26+
}
27+
28+
public function ids($attributes = null)
29+
{
30+
$this->setAttributes($attributes);
31+
32+
if (!isset($this->_cachedIds))
33+
{
34+
foreach($this->find() as $row)
35+
{
36+
$this->_cachedIds[] = $row->id;
37+
}
38+
}
39+
40+
return $this->_cachedIds;
41+
}
42+
43+
public function total($attributes = null)
44+
{
45+
$this->setAttributes($attributes);
46+
47+
if (!isset($this->_cachedTotal))
48+
{
49+
$this->_cachedTotal = count($this->find());
50+
}
51+
52+
return $this->_cachedTotal;
53+
}
54+
55+
public function first($attributes = null)
56+
{
57+
$this->setAttributes($attributes);
58+
59+
return $this->nth(0);
60+
}
61+
62+
public function last($attributes = null)
63+
{
64+
$this->setAttributes($attributes);
65+
66+
$total = $this->total();
67+
68+
if ($total)
69+
{
70+
return $this->nth($total-1);
71+
}
72+
}
73+
74+
public function nth($offset)
75+
{
76+
if (!isset($this->_matchedResponsesAtOffsets) || !array_key_exists($offset, $this->_matchedResponsesAtOffsets))
77+
{
78+
$criteria = new Postmaster_TransportResponseCriteriaModel($this->getAttributes());
79+
$criteria->offset = $offset;
80+
$criteria->limit = 1;
81+
$elements = $criteria->find();
82+
83+
if ($elements)
84+
{
85+
$this->_matchedResponsesAtOffsets[$offset] = $elements[0];
86+
}
87+
else
88+
{
89+
$this->_matchedResponsesAtOffsets[$offset] = null;
90+
}
91+
}
92+
93+
return $this->_matchedResponsesAtOffsets[$offset];
94+
}
95+
96+
public function copy()
97+
{
98+
$class = get_class($this);
99+
100+
return new $class($this->getAttributes());
101+
}
102+
103+
public function setAttribute($name, $value)
104+
{
105+
if (in_array($name, $this->attributeNames()) && $this->getAttribute($name) === $value)
106+
{
107+
return true;
108+
}
109+
110+
if (parent::setAttribute($name, $value))
111+
{
112+
$this->_matchedResponses = null;
113+
$this->_matchedResponsesAtOffsets = null;
114+
$this->_cachedIds = null;
115+
$this->_cachedTotal = null;
116+
117+
return true;
118+
}
119+
else
120+
{
121+
return false;
122+
}
123+
}
124+
125+
public function setMatchedParcels($parcels)
126+
{
127+
$this->_matchedResponses = $parcels;
128+
129+
// Store them by offset, too
130+
$offset = $this->offset;
131+
132+
foreach ($this->_matchedResponses as $parcels)
133+
{
134+
$this->_matchedResponsesAtOffsets[$offset] = $parcels;
135+
$offset++;
136+
}
137+
}
138+
139+
protected function defineAttributes()
140+
{
141+
return array(
142+
'title' => AttributeType::String,
143+
'uid' => AttributeType::String,
144+
'id' => AttributeType::Number,
145+
'enabled' => AttributeType::Number,
146+
'limit' => array('default' => 100, AttributeType::Number),
147+
'order' => array('default' => 'id', AttributeType::String),
148+
'offset' => array('default' => 0, AttributeType::String),
149+
'sort' => array('default' => 'desc', AttributeType::String)
150+
);
151+
}
152+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
namespace Craft;
3+
4+
use Craft\Plugins\Postmaster\Interfaces\ResponseInterface;
5+
6+
class Postmaster_TransportResponseModel extends BaseModel implements ResponseInterface
7+
{
8+
public function __toString()
9+
{
10+
return $this->success;
11+
}
12+
13+
public function getModel()
14+
{
15+
return $this->model;
16+
}
17+
18+
public function setModel($model)
19+
{
20+
$this->model = $model;
21+
}
22+
23+
public function getSuccess()
24+
{
25+
return $this->success;
26+
}
27+
28+
public function setSuccess($value)
29+
{
30+
$this->success = $value;
31+
}
32+
33+
public function getCode()
34+
{
35+
return $this->code;
36+
}
37+
38+
public function setCode($code)
39+
{
40+
$this->code = $code;
41+
}
42+
43+
public function setErrors(Array $errors = array())
44+
{
45+
$this->errors = $errors;
46+
}
47+
48+
public function getErrors()
49+
{
50+
return $this->errors;
51+
}
52+
53+
public function addErrors(Array $errors = array())
54+
{
55+
foreach($errors as $error)
56+
{
57+
$this->addError($error);
58+
}
59+
}
60+
61+
public function addError($error)
62+
{
63+
$errors = $this->errors;
64+
$errors[] = $error;
65+
$this->errors = $errors;
66+
}
67+
68+
public function save()
69+
{
70+
$record = new Postmaster_TransportResponseRecord();
71+
$record->success = $this->getSuccess();
72+
$record->errors = $this->getErrors();
73+
$record->code = $this->getCode();
74+
$record->model = $this->model;
75+
$record->service = $this->model->service->name;
76+
77+
$record->save();
78+
}
79+
80+
protected function defineAttributes()
81+
{
82+
return array(
83+
'success' => array(AttributeType::Bool, 'default' => true),
84+
'code' => array(AttributeType::Number, 'default' => 200),
85+
'errors' => array(AttributeType::Mixed, 'default' => array()),
86+
'service' => AttributeType::String,
87+
'model' => AttributeType::Mixed,
88+
'dateCreated' => array(AttributeType::Mixed, 'default' => DateTimeHelper::formatTimeForDb())
89+
);
90+
}
91+
}

records/Postmaster_TransportResponseRecord.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ protected function defineAttributes()
1313
return array(
1414
'success' => array(AttributeType::Bool, 'column' => ColumnType::Int),
1515
'errors' => array(AttributeType::Mixed, 'column' => ColumnType::LongText),
16+
'code' => array(AttributeType::String, 'column' => ColumnType::Text),
1617
'service' => array(AttributeType::Mixed, 'column' => ColumnType::Text),
1718
'model' => array(AttributeType::Mixed, 'column' => ColumnType::LongText),
1819
);

services/PostmasterService.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public function parcels($criteria = false)
2121
return new Postmaster_ParcelCriteriaModel($criteria ?: array());
2222
}
2323

24+
public function transportResponses($criteria = false)
25+
{
26+
return new Postmaster_TransportResponseCriteriaModel($criteria ?: array());
27+
}
28+
2429
public function service($class, $settings = null)
2530
{
2631
if(!is_array($settings))
@@ -61,6 +66,12 @@ public function send(Postmaster_TransportModel $model)
6166
// exchange for a Craft\Plugins\Postmaster\Responses\TransportResponse object
6267
$response = $model->service->send($model);
6368

69+
// Test the service response for correct class and throw an error if it fails
70+
if(!$response instanceof \Craft\Postmaster_TransportResponseModel)
71+
{
72+
throw new Exception('The '.$model->service->name.' service did not return a \Craft\Postmaster_TransportResponseModel');
73+
}
74+
6475
// Trigger the onAfterSend method
6576
$model->service->onAfterSend();
6677

0 commit comments

Comments
 (0)