diff --git a/app/config/services.yml b/app/config/services.yml index c6435bc..910658f 100644 --- a/app/config/services.yml +++ b/app/config/services.yml @@ -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 diff --git a/src/Skobkin/Bundle/PointToolsBundle/Controller/Api/AbstractApiController.php b/src/Skobkin/Bundle/PointToolsBundle/Controller/Api/AbstractApiController.php index fef7b9b..e132080 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Controller/Api/AbstractApiController.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Controller/Api/AbstractApiController.php @@ -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 { diff --git a/src/Skobkin/Bundle/PointToolsBundle/Controller/Api/CrawlerController.php b/src/Skobkin/Bundle/PointToolsBundle/Controller/Api/CrawlerController.php index 5364378..7ecc95a 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Controller/Api/CrawlerController.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Controller/Api/CrawlerController.php @@ -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); } diff --git a/src/Skobkin/Bundle/PointToolsBundle/Controller/ApiController.php b/src/Skobkin/Bundle/PointToolsBundle/Controller/ApiController.php index 577e87c..9af3b96 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Controller/ApiController.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Controller/ApiController.php @@ -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); } } diff --git a/src/Skobkin/Bundle/PointToolsBundle/Controller/EventsController.php b/src/Skobkin/Bundle/PointToolsBundle/Controller/EventsController.php index 9ec590f..c715117 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Controller/EventsController.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Controller/EventsController.php @@ -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 { diff --git a/src/Skobkin/Bundle/PointToolsBundle/Controller/MainController.php b/src/Skobkin/Bundle/PointToolsBundle/Controller/MainController.php index 2cb4137..a6c5cfc 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Controller/MainController.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Controller/MainController.php @@ -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, ]); } diff --git a/src/Skobkin/Bundle/PointToolsBundle/Controller/PostController.php b/src/Skobkin/Bundle/PointToolsBundle/Controller/PostController.php index 334d713..2022d2e 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Controller/PostController.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Controller/PostController.php @@ -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") diff --git a/src/Skobkin/Bundle/PointToolsBundle/Controller/PublicFeedController.php b/src/Skobkin/Bundle/PointToolsBundle/Controller/PublicFeedController.php index 9554bb4..0295fb6 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Controller/PublicFeedController.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Controller/PublicFeedController.php @@ -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; diff --git a/src/Skobkin/Bundle/PointToolsBundle/Controller/Telegram/WebHookController.php b/src/Skobkin/Bundle/PointToolsBundle/Controller/Telegram/WebHookController.php index dd474df..10d03be 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Controller/Telegram/WebHookController.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Controller/Telegram/WebHookController.php @@ -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; } diff --git a/src/Skobkin/Bundle/PointToolsBundle/Controller/UserController.php b/src/Skobkin/Bundle/PointToolsBundle/Controller/UserController.php index 48e210b..3948946 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Controller/UserController.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Controller/UserController.php @@ -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;