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/*'
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
app.http.telegram_client:
class: GuzzleHttp\Client
@ -48,6 +43,26 @@ services:
tags:
- { 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
# @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;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
class AbstractApiController extends Controller
class AbstractApiController extends AbstractController
{
protected function createSuccessResponse($data, int $code = 200): Response
{

View file

@ -10,12 +10,19 @@ use Symfony\Component\HttpFoundation\{Request, Response};
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
{
$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);
}

View file

@ -5,10 +5,10 @@ namespace Skobkin\Bundle\PointToolsBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Skobkin\Bundle\PointToolsBundle\Entity\{SubscriptionEvent, User};
use Skobkin\Bundle\PointToolsBundle\Repository\SubscriptionEventRepository;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
class ApiController extends Controller
class ApiController
{
/**
* 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 Skobkin\Bundle\PointToolsBundle\Repository\SubscriptionEventRepository;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\{Request, Response};
class EventsController extends Controller
class EventsController extends AbstractController
{
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 Skobkin\Bundle\PointToolsBundle\Form\UserSearchType;
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\HttpFoundation\{JsonResponse, Request, Response};
class MainController extends Controller
class MainController extends AbstractController
{
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(
Request $request,
UserRepository $userRepository,
@ -46,9 +58,9 @@ class MainController extends Controller
'form' => $form->createView(),
'autocomplete_size' => self::AJAX_AUTOCOMPLETE_SIZE,
'users_count' => $userRepository->getUsersCount(),
'subscribers_count' => $subscriptionRepository->getUserSubscribersCountById($this->getParameter('point_id')),
'subscribers_count' => $subscriptionRepository->getUserSubscribersCountById($this->appUserId),
'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\Repository\Blogs\PostRepository;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\HttpFoundation\Response;
class PostController extends Controller
class PostController extends AbstractController
{
/**
* @ParamConverter("post", class="SkobkinPointToolsBundle:Blogs\Post")

View file

@ -4,10 +4,10 @@ namespace Skobkin\Bundle\PointToolsBundle\Controller;
use Knp\Component\Pager\Paginator;
use Skobkin\Bundle\PointToolsBundle\Repository\Blogs\PostRepository;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
class PublicFeedController extends Controller
class PublicFeedController extends AbstractController
{
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 Symfony\Bridge\Monolog\Logger;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\{JsonResponse, Request, Response};
use unreal4u\TelegramAPI\Telegram\Types\Update;
/**
* {@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
{
if ($token !== $savedToken = $this->getParameter('telegram_token')) {
if ($token !== $savedToken = $this->telegramToken) {
throw $this->createNotFoundException();
}
@ -29,7 +41,7 @@ class WebHookController extends Controller
try {
$updateDispatcher->process($update);
} catch (\Exception $e) {
if ($this->getParameter('kernel.debug')) {
if ($this->debug) {
throw $e;
}

View file

@ -6,12 +6,12 @@ use Knp\Component\Pager\Paginator;
use Skobkin\Bundle\PointToolsBundle\DTO\{DailyEvents, TopUserDTO};
use Skobkin\Bundle\PointToolsBundle\Entity\User;
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 Symfony\Component\HttpFoundation\{Request, Response};
use Symfony\Component\Translation\TranslatorInterface;
class UserController extends Controller
class UserController extends AbstractController
{
/** @var TranslatorInterface */
private $translator;