Statistics for main page.

This commit is contained in:
Alexey Skobkin 2015-05-30 09:22:06 +03:00
parent 20c44371c9
commit 097488446a
4 changed files with 79 additions and 3 deletions

View file

@ -0,0 +1,51 @@
<?php
namespace Skobkin\Bundle\PointToolsBundle\Controller;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\QueryBuilder;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class MainController extends Controller
{
public function indexAction()
{
/** @var EntityManager $em */
$em = $this->getDoctrine()->getManager();
/** @var QueryBuilder $qb */
$qb = $em->getRepository('SkobkinPointToolsBundle:User')->createQueryBuilder('u');
// All users in the system count
$usersCount = $qb->select('COUNT(u)')->getQuery()->getSingleScalarResult();
$qb = $em->getRepository('SkobkinPointToolsBundle:Subscription')->createQueryBuilder('s');
// Service subscribers count
$subscribersCount = $qb
->select('COUNT(s)')
->join('s.author', 'a')
->where('a.login = :login')
->setParameter('login', $this->container->getParameter('point_login'))
->getQuery()->getSingleScalarResult()
;
$qb = $em->getRepository('SkobkinPointToolsBundle:SubscriptionEvent')->createQueryBuilder('se');
$now = new \DateTime();
$eventsCount = $qb
->select('COUNT(se)')
->where('se.date > :time')
->setParameter('time', $now->sub(new \DateInterval('PT24H')))
->getQuery()->getSingleScalarResult()
;
return $this->render('SkobkinPointToolsBundle:Main:index.html.twig', [
'users_count' => $usersCount,
'subscribers_count' => $subscribersCount,
'events_count' => $eventsCount,
]);
}
}

View file

@ -1,3 +1,3 @@
#skobkin_point_tools_homepage:
# path: /hello/{name}
# defaults: { _controller: SkobkinPointToolsBundle:Default:index }
index:
path: /
defaults: { _controller: SkobkinPointToolsBundle:Main:index }

View file

@ -0,0 +1,9 @@
{% extends "::base.html.twig" %}
{% block title %}SkobkinPointToolsBundle:Main:index{% endblock %}
{% block body %}
<h1>{{ users_count }}</h1>
<h1>{{ subscribers_count }}</h1>
<h1>{{ events_count }}</h1>
{% endblock %}

View file

@ -0,0 +1,16 @@
<?php
namespace Skobkin\Bundle\PointToolsBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class MainControllerTest extends WebTestCase
{
public function testIndex()
{
$client = static::createClient();
$crawler = $client->request('GET', '/');
}
}