refactored Models/Newsletter::getStatistics method to avoid duplication

- replaced "TO REFACTOR" with more conventional "TODO"
This commit is contained in:
Jonathan Labreuille
2016-07-19 15:34:14 +02:00
parent 0706450f9a
commit 12c036dbef
2 changed files with 35 additions and 55 deletions

View File

@@ -351,8 +351,9 @@ const Listing = React.createClass({
getParams: function() {
// get all route parameters (without the "splat")
let params = _.omit(this.props.params, 'splat');
// TO REFACTOR:
// TODO:
// find a way to set the "type" in the routes definition
// so that it appears in `this.props.params`
if (this.props.type) {
params.type = this.props.type;
}

View File

@@ -56,7 +56,6 @@ class Newsletter extends Model {
}
function duplicate($data = array()) {
// get current newsletter's data as an array
$newsletter_data = $this->asArray();
// remove id so that it creates a new record
@@ -110,13 +109,11 @@ class Newsletter extends Model {
}
function createNotificationHistory() {
// get current newsletter's data as an array
$newsletter_data = $this->asArray();
// remove id so that it creates a new record
unset($newsletter_data['id']);
// update newsletter columns
$data = array_merge(
$newsletter_data,
array(
@@ -245,59 +242,41 @@ class Newsletter extends Model {
}
function getStatistics() {
switch($this->type) {
case self::TYPE_WELCOME:
return SendingQueue::tableAlias('queues')
->selectExpr(
'COUNT(DISTINCT(clicks.subscriber_id)) as clicked, ' .
'COUNT(DISTINCT(opens.subscriber_id)) as opened, ' .
'COUNT(DISTINCT(unsubscribes.subscriber_id)) as unsubscribed '
)
->leftOuterJoin(
MP_STATISTICS_CLICKS_TABLE,
'queues.id = clicks.queue_id',
'clicks'
)
->leftOuterJoin(
MP_STATISTICS_OPENS_TABLE,
'queues.id = opens.queue_id',
'opens'
)
->leftOuterJoin(
MP_STATISTICS_UNSUBSCRIBES_TABLE,
'queues.id = unsubscribes.queue_id',
'unsubscribes'
)
->where('queues.newsletter_id', $this->id)
->where('queues.status', SendingQueue::STATUS_COMPLETED)
->findOne();
default:
if($this->queue === false) {
return false;
}
return SendingQueue::tableAlias('queues')
->selectExpr(
'COUNT(DISTINCT(clicks.subscriber_id)) as clicked, ' .
'COUNT(DISTINCT(opens.subscriber_id)) as opened, ' .
'COUNT(DISTINCT(unsubscribes.subscriber_id)) as unsubscribed '
)
->leftOuterJoin(
MP_STATISTICS_CLICKS_TABLE,
'queues.id = clicks.queue_id',
'clicks'
)
->leftOuterJoin(
MP_STATISTICS_OPENS_TABLE,
'queues.id = opens.queue_id',
'opens'
)
->leftOuterJoin(
MP_STATISTICS_UNSUBSCRIBES_TABLE,
'queues.id = unsubscribes.queue_id',
'unsubscribes'
)
$statistics_query = SendingQueue::tableAlias('queues')
->selectExpr(
'COUNT(DISTINCT(clicks.subscriber_id)) as clicked, ' .
'COUNT(DISTINCT(opens.subscriber_id)) as opened, ' .
'COUNT(DISTINCT(unsubscribes.subscriber_id)) as unsubscribed '
)
->leftOuterJoin(
MP_STATISTICS_CLICKS_TABLE,
'queues.id = clicks.queue_id',
'clicks'
)
->leftOuterJoin(
MP_STATISTICS_OPENS_TABLE,
'queues.id = opens.queue_id',
'opens'
)
->leftOuterJoin(
MP_STATISTICS_UNSUBSCRIBES_TABLE,
'queues.id = unsubscribes.queue_id',
'unsubscribes'
);
if($this->type === self::TYPE_WELCOME) {
return $statistics_query
->where('queues.newsletter_id', $this->id)
->where('queues.status', SendingQueue::STATUS_COMPLETED)
->findOne();
} else {
if($this->queue === false) {
return false;
} else {
return $statistics_query
->where('queues.id', $this->queue['id'])
->findOne();
}
}
}