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() { getParams: function() {
// get all route parameters (without the "splat") // get all route parameters (without the "splat")
let params = _.omit(this.props.params, 'splat'); let params = _.omit(this.props.params, 'splat');
// TO REFACTOR: // TODO:
// find a way to set the "type" in the routes definition // find a way to set the "type" in the routes definition
// so that it appears in `this.props.params`
if (this.props.type) { if (this.props.type) {
params.type = this.props.type; params.type = this.props.type;
} }

View File

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