Some refactoring.

This commit is contained in:
Alexey Skobkin 2015-06-23 12:38:43 +03:00
parent f83d719299
commit 6384527720
7 changed files with 73 additions and 34 deletions

View file

@ -13,38 +13,10 @@ class MainController extends Controller
/** @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)')
->innerJoin('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,
'users_count' => $em->getRepository('SkobkinPointToolsBundle:User')->getUsersCount(),
'subscribers_count' => $em->getRepository('SkobkinPointToolsBundle:Subscription')->getUserSubscribersCountById($this->container->getParameter('point_id')),
'events_count' => $em->getRepository('SkobkinPointToolsBundle:SubscriptionEvent')->getLastDayEventsCount(),
'service_login' => $this->container->getParameter('point_login'),
]);
}

View file

@ -10,7 +10,7 @@ use Doctrine\ORM\Mapping as ORM;
* @ORM\Table(name="subscriptions.subscriptions", uniqueConstraints={
* @ORM\UniqueConstraint(name="subscription_unique", columns={"author_id", "subscriber_id"})}
* )
* @ORM\Entity
* @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Entity\SubscriptionRepository")
*/
class Subscription
{

View file

@ -12,7 +12,7 @@ use Doctrine\ORM\Mapping as ORM;
* @ORM\Index(name="subscriber_idx", columns={"subscriber_id"}),
* @ORM\Index(name="date_idx", columns={"date"})
* })
* @ORM\Entity
* @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Entity\SubscriptionEventRepository")
* @ORM\HasLifecycleCallbacks
*/
class SubscriptionEvent

View file

@ -0,0 +1,25 @@
<?php
namespace Skobkin\Bundle\PointToolsBundle\Entity;
use Doctrine\ORM\EntityRepository;
class SubscriptionEventRepository extends EntityRepository
{
/**
* @return integer
*/
public function getLastDayEventsCount()
{
$qb = $this->createQueryBuilder('se');
$now = new \DateTime();
$eventsCount = $qb
->select('COUNT(se)')
->where('se.date > :time')
->setParameter('time', $now->sub(new \DateInterval('PT24H')))
->getQuery()->getSingleScalarResult()
;
}
}

View file

@ -0,0 +1,24 @@
<?php
namespace Skobkin\Bundle\PointToolsBundle\Entity;
use Doctrine\ORM\EntityRepository;
class SubscriptionRepository extends EntityRepository
{
/**
* @param integer $id
* @return integer
*/
public function getUserSubscribersCountById($id)
{
$qb = $this->createQueryBuilder('s');
return $qb
->select('COUNT(s)')
->innerJoin('s.author', 'a')
->where('a.id = :id')
->setParameter('id', $id)
->getQuery()->getSingleScalarResult()
;
}
}

View file

@ -9,7 +9,7 @@ use Doctrine\ORM\Mapping as ORM;
* User
*
* @ORM\Table(name="users.users")
* @ORM\Entity
* @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Entity\UserRepository")
* @ORM\HasLifecycleCallbacks
*/
class User

View file

@ -0,0 +1,18 @@
<?php
namespace Skobkin\Bundle\PointToolsBundle\Entity;
use Doctrine\ORM\EntityRepository;
class UserRepository extends EntityRepository
{
/**
* @return integer
*/
public function getUsersCount()
{
$qb = $this->createQueryBuilder('u');
return $qb->select('COUNT(u)')->getQuery()->getSingleScalarResult();
}
}