Controllers small refactoring.

This commit is contained in:
Alexey Skobkin 2017-11-05 05:03:26 +03:00
parent b8f912b91d
commit 00526e5f19
10 changed files with 74 additions and 28 deletions

View file

@ -10,11 +10,6 @@ services:
resource: '../../src/Skobkin/Bundle/PointToolsBundle/*' resource: '../../src/Skobkin/Bundle/PointToolsBundle/*'
exclude: '../../src/Skobkin/Bundle/PointToolsBundle/{DataFixtures,DependencyInjection,DQL,DTO,Entity,Exception,Repository,Twig}' exclude: '../../src/Skobkin/Bundle/PointToolsBundle/{DataFixtures,DependencyInjection,DQL,DTO,Entity,Exception,Repository,Twig}'
Skobkin\Bundle\PointToolsBundle\Controller\:
resource: '../../src/Skobkin/Bundle/PointToolsBundle/Controller'
public: true
tags: ['controller.service_arguments']
# HTTP client for Telegram # HTTP client for Telegram
app.http.telegram_client: app.http.telegram_client:
class: GuzzleHttp\Client class: GuzzleHttp\Client
@ -48,6 +43,26 @@ services:
tags: tags:
- { name: monolog.logger, channel: subscribers_update } - { name: monolog.logger, channel: subscribers_update }
# Controllers
Skobkin\Bundle\PointToolsBundle\Controller\:
resource: '../../src/Skobkin/Bundle/PointToolsBundle/Controller'
public: true
tags: ['controller.service_arguments']
Skobkin\Bundle\PointToolsBundle\Controller\MainController:
arguments:
$appUserId: '%point_id%'
$appUserLogin: '%point_login%'
Skobkin\Bundle\PointToolsBundle\Controller\Api\CrawlerController:
arguments:
$crawlerToken: '%crawler_token%'
Skobkin\Bundle\PointToolsBundle\Controller\Telegram\WebHookController:
arguments:
$telegramToken: '%telegram_token%'
$debug: '%kernel.debug%'
# Console commands # Console commands
# @todo https://github.com/symfony/symfony/blob/3.4/UPGRADE-3.4.md#httpkernel # @todo https://github.com/symfony/symfony/blob/3.4/UPGRADE-3.4.md#httpkernel

View file

