PHP MVC controller code needs diet? This is the dashboard controller code in PHP Symfony 2. It collects some aggregate data and points for charts, but i don't like it very much. Do you think that this code belongs to what a controller should do in a MVC patter? How can i refactor it?

public function dashboardAction()
{
    $bag = array();

    // Helpers from service container
    $messaging = $this-&gt;get('messaging.helper');
    $charting  = $this-&gt;get('charting.helper');

    // Current logged user, subscription and message inhibitor
    $loggedUser   = $this-&gt;getSecurityContext()-&gt;getToken()-&gt;getUser();
    $subscription = $messaging-&gt;createSubscriptionFromUser($loggedUser);
    $usage        = $messaging-&gt;createUsageFromSubscription($subscription);

    $bag['subscription'] = $subscription;
    $bag['usage']        = $usage;
    $bag['inhibitor']    = $messaging-&gt;createInhibitor($usage, $subscription);

    // Get charts from usage raw data
    $rawData = $usage-&gt;getRawData();
    $start   = $usage-&gt;getStartDate();
    $end     = $usage-&gt;getEndDate();

    $bag['line_chart'] = $charting-&gt;createMessagesLineChart($rawData, $start, $end);
    $bag['pie_chart']  = $charting-&gt;createMessagesPieChart($rawData);

    // Customers and latest 5 added
    $customers = $loggedUser-&gt;getCustomers();

    $bag['customers_count']  = $customers-&gt;count();
    $bag['latest_customers'] = $customers-&gt;slice(0, 5);

    // Tags/keywords counts and latest 5 meta
    $metas       = $loggedUser-&gt;getMeta();
    $getTags     = function($m) { return $m instanceof Tag; };
    $getKeywords = function($m) { return $m instanceof Keyword; };

    $bag['tags_count']     = $metas-&gt;filter($getTags)-&gt;count();
    $bag['keywords_count'] = $metas-&gt;filter($getKeywords)-&gt;count();
    $bag['latest_meta']    = $metas-&gt;slice(0, 5);

    // SMS and newsletter counts
    $outgoingMessages = $loggedUser-&gt;getOutgoingMessages();
    $getSms           = function($m) { return $m instanceof SmallTextMessage; };
    $getNewsletters   = function($m) { return $m instanceof Newsletter; };

    $bag['sms_count']        = $outgoingMessages-&gt;filter($getSms)-&gt;count();
    $bag['newsletter_count'] = $outgoingMessages-&gt;filter($getNewsletters)-&gt;count();

    return $bag;
}
A controller? No. A model? Sure. Not sure if Symfony defines a controller differently, but this is not what I think of as a controller.

As for refactoring, the easiest way would be to break it up into smaller methods. For instance, right off the bat I see at least 6 different potential methods here. You could create a bunch of smaller methods that returns a part of the bag (messaging, charting, etc...) and then merge them all together. In fact, that's the only real improvement I can see. I feel bad leaving such a short answer, so here's an example of a couple of those smaller methods, which is actually about half that code.

private function getUsageCharts ( $usage ) {
    $rawData = $usage-&gt;getRawData();
    $start   = $usage-&gt;getStartDate();
    $end     = $usage-&gt;getEndDate();

    $line_chart = $charting-&gt;createMessagesLineChart($rawData, $start, $end);
    $pie_chart  = $charting-&gt;createMessagesPieChart($rawData);

    return compact( 'usage', 'line_chart', 'pie_chart' );
}

private function getMessagingFields( $loggedUser ) {
    $messaging = $this-&gt;get('messaging.helper');

    $subscription = $messaging-&gt;createSubscriptionFromUser($loggedUser);
    $usage = $messaging-&gt;createUsageFromSubscription($subscription);
    $inhibitor = $messaging-&gt;createInhibitor($usage, $subscription);

    extract( $this-&gt;getUsageCharts( $usage ) );//$line_chart, $pie_chart
    return compact( 'subscription', 'usage', 'inhibitor', 'line_chard', 'pie_chart' );
}


Hope this helps!