@ -2,10 +2,10 @@
namespace Skobkin\Bundle\PointToolsBundle\Controller\Api; namespace Skobkin\Bundle\PointToolsBundle\Controller\Api;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
class AbstractApiController extends Controller class AbstractApiController extends AbstractController
{ {
protected function createSuccessResponse($data, int $code = 200): Response protected function createSuccessResponse($data, int $code = 200): Response
{ {

View file

@ -10,12 +10,19 @@ use Symfony\Component\HttpFoundation\{Request, Response};
class CrawlerController extends AbstractApiController class CrawlerController extends AbstractApiController
{ {
/** @var string */
private $crawlerToken;
public function __construct(string $crawlerToken)
{
$this->crawlerToken = $crawlerToken;
}
public function receiveAllPageAction(Request $request, Serializer $serializer, PostFactory $postFactory, EntityManager $em): Response public function receiveAllPageAction(Request $request, Serializer $serializer, PostFactory $postFactory, EntityManager $em): Response
{ {
$remoteToken = $request->request->get('token'); $remoteToken = $request->request->get('token');
$localToken = $this->getParameter('crawler_token');
if (!$localToken || ($localToken !== $remoteToken)) { if (!$this->crawlerToken || ($this->crawlerToken !== $remoteToken)) {
return $this->createErrorResponse('Token error. Please check it in crawler and API parameters.', Response::HTTP_FORBIDDEN); return $this->createErrorResponse('Token error. Please check it in crawler and API parameters.', Response::HTTP_FORBIDDEN);
} }

View file

@ -5,10 +5,10 @@ namespace Skobkin\Bundle\PointToolsBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Skobkin\Bundle\PointToolsBundle\Entity\{SubscriptionEvent, User}; use Skobkin\Bundle\PointToolsBundle\Entity\{SubscriptionEvent, User};
use Skobkin\Bundle\PointToolsBundle\Repository\SubscriptionEventRepository; use Skobkin\Bundle\PointToolsBundle\Repository\SubscriptionEventRepository;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
class ApiController extends Controller class ApiController
{ {
/** /**
* Returns last user subscribers log * Returns last user subscribers log
@ -38,6 +38,6 @@ class ApiController extends Controller
]; ];
} }
return $this->json($data); return new JsonResponse($data);
} }
} }

View file

@ -4,10 +4,10 @@ namespace Skobkin\Bundle\PointToolsBundle\Controller;
use Knp\Component\Pager\Paginator; use Knp\Component\Pager\Paginator;
use Skobkin\Bundle\PointToolsBundle\Repository\SubscriptionEventRepository; use Skobkin\Bundle\PointToolsBundle\Repository\SubscriptionEventRepository;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\{Request, Response}; use Symfony\Component\HttpFoundation\{Request, Response};
class EventsController extends Controller class EventsController extends AbstractController
{ {
public function lastAction(Request $request, SubscriptionEventRepository $eventRepository, Paginator $paginator): Response public function lastAction(Request $request, SubscriptionEventRepository $eventRepository, Paginator $paginator): Response
{ {

View file

@ -5,14 +5,26 @@ namespace Skobkin\Bundle\PointToolsBundle\Controller;
use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManager;
use Skobkin\Bundle\PointToolsBundle\Form\UserSearchType; use Skobkin\Bundle\PointToolsBundle\Form\UserSearchType;
use Skobkin\Bundle\PointToolsBundle\Repository\{SubscriptionEventRepository, SubscriptionRepository, UserRepository}; use Skobkin\Bundle\PointToolsBundle\Repository\{SubscriptionEventRepository, SubscriptionRepository, UserRepository};
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormError; use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\{JsonResponse, Request, Response}; use Symfony\Component\HttpFoundation\{JsonResponse, Request, Response};
class MainController extends Controller class MainController extends AbstractController
{ {
const AJAX_AUTOCOMPLETE_SIZE = 10; const AJAX_AUTOCOMPLETE_SIZE = 10;
/** @var int */
private $appUserId;
/** @var string */
private $appUserLogin;
public function __construct(int $appUserId, string $appUserLogin)
{
$this->appUserId = $appUserId;
$this->appUserLogin = $appUserLogin;
}
public function indexAction( public function indexAction(
Request $request, Request $request,
UserRepository $userRepository, UserRepository $userRepository,
@ -46,9 +58,9 @@ class MainController extends Controller
'form' => $form->createView(), 'form' => $form->createView(),
'autocomplete_size' => self::AJAX_AUTOCOMPLETE_SIZE, 'autocomplete_size' => self::AJAX_AUTOCOMPLETE_SIZE,
'users_count' => $userRepository->getUsersCount(), 'users_count' => $userRepository->getUsersCount(),
'subscribers_count' => $subscriptionRepository->getUserSubscribersCountById($this->getParameter('point_id')), 'subscribers_count' => $subscriptionRepository->getUserSubscribersCountById($this->appUserId),
'events_count' => $subscriptionEventRepository->getLastDayEventsCount(), 'events_count' => $subscriptionEventRepository->getLastDayEventsCount(),
'service_login' => $this->getParameter('point_login'), 'service_login' => $this->appUserLogin,
]); ]);
} }

View file

@ -4,11 +4,11 @@ namespace Skobkin\Bundle\PointToolsBundle\Controller;
use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post; use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post;
use Skobkin\Bundle\PointToolsBundle\Repository\Blogs\PostRepository; use Skobkin\Bundle\PointToolsBundle\Repository\Blogs\PostRepository;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
class PostController extends Controller class PostController extends AbstractController
{ {
/** /**
* @ParamConverter("post", class="SkobkinPointToolsBundle:Blogs\Post") * @ParamConverter("post", class="SkobkinPointToolsBundle:Blogs\Post")

View file

@ -4,10 +4,10 @@ namespace Skobkin\Bundle\PointToolsBundle\Controller;
use Knp\Component\Pager\Paginator; use Knp\Component\Pager\Paginator;
use Skobkin\Bundle\PointToolsBundle\Repository\Blogs\PostRepository; use Skobkin\Bundle\PointToolsBundle\Repository\Blogs\PostRepository;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
class PublicFeedController extends Controller class PublicFeedController extends AbstractController
{ {
private const POSTS_PER_PAGE = 20; private const POSTS_PER_PAGE = 20;

View file

@ -4,18 +4,30 @@ namespace Skobkin\Bundle\PointToolsBundle\Controller\Telegram;
use Skobkin\Bundle\PointToolsBundle\Service\Telegram\IncomingUpdateDispatcher; use Skobkin\Bundle\PointToolsBundle\Service\Telegram\IncomingUpdateDispatcher;
use Symfony\Bridge\Monolog\Logger; use Symfony\Bridge\Monolog\Logger;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\{JsonResponse, Request, Response}; use Symfony\Component\HttpFoundation\{JsonResponse, Request, Response};
use unreal4u\TelegramAPI\Telegram\Types\Update; use unreal4u\TelegramAPI\Telegram\Types\Update;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
class WebHookController extends Controller class WebHookController extends AbstractController
{ {
/** @var string */
private $telegramToken;
/** @var bool */
private $debug;
public function __construct(string $telegramToken, bool $debug)
{
$this->telegramToken = $telegramToken;
$this->debug = $debug;
}
public function receiveUpdateAction(Request $request, string $token, IncomingUpdateDispatcher $updateDispatcher, Logger $logger): Response public function receiveUpdateAction(Request $request, string $token, IncomingUpdateDispatcher $updateDispatcher, Logger $logger): Response
{ {
if ($token !== $savedToken = $this->getParameter('telegram_token')) { if ($token !== $savedToken = $this->telegramToken) {
throw $this->createNotFoundException(); throw $this->createNotFoundException();
} }
@ -29,7 +41,7 @@ class WebHookController extends Controller
try { try {
$updateDispatcher->process($update); $updateDispatcher->process($update);
} catch (\Exception $e) { } catch (\Exception $e) {
if ($this->getParameter('kernel.debug')) { if ($this->debug) {
throw $e; throw $e;
} }

View file

@ -6,12 +6,12 @@ use Knp\Component\Pager\Paginator;
use Skobkin\Bundle\PointToolsBundle\DTO\{DailyEvents, TopUserDTO}; use Skobkin\Bundle\PointToolsBundle\DTO\{DailyEvents, TopUserDTO};
use Skobkin\Bundle\PointToolsBundle\Entity\User; use Skobkin\Bundle\PointToolsBundle\Entity\User;
use Skobkin\Bundle\PointToolsBundle\Repository\{SubscriptionEventRepository, UserRenameEventRepository, UserRepository}; use Skobkin\Bundle\PointToolsBundle\Repository\{SubscriptionEventRepository, UserRenameEventRepository, UserRepository};
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Ob\HighchartsBundle\Highcharts\Highchart; use Ob\HighchartsBundle\Highcharts\Highchart;
use Symfony\Component\HttpFoundation\{Request, Response}; use Symfony\Component\HttpFoundation\{Request, Response};
use Symfony\Component\Translation\TranslatorInterface; use Symfony\Component\Translation\TranslatorInterface;
class UserController extends Controller class UserController extends AbstractController
{ {
/** @var TranslatorInterface */ /** @var TranslatorInterface */
private $translator; private $translator;