Dependency Injection refactoring according to new autowiring and autoconfiguration. A bunch of classes refactored.

This commit is contained in:
Alexey Skobkin 2017-11-05 04:42:08 +03:00
parent d018c94a7a
commit b8f912b91d
32 changed files with 388 additions and 638 deletions

View file

@ -1,9 +1,182 @@
# Learn more about services, parameters and containers at
# http://symfony.com/doc/current/book/service_container.html
parameters: parameters:
# parameter_name: value
services: services:
# service_name: _defaults:
# class: AppBundle\Directory\ClassName autowire: true
# arguments: ["@another_service_name", "plain_value", "%parameter_name%"] autoconfigure: true
public: false
Skobkin\Bundle\PointToolsBundle\:
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
arguments: [{ timeout: 3.0 }]
tags:
- { name: csa_guzzle.client }
# HTTP client for Point API
GuzzleHttp\Client:
class: GuzzleHttp\Client
autowiring_types: GuzzleHttp\ClientInterface
arguments: [ { base_uri: '%point_base_url%', timeout: 5.0 } ]
tags:
- { name: csa_guzzle.client }
# Point API clients
# User
Skobkin\Bundle\PointToolsBundle\Service\Api\UserApi:
tags:
- { name: monolog.logger, channel: point_user_api }
# Post
Skobkin\Bundle\PointToolsBundle\Service\Api\PostApi:
tags:
- { name: monolog.logger, channel: point_post_api }
# Point subscription manager
Skobkin\Bundle\PointToolsBundle\Service\SubscriptionsManager:
# TODO deal with autowire for EventDispatcherInterface
tags:
- { name: monolog.logger, channel: subscribers_update }
# Console commands
# @todo https://github.com/symfony/symfony/blob/3.4/UPGRADE-3.4.md#httpkernel
# Subsribers update
Skobkin\Bundle\PointToolsBundle\Command\UpdateSubscriptionsCommand:
arguments:
$apiDelay: '%point_api_delay%'
$appUserId: '%point_id%'
tags:
- { name: console.command }
- { name: monolog.logger, channel: subscribers_update }
# Privacy update
Skobkin\Bundle\PointToolsBundle\Command\UpdateUsersPrivacyCommand:
arguments:
$apiDelay: '%point_api_delay%'
$appUserId: '%point_id%'
tags:
- { name: console.command }
- { name: monolog.logger, channel: privacy_update }
# Restore users removed by error
Skobkin\Bundle\PointToolsBundle\Command\RestoreRemovedUsersCommand:
arguments:
$apiDelay: '%point_api_delay%'
tags:
- { name: console.command }
# Webhook management
Skobkin\Bundle\PointToolsBundle\Command\TelegramWebHookCommand:
arguments:
$telegramToken: '%telegram_token%'
$telegramWebhookMaxConnections: '%telegram_max_connections%'
tags: [{ name: console.command }]
# Send message
Skobkin\Bundle\PointToolsBundle\Command\TelegramSendMessageCommand:
arguments:
$logChatId: '%telegram_log_chat_id%'
tags: [{ name: console.command }]
# Entity repositories as services
# User
Skobkin\Bundle\PointToolsBundle\Repository\UserRepository:
factory: 'doctrine:getRepository'
arguments: ['Skobkin\Bundle\PointToolsBundle\Entity\User']
# Subscription
Skobkin\Bundle\PointToolsBundle\Repository\SubscriptionRepository:
factory: 'doctrine:getRepository'
arguments: ['Skobkin\Bundle\PointToolsBundle\Entity\Subscription']
# Subscription record/event
Skobkin\Bundle\PointToolsBundle\Repository\SubscriptionEventRepository:
factory: 'doctrine:getRepository'
arguments: ['Skobkin\Bundle\PointToolsBundle\Entity\SubscriptionEvent']
# Subscription record/event
Skobkin\Bundle\PointToolsBundle\Repository\UserRenameEventRepository:
factory: 'doctrine:getRepository'
arguments: ['Skobkin\Bundle\PointToolsBundle\Entity\UserRenameEvent']
# Post repository
Skobkin\Bundle\PointToolsBundle\Repository\Blogs\PostRepository:
factory: 'doctrine:getRepository'
arguments: ['Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post']
# Comment repository
Skobkin\Bundle\PointToolsBundle\Repository\Blogs\CommentRepository:
factory: 'doctrine:getRepository'
arguments: ['Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Comment']
# Tag repository
Skobkin\Bundle\PointToolsBundle\Repository\Blogs\TagRepository:
factory: 'doctrine:getRepository'
arguments: ['Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Tag']
# File repository
Skobkin\Bundle\PointToolsBundle\Repository\Blogs\FileRepository:
factory: 'doctrine:getRepository'
arguments: ['Skobkin\Bundle\PointToolsBundle\Entity\Blogs\File']
# Telegram Account repository
Skobkin\Bundle\PointToolsBundle\Repository\Telegram\AccountRepository:
factory: 'doctrine:getRepository'
arguments: ['Skobkin\Bundle\PointToolsBundle\Entity\Telegram\Account']
# Custom Markdown parser
app.point.markdown_parser:
class: Skobkin\Bundle\PointToolsBundle\Service\Markdown\PointParser
arguments: [[], '@router']
tags:
- { name: markdown.parser }
# Event listeners
# User name changed in Doctrine
Skobkin\Bundle\PointToolsBundle\EventListener\UsersUpdatedSubscriber:
tags:
- { name: doctrine.event_subscriber, connection: default }
# User renaming
Skobkin\Bundle\PointToolsBundle\EventListener\UsersRenamedListener:
tags:
- { name: kernel.event_listener, event: app.users.renamed }
# User subscribers updated
Skobkin\Bundle\PointToolsBundle\EventListener\UserSubscribersUpdatedListener:
tags:
- { name: kernel.event_listener, event: app.user.subscribers_updated }
# Twig extensions
Skobkin\Bundle\PointToolsBundle\Twig\PointUrlExtension:
arguments:
$pointDomain: '%point_domain%'
$pointScheme: '%point_scheme%'
$pointBaseUrl: '%point_base_url%'
# Telegram services
# Bot API client
unreal4u\TelegramAPI\TgLog:
autowiring_types: unreal4u\TelegramAPI\TgLog
arguments:
$client: '@app.http.telegram_client'
$botToken: '%telegram_token%'
# Logger API client
app.telegram.logger_client:
class: unreal4u\TelegramAPI\TgLog
arguments:
$botToken: '%telegram_token%'
$logger: null
$client: '@app.http.telegram_client'
# Monolog handler
unreal4u\MonologHandler:
arguments:
$chatId: '%telegram_log_chat_id%'
$level: 'error'
# Private message processor
Skobkin\Bundle\PointToolsBundle\Service\Telegram\PrivateMessageProcessor:
arguments:
$appUserId: '%point_id%'

View file

@ -1,10 +1,10 @@
# Point-Tools
# Subscriptions
# Primary subscribers # Primary subscribers
*/10 * * * * flock -n /tmp/point_tools_sub_update.lock timeout 300 /usr/bin/php /var/www/point-tools/current/app/console point:update:subscriptions --env=prod */10 * * * * flock -n /tmp/point_tools_sub_update.lock timeout 300 /usr/bin/php /var/www/point.skobk.in/current/app/console point:update:subscriptions --env=prod
# App users # All users
0 0 * * * flock -n /tmp/point_tools_sub_update_all.lock timeout 3600 /usr/bin/php /var/www/point-tools/current/app/console point:update:subscriptions --all-users --env=prod 0 0 * * * flock -n /tmp/point_tools_sub_update_all.lock timeout 3600 /usr/bin/php /var/www/point.skobk.in/current/app/console point:update:subscriptions --all-users --env=prod
# Privacy
0 1 * * * flock -n /tmp/point_tools_privacy_update_all.lock timeout 3600 /usr/bin/php /var/www/point.skobk.in/current/app/console point:update:privacy --all-users --env=prod
# Other # Other
# Restore users deleted by mistake # Restore users deleted by mistake
0 0 */7 * * flock -n /tmp/point_tools_restore_users.lock timeout 300 /usr/bin/php /var/www/point-tools/current/app/console point:users:restore --env=prod 0 0 */7 * * flock -n /tmp/point_tools_restore_users.lock timeout 300 /usr/bin/php /var/www/point.skobk.in/current/app/console point:users:restore --env=prod

View file

@ -2,11 +2,10 @@
namespace Skobkin\Bundle\PointToolsBundle\Command; namespace Skobkin\Bundle\PointToolsBundle\Command;
use Doctrine\ORM\EntityManager;
use Skobkin\Bundle\PointToolsBundle\Entity\User; use Skobkin\Bundle\PointToolsBundle\Entity\User;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\{InputInterface, InputArgument, InputOption};
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Filesystem\Filesystem;
@ -19,8 +18,18 @@ use Symfony\Component\Filesystem\Filesystem;
* WHERE u.id <> (-1) * WHERE u.id <> (-1)
* ) TO '/tmp/point_users.csv' WITH HEADER DELIMITER '|' CSV; * ) TO '/tmp/point_users.csv' WITH HEADER DELIMITER '|' CSV;
*/ */
class ImportUsersCommand extends ContainerAwareCommand class ImportUsersCommand extends Command
{ {
/** @var EntityManager */
private $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
parent::__construct();
}
protected function configure() protected function configure()
{ {
$this $this
@ -48,7 +57,6 @@ class ImportUsersCommand extends ContainerAwareCommand
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
$em = $this->getContainer()->get('doctrine.orm.entity_manager');
$fs = new Filesystem(); $fs = new Filesystem();
$fileName = $input->getArgument('file'); $fileName = $input->getArgument('file');
@ -80,9 +88,9 @@ class ImportUsersCommand extends ContainerAwareCommand
$user = new User($row[0], $createdAt, $row[1], $row[2]); $user = new User($row[0], $createdAt, $row[1], $row[2]);
if (!$input->getOption('check-only')) { if (!$input->getOption('check-only')) {
$em->persist($user); $this->em->persist($user);
$em->flush($user); $this->em->flush($user);
$em->detach($user); $this->em->detach($user);
} }
if (OutputInterface::VERBOSITY_VERBOSE === $output->getVerbosity()) { if (OutputInterface::VERBOSITY_VERBOSE === $output->getVerbosity()) {

View file

@ -14,31 +14,22 @@ use Symfony\Component\Console\Output\OutputInterface;
class RestoreRemovedUsersCommand extends Command class RestoreRemovedUsersCommand extends Command
{ {
/** /** @var LoggerInterface */
* @var LoggerInterface
*/
private $logger; private $logger;
/**
* @var EntityManagerInterface /** @var EntityManagerInterface */
*/
private $em; private $em;
/** /** @var UserRepository */
* @var UserRepository
*/
private $userRepo; private $userRepo;
/** /** @var UserApi */
* @var UserApi
*/
private $userApi; private $userApi;
/** /** @var int */
* @var int
*/
private $delay; private $delay;
public function __construct(LoggerInterface $logger, EntityManagerInterface $em, UserRepository $userRepo, UserApi $userApi, int $delay) public function __construct(LoggerInterface $logger, EntityManagerInterface $em, UserRepository $userRepo, UserApi $userApi, int $apiDelay)
{ {
parent::__construct(); parent::__construct();
@ -46,7 +37,7 @@ class RestoreRemovedUsersCommand extends Command
$this->em = $em; $this->em = $em;
$this->userRepo = $userRepo; $this->userRepo = $userRepo;
$this->userApi = $userApi; $this->userApi = $userApi;
$this->delay = $delay; $this->delay = $apiDelay;
} }
/** /**

View file

@ -3,35 +3,24 @@
namespace Skobkin\Bundle\PointToolsBundle\Command; namespace Skobkin\Bundle\PointToolsBundle\Command;
use Skobkin\Bundle\PointToolsBundle\Service\Telegram\MessageSender; use Skobkin\Bundle\PointToolsBundle\Service\Telegram\MessageSender;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\{InputArgument, InputInterface, InputOption};
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
class TelegramSendMessageCommand extends ContainerAwareCommand class TelegramSendMessageCommand extends Command
{ {
/** /** @var MessageSender */
* @var MessageSender
*/
private $messenger; private $messenger;
/** /** @var int */
* @var int
*/
private $logChatId; private $logChatId;
public function setMessenger(MessageSender $messenger): void public function __construct(MessageSender $messenger, int $logChatId)
{ {
$this->messenger = $messenger; $this->messenger = $messenger;
}
/**
* @param int $logChatId
*/
public function setLogChatId(int $logChatId): void
{
$this->logChatId = $logChatId; $this->logChatId = $logChatId;
parent::__construct();
} }
/** /**

View file

@ -2,63 +2,43 @@
namespace Skobkin\Bundle\PointToolsBundle\Command; namespace Skobkin\Bundle\PointToolsBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Bundle\FrameworkBundle\Routing\Router; use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\{InputArgument, InputInterface};
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use unreal4u\TelegramAPI\Telegram\Methods\DeleteWebhook; use unreal4u\TelegramAPI\Telegram\Methods\{DeleteWebhook, SetWebhook};
use unreal4u\TelegramAPI\Telegram\Methods\SetWebhook;
use unreal4u\TelegramAPI\TgLog; use unreal4u\TelegramAPI\TgLog;
/** /**
* Sets or deletes Telegram bot Web-Hook * Sets or deletes Telegram bot Web-Hook
* @see https://core.telegram.org/bots/api#setwebhook * @see https://core.telegram.org/bots/api#setwebhook
*/ */
class TelegramWebHookCommand extends ContainerAwareCommand class TelegramWebHookCommand extends Command
{ {
private const MODE_SET = 'set'; private const MODE_SET = 'set';
private const MODE_DELETE = 'delete'; private const MODE_DELETE = 'delete';
/** /** @var TgLog */
* @var TgLog
*/
private $client; private $client;
/** /** @var Router */
* @var Router
*/
private $router; private $router;
/** /** @var string */
* @var string
*/
private $token; private $token;
/** /** @var int */
* @var int
*/
private $maxConnections; private $maxConnections;
public function setClient(TgLog $client): void public function __construct(TgLog $client, Router $router, string $telegramToken, int $telegramWebhookMaxConnections)
{ {
parent::__construct();
$this->client = $client; $this->client = $client;
}
public function setRouter(Router $router): void
{
$this->router = $router; $this->router = $router;
} $this->token = $telegramToken;
$this->maxConnections = $telegramWebhookMaxConnections;
public function setToken(string $token): void
{
$this->token = $token;
}
public function setMaxConnections(int $maxConnections)
{
$this->maxConnections = $maxConnections;
} }
/** /**

View file

@ -8,7 +8,7 @@ use Skobkin\Bundle\PointToolsBundle\Entity\{Subscription, User};
use Skobkin\Bundle\PointToolsBundle\Exception\Api\UserNotFoundException; use Skobkin\Bundle\PointToolsBundle\Exception\Api\UserNotFoundException;
use Skobkin\Bundle\PointToolsBundle\Repository\UserRepository; use Skobkin\Bundle\PointToolsBundle\Repository\UserRepository;
use Skobkin\Bundle\PointToolsBundle\Service\{SubscriptionsManager, Api\UserApi}; use Skobkin\Bundle\PointToolsBundle\Service\{SubscriptionsManager, Api\UserApi};
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\{InputInterface, InputOption}; use Symfony\Component\Console\Input\{InputInterface, InputOption};
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
@ -16,51 +16,33 @@ use Symfony\Component\Console\Output\OutputInterface;
/** /**
* @todo https://symfony.com/doc/current/console/lockable_trait.html * @todo https://symfony.com/doc/current/console/lockable_trait.html
*/ */
class UpdateSubscriptionsCommand extends ContainerAwareCommand class UpdateSubscriptionsCommand extends Command
{ {
/** /** @var EntityManagerInterface */
* @var EntityManagerInterface
*/
private $em; private $em;
/** /** @var LoggerInterface */
* @var LoggerInterface
*/
private $logger; private $logger;
/** /** @var UserRepository */
* @var UserRepository
*/
private $userRepo; private $userRepo;
/** /** @var InputInterface */
* @var InputInterface
*/
private $input; private $input;
/** /** @var UserApi */
* @var UserApi
*/
private $api; private $api;
/** /** @var int */
* @var int
*/
private $apiDelay = 500000; private $apiDelay = 500000;
/** /** @var int */
* @var int
*/
private $appUserId; private $appUserId;
/** /** @var SubscriptionsManager */
* @var SubscriptionsManager
*/
private $subscriptionManager; private $subscriptionManager;
/** /** @var ProgressBar */
* @var ProgressBar
*/
private $progress; private $progress;
public function __construct( public function __construct(

View file

@ -8,12 +8,12 @@ use Skobkin\Bundle\PointToolsBundle\Entity\{Subscription, User};
use Skobkin\Bundle\PointToolsBundle\Exception\Api\{ForbiddenException, UserNotFoundException}; use Skobkin\Bundle\PointToolsBundle\Exception\Api\{ForbiddenException, UserNotFoundException};
use Skobkin\Bundle\PointToolsBundle\Repository\UserRepository; use Skobkin\Bundle\PointToolsBundle\Repository\UserRepository;
use Skobkin\Bundle\PointToolsBundle\Service\Api\UserApi; use Skobkin\Bundle\PointToolsBundle\Service\Api\UserApi;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\{InputInterface, InputOption}; use Symfony\Component\Console\Input\{InputInterface, InputOption};
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
class UpdateUsersPrivacyCommand extends ContainerAwareCommand class UpdateUsersPrivacyCommand extends Command
{ {
/** @var EntityManagerInterface */ /** @var EntityManagerInterface */
private $em; private $em;

View file

@ -2,13 +2,15 @@
namespace Skobkin\Bundle\PointToolsBundle\Controller\Api; namespace Skobkin\Bundle\PointToolsBundle\Controller\Api;
use Doctrine\ORM\EntityManager;
use JMS\Serializer\Serializer;
use Skobkin\Bundle\PointToolsBundle\DTO\Api\PostsPage; use Skobkin\Bundle\PointToolsBundle\DTO\Api\PostsPage;
use Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs\PostFactory; use Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs\PostFactory;
use Symfony\Component\HttpFoundation\{Request, Response}; use Symfony\Component\HttpFoundation\{Request, Response};
class CrawlerController extends AbstractApiController class CrawlerController extends AbstractApiController
{ {
public function receiveAllPageAction(Request $request): 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'); $localToken = $this->getParameter('crawler_token');
@ -19,16 +21,11 @@ class CrawlerController extends AbstractApiController
$json = $request->request->get('json'); $json = $request->request->get('json');
$serializer = $this->get('jms_serializer');
$page = $serializer->deserialize($json, PostsPage::class, 'json'); $page = $serializer->deserialize($json, PostsPage::class, 'json');
/** @var PostFactory $factory */
$factory = $this->get('app.point.post_factory');
$continue = $factory->createFromPageDTO($page); $continue = $postFactory->createFromPageDTO($page);
$this->getDoctrine()->getManager()->flush(); $em->flush();
return $this->createSuccessResponse([ return $this->createSuccessResponse([
'continue' => $continue, 'continue' => $continue,

View file

@ -3,8 +3,8 @@
namespace Skobkin\Bundle\PointToolsBundle\Controller; namespace Skobkin\Bundle\PointToolsBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Skobkin\Bundle\PointToolsBundle\Entity\SubscriptionEvent; use Skobkin\Bundle\PointToolsBundle\Entity\{SubscriptionEvent, User};
use Skobkin\Bundle\PointToolsBundle\Entity\User; use Skobkin\Bundle\PointToolsBundle\Repository\SubscriptionEventRepository;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
@ -15,9 +15,9 @@ class ApiController extends Controller
* *
* @ParamConverter("user", class="SkobkinPointToolsBundle:User") * @ParamConverter("user", class="SkobkinPointToolsBundle:User")
*/ */
public function lastUserSubscribersByIdAction(User $user): Response public function lastUserSubscribersByIdAction(User $user, SubscriptionEventRepository $subscriptionEventRepository): Response
{ {
$qb = $this->getDoctrine()->getRepository('SkobkinPointToolsBundle:SubscriptionEvent')->createQueryBuilder('se'); $qb = $subscriptionEventRepository->createQueryBuilder('se');
$qb $qb
->select(['se', 'sub']) ->select(['se', 'sub'])
->innerJoin('se.subscriber', 'sub') ->innerJoin('se.subscriber', 'sub')

View file

@ -2,23 +2,17 @@
namespace Skobkin\Bundle\PointToolsBundle\Controller; namespace Skobkin\Bundle\PointToolsBundle\Controller;
use Doctrine\ORM\EntityManager; use Knp\Component\Pager\Paginator;
use Doctrine\ORM\QueryBuilder; use Skobkin\Bundle\PointToolsBundle\Repository\SubscriptionEventRepository;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\{Request, Response};
use Symfony\Component\HttpFoundation\Response;
class EventsController extends Controller class EventsController extends Controller
{ {
public function lastAction(Request $request): Response public function lastAction(Request $request, SubscriptionEventRepository $eventRepository, Paginator $paginator): Response
{ {
/** @var EntityManager $em */
$em = $this->getDoctrine()->getManager();
$paginator = $this->get('knp_paginator');
$eventsPagination = $paginator->paginate( $eventsPagination = $paginator->paginate(
$em->getRepository('SkobkinPointToolsBundle:SubscriptionEvent')->createLastSubscriptionEventsQuery(), $eventRepository->createLastSubscriptionEventsQuery(),
$request->query->getInt('page', 1), $request->query->getInt('page', 1),
20 20
); );

View file

@ -4,18 +4,21 @@ 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 Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\FormError; use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\{JsonResponse, Request, Response};
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class MainController extends Controller class MainController extends Controller
{ {
const AJAX_AUTOCOMPLETE_SIZE = 10; const AJAX_AUTOCOMPLETE_SIZE = 10;
public function indexAction(Request $request): Response public function indexAction(
{ Request $request,
UserRepository $userRepository,
SubscriptionRepository $subscriptionRepository,
SubscriptionEventRepository $subscriptionEventRepository
): Response {
/** @var EntityManager $em */ /** @var EntityManager $em */
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
@ -32,7 +35,7 @@ class MainController extends Controller
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$login = $form->get('login')->getData(); $login = $form->get('login')->getData();
if (null !== $user = $em->getRepository('SkobkinPointToolsBundle:User')->findOneBy(['login' => $login])) { if (null !== $user = $userRepository->findOneBy(['login' => $login])) {
return $this->redirectToRoute('user_show', ['login' => $login]); return $this->redirectToRoute('user_show', ['login' => $login]);
} }
@ -42,9 +45,9 @@ class MainController extends Controller
return $this->render('SkobkinPointToolsBundle:Main:index.html.twig', [ return $this->render('SkobkinPointToolsBundle:Main:index.html.twig', [
'form' => $form->createView(), 'form' => $form->createView(),
'autocomplete_size' => self::AJAX_AUTOCOMPLETE_SIZE, 'autocomplete_size' => self::AJAX_AUTOCOMPLETE_SIZE,
'users_count' => $em->getRepository('SkobkinPointToolsBundle:User')->getUsersCount(), 'users_count' => $userRepository->getUsersCount(),
'subscribers_count' => $em->getRepository('SkobkinPointToolsBundle:Subscription')->getUserSubscribersCountById($this->getParameter('point_id')), 'subscribers_count' => $subscriptionRepository->getUserSubscribersCountById($this->getParameter('point_id')),
'events_count' => $em->getRepository('SkobkinPointToolsBundle:SubscriptionEvent')->getLastDayEventsCount(), 'events_count' => $subscriptionEventRepository->getLastDayEventsCount(),
'service_login' => $this->getParameter('point_login'), 'service_login' => $this->getParameter('point_login'),
]); ]);
} }
@ -56,13 +59,13 @@ class MainController extends Controller
* *
* @return JsonResponse * @return JsonResponse
*/ */
public function searchUserAjaxAction(string $login): Response public function searchUserAjaxAction(string $login, UserRepository $userRepository): Response
{ {
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$result = []; $result = [];
foreach ($em->getRepository('SkobkinPointToolsBundle:User')->findUsersLikeLogin($login, self::AJAX_AUTOCOMPLETE_SIZE) as $user) { foreach ($userRepository->findUsersLikeLogin($login, self::AJAX_AUTOCOMPLETE_SIZE) as $user) {
$result[] = [ $result[] = [
'login' => $user->getLogin(), 'login' => $user->getLogin(),
'name' => $user->getName(), 'name' => $user->getName(),

View file

@ -3,6 +3,7 @@
namespace Skobkin\Bundle\PointToolsBundle\Controller; 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 Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
@ -14,10 +15,10 @@ class PostController extends Controller
* *
* @return Response * @return Response
*/ */
public function showAction(Post $post): Response public function showAction(Post $post, PostRepository $postRepository): Response
{ {
return $this->render('SkobkinPointToolsBundle:Post:show.html.twig', [ return $this->render('SkobkinPointToolsBundle:Post:show.html.twig', [
'post' => $this->getDoctrine()->getRepository('SkobkinPointToolsBundle:Blogs\Post')->getPostWithComments($post->getId()), 'post' => $postRepository->getPostWithComments($post->getId()),
]); ]);
} }

View file

@ -2,7 +2,8 @@
namespace Skobkin\Bundle\PointToolsBundle\Controller; namespace Skobkin\Bundle\PointToolsBundle\Controller;
use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post; use Knp\Component\Pager\Paginator;
use Skobkin\Bundle\PointToolsBundle\Repository\Blogs\PostRepository;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
@ -10,13 +11,8 @@ class PublicFeedController extends Controller
{ {
private const POSTS_PER_PAGE = 20; private const POSTS_PER_PAGE = 20;
public function indexAction(Request $request) public function indexAction(Request $request, PostRepository $postRepository, Paginator $paginator)
{ {
// @todo autowire
$postRepository = $this->getDoctrine()->getRepository(Post::class);
$paginator = $this->get('knp_paginator');
$postsPagination = $paginator->paginate( $postsPagination = $paginator->paginate(
$postRepository->createPublicFeedPostsQuery(), $postRepository->createPublicFeedPostsQuery(),
$request->query->getInt('page', 1), $request->query->getInt('page', 1),

View file

@ -2,10 +2,10 @@
namespace Skobkin\Bundle\PointToolsBundle\Controller\Telegram; 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\Controller;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\{JsonResponse, Request, Response};
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use unreal4u\TelegramAPI\Telegram\Types\Update; use unreal4u\TelegramAPI\Telegram\Types\Update;
/** /**
@ -13,14 +13,12 @@ use unreal4u\TelegramAPI\Telegram\Types\Update;
*/ */
class WebHookController extends Controller class WebHookController extends Controller
{ {
public function receiveUpdateAction(Request $request, string $token): Response public function receiveUpdateAction(Request $request, string $token, IncomingUpdateDispatcher $updateDispatcher, Logger $logger): Response
{ {
if ($token !== $savedToken = $this->getParameter('telegram_token')) { if ($token !== $savedToken = $this->getParameter('telegram_token')) {
throw $this->createNotFoundException(); throw $this->createNotFoundException();
} }
$logger = $this->get('logger');
$content = json_decode($request->getContent(), true); $content = json_decode($request->getContent(), true);
$update = new Update( $update = new Update(
@ -29,7 +27,7 @@ class WebHookController extends Controller
); );
try { try {
$this->get('app.telegram.update_dispatcher')->process($update); $updateDispatcher->process($update);
} catch (\Exception $e) { } catch (\Exception $e) {
if ($this->getParameter('kernel.debug')) { if ($this->getParameter('kernel.debug')) {
throw $e; throw $e;

View file

@ -2,52 +2,61 @@
namespace Skobkin\Bundle\PointToolsBundle\Controller; namespace Skobkin\Bundle\PointToolsBundle\Controller;
use Doctrine\ORM\EntityManager; 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 Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
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;
class UserController extends Controller class UserController extends Controller
{ {
/** @var TranslatorInterface */
private $translator;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
/** /**
* @param string $login * @param string $login
*/ */
public function showAction(Request $request, string $login): Response public function showAction(
{ Request $request,
/** @var EntityManager $em */ string $login,
$em = $this->getDoctrine()->getManager(); SubscriptionEventRepository $subscriptionEventRepository,
UserRepository $userRepository,
UserRenameEventRepository $renameEventRepository,
Paginator $paginator
): Response {
/** @var User $user */ /** @var User $user */
$user = $em->getRepository('SkobkinPointToolsBundle:User')->findUserByLogin($login); $user = $userRepository->findUserByLogin($login);
if (!$user) { if (!$user) {
throw $this->createNotFoundException('User ' . $login . ' not found.'); throw $this->createNotFoundException('User ' . $login . ' not found.');
} }
$paginator = $this->get('knp_paginator');
$subscriberEventsPagination = $paginator->paginate( $subscriberEventsPagination = $paginator->paginate(
$em->getRepository('SkobkinPointToolsBundle:SubscriptionEvent')->createUserLastSubscribersEventsQuery($user), $subscriptionEventRepository->createUserLastSubscribersEventsQuery($user),
$request->query->getInt('page', 1), $request->query->getInt('page', 1),
10 10
); );
return $this->render('SkobkinPointToolsBundle:User:show.html.twig', [ return $this->render('SkobkinPointToolsBundle:User:show.html.twig', [
'user' => $user, 'user' => $user,
'subscribers' => $em->getRepository('SkobkinPointToolsBundle:User')->findUserSubscribersById($user->getId()), 'subscribers' => $userRepository->findUserSubscribersById($user->getId()),
'subscriptions_log' => $subscriberEventsPagination, 'subscriptions_log' => $subscriberEventsPagination,
'rename_log' => $em->getRepository('SkobkinPointToolsBundle:UserRenameEvent')->findBy(['user' => $user], ['date' => 'DESC'], 10), 'rename_log' => $renameEventRepository->findBy(['user' => $user], ['date' => 'DESC'], 10),
]); ]);
} }
public function topAction(): Response public function topAction(UserRepository $userRepository, SubscriptionEventRepository $subscriptionEventRepository): Response
{ {
$userRepo = $this->getDoctrine()->getManager()->getRepository('SkobkinPointToolsBundle:User'); $topUsers = $userRepository->getTopUsers();
$subscriptionsRecordsRepo = $this->getDoctrine()->getManager()->getRepository('SkobkinPointToolsBundle:SubscriptionEvent'); $eventsByDay = $subscriptionEventRepository->getLastEventsByDay();
$topUsers = $userRepo->getTopUsers();
$eventsByDay = $subscriptionsRecordsRepo->getLastEventsByDay();
return $this->render('@SkobkinPointTools/User/top.html.twig', [ return $this->render('@SkobkinPointTools/User/top.html.twig', [
'events_dynamic_chat' => $this->createEventsDynamicChart($eventsByDay), 'events_dynamic_chat' => $this->createEventsDynamicChart($eventsByDay),
@ -89,8 +98,6 @@ class UserController extends Controller
private function createChart(string $blockId, string $type, array $data, string $bottomLabel, string $amountLabel): Highchart private function createChart(string $blockId, string $type, array $data, string $bottomLabel, string $amountLabel): Highchart
{ {
$translator = $this->get('translator');
$chartData = [ $chartData = [
'keys' => [], 'keys' => [],
'values' => [], 'values' => [],
@ -104,7 +111,7 @@ class UserController extends Controller
// Chart // Chart
$series = [[ $series = [[
'name' => $translator->trans($amountLabel), 'name' => $this->translator->trans($amountLabel),
'data' => $chartData['values'], 'data' => $chartData['values'],
]]; ]];
@ -112,10 +119,10 @@ class UserController extends Controller
$ob = new Highchart(); $ob = new Highchart();
$ob->chart->renderTo($blockId); $ob->chart->renderTo($blockId);
$ob->chart->type($type); $ob->chart->type($type);
$ob->title->text($translator->trans($bottomLabel)); $ob->title->text($this->translator->trans($bottomLabel));
$ob->xAxis->title(['text' => null]); $ob->xAxis->title(['text' => null]);
$ob->xAxis->categories($chartData['keys']); $ob->xAxis->categories($chartData['keys']);
$ob->yAxis->title(['text' => $translator->trans($amountLabel)]); $ob->yAxis->title(['text' => $this->translator->trans($amountLabel)]);
$ob->plotOptions->bar([ $ob->plotOptions->bar([
'dataLabels' => [ 'dataLabels' => [
'enabled' => true 'enabled' => true

View file

@ -142,11 +142,12 @@ class Account
return $this->firstName; return $this->firstName;
} }
public function setFirstName(string $firstName): Account public function updateFromMessageData(string $firstName, ?string $lastName, ?string $username, int $chatId): void
{ {
$this->firstName = $firstName; $this->firstName = $firstName;
$this->lastName = $lastName;
return $this; $this->username = $username;
$this->chatId = $chatId;
} }
public function getLastName(): ?string public function getLastName(): ?string
@ -154,32 +155,11 @@ class Account
return $this->lastName; return $this->lastName;
} }
public function setLastName(?string $lastName = null): Account
{
$this->lastName = $lastName;
return $this;
}
public function getUsername(): string public function getUsername(): string
{ {
return $this->username; return $this->username;
} }
public function setUsername(string $username = null): Account
{
$this->username = $username;
return $this;
}
public function setChatId(int $chatId): self
{
$this->chatId = $chatId;
return $this;
}
public function getChatId(): int public function getChatId(): int
{ {
return $this->chatId; return $this->chatId;

View file

@ -157,7 +157,7 @@ class User
/** /**
* @return Subscription[]|ArrayCollection * @return Subscription[]|ArrayCollection
*/ */
public function getSubscribers(): ArrayCollection public function getSubscribers(): iterable
{ {
return $this->subscribers; return $this->subscribers;
} }
@ -165,7 +165,7 @@ class User
/** /**
* @return Subscription[]|ArrayCollection * @return Subscription[]|ArrayCollection
*/ */
public function getSubscriptions(): ArrayCollection public function getSubscriptions(): iterable
{ {
return $this->subscriptions; return $this->subscriptions;
} }
@ -180,7 +180,7 @@ class User
/** /**
* @return SubscriptionEvent[]|ArrayCollection * @return SubscriptionEvent[]|ArrayCollection
*/ */
public function getNewSubscriberEvents(): ArrayCollection public function getNewSubscriberEvents(): iterable
{ {
return $this->newSubscriberEvents; return $this->newSubscriberEvents;
} }

View file

@ -9,7 +9,7 @@ use Doctrine\ORM\Mapping as ORM;
* @ORM\Index(name="idx_rename_log_date", columns={"date"}), * @ORM\Index(name="idx_rename_log_date", columns={"date"}),
* @ORM\Index(name="idx_rename_log_old_login", columns={"old_login"}) * @ORM\Index(name="idx_rename_log_old_login", columns={"old_login"})
* }) * })
* @ORM\Entity(readOnly=true) * @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Repository\UserRenameEventRepository", readOnly=true)
*/ */
class UserRenameEvent class UserRenameEvent
{ {

View file

@ -0,0 +1,14 @@
<?php
namespace Skobkin\Bundle\PointToolsBundle\Repository;
use Doctrine\ORM\EntityRepository;
use Skobkin\Bundle\PointToolsBundle\Entity\UserRenameEvent;
class UserRenameEventRepository extends EntityRepository
{
public function add(UserRenameEvent $event)
{
$this->getEntityManager()->persist($event);
}
}

View file

@ -1,276 +1 @@
services: services:
# HTTP client for Telegram
app.http.telegram_client:
class: GuzzleHttp\Client
arguments: [{ timeout: 3.0 }]
tags:
- { name: csa_guzzle.client }
# HTTP client for Point API
app.http.point_client:
class: GuzzleHttp\Client
autowiring_types: GuzzleHttp\ClientInterface
arguments: [ { base_uri: '%point_base_url%', timeout: 5.0 } ]
tags:
- { name: csa_guzzle.client }
# Point API clients
# Abstract API client with common dependency
app.point.abstract_api:
abstract: true
autowire: true
# User
app.point.api_user:
class: Skobkin\Bundle\PointToolsBundle\Service\Api\UserApi
parent: app.point.abstract_api
autowire: true
tags:
- { name: monolog.logger, channel: point_user_api }
# Post
app.point.api_post:
class: Skobkin\Bundle\PointToolsBundle\Service\Api\PostApi
parent: app.point.abstract_api
autowire: true
tags:
- { name: monolog.logger, channel: point_post_api }
# Point subscription manager
app.point.subscriptions_manager:
class: Skobkin\Bundle\PointToolsBundle\Service\SubscriptionsManager
# TODO deal with autowire for EventDispatcherInterface
arguments:
- '@event_dispatcher'
- '@logger'
- '@app.point.subscription_repository'
- '@app.point.subscription_record_repository'
tags:
- { name: monolog.logger, channel: subscribers_update }
# Console commands
# @todo https://github.com/symfony/symfony/blob/3.4/UPGRADE-3.4.md#httpkernel
# Subsribers update
app.point.update_subscribers_command:
class: Skobkin\Bundle\PointToolsBundle\Command\UpdateSubscriptionsCommand
arguments:
- '@doctrine.orm.entity_manager'
- '@logger'
- '@app.point.user_repository'
- '@app.point.api_user'
- '@app.point.subscriptions_manager'
- '%point_api_delay%'
- '%point_id%'
tags:
- { name: console.command }
- { name: monolog.logger, channel: subscribers_update }
# Privacy update
app.point.update_privacy_command:
class: Skobkin\Bundle\PointToolsBundle\Command\UpdateUsersPrivacyCommand
#autowire: []
arguments:
- '@doctrine.orm.entity_manager'
- '@logger'
- '@app.point.user_repository'
- '@app.point.api_user'
- '%point_api_delay%'
- '%point_id%'
tags:
- { name: console.command }
- { name: monolog.logger, channel: privacy_update }
# Restore users removed by error
app.point.restore_users:
class: Skobkin\Bundle\PointToolsBundle\Command\RestoreRemovedUsersCommand
arguments:
- '@logger'
- '@doctrine.orm.entity_manager'
- '@app.point.user_repository'
- '@app.point.api_user'
- '%point_api_delay%'
tags:
- { name: console.command }
# Webhook management
app.telegram.webhook_command:
class: Skobkin\Bundle\PointToolsBundle\Command\TelegramWebHookCommand
#autowire: []
calls:
- [setClient, ['@app.telegram.bot_client']]
- [setRouter, ['@router']]
- [setToken, ['%telegram_token%']]
tags: [{ name: console.command }]
# Send message
app.telegram.send_message:
class: Skobkin\Bundle\PointToolsBundle\Command\TelegramSendMessageCommand
#autowire: []
calls:
- [setMessenger, ['@app.telegram.message_sender']]
- [setLogChatId, ['%telegram_log_chat_id%']]
tags: [{ name: console.command }]
# Entity repositories as services
# User
app.point.user_repository:
class: Skobkin\Bundle\PointToolsBundle\Repository\UserRepository
factory: 'doctrine:getRepository'
arguments: ['Skobkin\Bundle\PointToolsBundle\Entity\User']
# Subscription
app.point.subscription_repository:
class: Skobkin\Bundle\PointToolsBundle\Repository\SubscriptionRepository
factory: 'doctrine:getRepository'
arguments: ['Skobkin\Bundle\PointToolsBundle\Entity\Subscription']
# Subscription record/event
app.point.subscription_record_repository:
class: Skobkin\Bundle\PointToolsBundle\Repository\SubscriptionEventRepository
factory: 'doctrine:getRepository'
arguments: ['Skobkin\Bundle\PointToolsBundle\Entity\SubscriptionEvent']
# Post repository
app.point.post_repository:
class: Skobkin\Bundle\PointToolsBundle\Repository\Blogs\PostRepository
factory: 'doctrine:getRepository'
arguments: ['Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post']
# Comment repository
app.point.comment_repository:
class: Skobkin\Bundle\PointToolsBundle\Repository\Blogs\CommentRepository
factory: 'doctrine:getRepository'
arguments: ['Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Comment']
# Tag repository
app.point.tag_repository:
class: Skobkin\Bundle\PointToolsBundle\Repository\Blogs\TagRepository
factory: 'doctrine:getRepository'
arguments: ['Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Tag']
# File repository
app.point.file_repository:
class: Skobkin\Bundle\PointToolsBundle\Repository\Blogs\FileRepository
factory: 'doctrine:getRepository'
arguments: ['Skobkin\Bundle\PointToolsBundle\Entity\Blogs\File']
# Telegram Account repository
app.point.telegram_account_repository:
class: Skobkin\Bundle\PointToolsBundle\Repository\Telegram\AccountRepository
factory: 'doctrine:getRepository'
arguments: ['Skobkin\Bundle\PointToolsBundle\Entity\Telegram\Account']
# Factories
# Abstract factory
app.point.abstract_factory:
abstract: true
autowire: true
# User
app.point.user_factory:
class: Skobkin\Bundle\PointToolsBundle\Service\Factory\UserFactory
parent: app.point.abstract_factory
autowire: true
# Comment
app.point.comment_factory:
class: Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs\CommentFactory
parent: app.point.abstract_factory
autowire: true
# Tag
app.point.tag_factory:
class: Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs\TagFactory
parent: app.point.abstract_factory
autowire: true
# File
app.point.file_factory:
class: Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs\FileFactory
parent: app.point.abstract_factory
autowire: true
# Post
app.point.post_factory:
class: Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs\PostFactory
parent: app.point.abstract_factory
autowire: true
# Telegram account
app.telegram.telegram_account_factory:
class: Skobkin\Bundle\PointToolsBundle\Service\Factory\Telegram\AccountFactory
parent: app.point.abstract_factory
autowire: true
# Custom Markdown parser
app.point.markdown_parser:
class: Skobkin\Bundle\PointToolsBundle\Service\Markdown\PointParser
arguments: [[], '@router']
tags:
- { name: markdown.parser }
# Event listeners
# User name changed in Doctrine
app.event_listener.users_updated:
class: Skobkin\Bundle\PointToolsBundle\EventListener\UsersUpdatedSubscriber
arguments: ['@event_dispatcher']
tags:
- { name: doctrine.event_subscriber, connection: default }
# User renaming
app.event_listener.users_renamed_notifier:
class: Skobkin\Bundle\PointToolsBundle\EventListener\UsersRenamedListener
autowire: true
tags:
- { name: kernel.event_listener, event: app.users.renamed }
# User subscribers updated
app.event_listener.user_subscribers_updated:
class: Skobkin\Bundle\PointToolsBundle\EventListener\UserSubscribersUpdatedListener
autowire: true
tags:
- { name: kernel.event_listener, event: app.user.subscribers_updated }
# Twig extensions
app.twig.point_users_extension:
class: Skobkin\Bundle\PointToolsBundle\Twig\PointUrlExtension
public: false
arguments:
- '%point_domain%'
- '%point_scheme%'
- '%point_base_url%'
tags:
- { name: twig.extension }
# Telegram services
# Bot API client
app.telegram.bot_client:
class: unreal4u\TelegramAPI\TgLog
autowiring_types: unreal4u\TelegramAPI\TgLog
arguments: ['%telegram_token%', '@logger', '@app.http.telegram_client']
# Logger API client
app.telegram.logger_client:
class: unreal4u\TelegramAPI\TgLog
arguments: ['%telegram_token%', null, '@app.http.telegram_client']
# Monolog handler
app.log.telegram_handler:
class: unreal4u\MonologHandler
arguments:
- '@app.telegram.logger_client'
- '%telegram_log_chat_id%'
- 'error'
# User notifier
app.telegram.notifier:
class: Skobkin\Bundle\PointToolsBundle\Service\Telegram\Notifier
autowire: true
# Message sender
app.telegram.message_sender:
class: Skobkin\Bundle\PointToolsBundle\Service\Telegram\MessageSender
autowire: true
# Common incoming message processor
app.telegram.update_dispatcher:
class: Skobkin\Bundle\PointToolsBundle\Service\Telegram\IncomingUpdateDispatcher
autowire: true
# InlineQuery processor
app.telegram.inline_query_processor:
class: Skobkin\Bundle\PointToolsBundle\Service\Telegram\InlineQueryProcessor
lazy: true
autowire: true
# Private message processor
app.telegram.private_message_processor:
class: Skobkin\Bundle\PointToolsBundle\Service\Telegram\PrivateMessageProcessor
lazy: true
autowire: true
calls:
- [setPointUserId, ['%point_id%']]

View file

@ -3,26 +3,18 @@
namespace Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs; namespace Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Skobkin\Bundle\PointToolsBundle\Repository\Blogs\CommentRepository; use Skobkin\Bundle\PointToolsBundle\Repository\Blogs\{CommentRepository, PostRepository};
use Skobkin\Bundle\PointToolsBundle\Repository\Blogs\PostRepository; use Skobkin\Bundle\PointToolsBundle\Service\Factory\{AbstractFactory, UserFactory};
use Skobkin\Bundle\PointToolsBundle\Service\Factory\AbstractFactory;
use Skobkin\Bundle\PointToolsBundle\Service\Factory\UserFactory;
class CommentFactory extends AbstractFactory class CommentFactory extends AbstractFactory
{ {
/** /** @var CommentRepository */
* @var CommentRepository
*/
private $commentRepository; private $commentRepository;
/** /** @var PostRepository */
* @var PostRepository
*/
private $postRepository; private $postRepository;
/** /** @var UserFactory */
* @var UserFactory
*/
private $userFactory; private $userFactory;

View file

@ -10,9 +10,7 @@ use Skobkin\Bundle\PointToolsBundle\Service\Factory\AbstractFactory;
class FileFactory extends AbstractFactory class FileFactory extends AbstractFactory
{ {
/** /** @var FileRepository */
* @var FileRepository
*/
private $fileRepository; private $fileRepository;

View file

@ -4,48 +4,30 @@ namespace Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Skobkin\Bundle\PointToolsBundle\DTO\Api\MetaPost; use Skobkin\Bundle\PointToolsBundle\DTO\Api\{MetaPost, Post as PostDTO, PostsPage};
use Skobkin\Bundle\PointToolsBundle\DTO\Api\Post as PostDTO; use Skobkin\Bundle\PointToolsBundle\Entity\{Blogs\Post, Blogs\PostTag, User};
use Skobkin\Bundle\PointToolsBundle\DTO\Api\PostsPage; use Skobkin\Bundle\PointToolsBundle\Exception\{Api\InvalidResponseException, Factory\Blog\InvalidDataException};
use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post;
use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\PostTag;
use Skobkin\Bundle\PointToolsBundle\Entity\User;
use Skobkin\Bundle\PointToolsBundle\Exception\Factory\Blog\InvalidDataException;
use Skobkin\Bundle\PointToolsBundle\Repository\Blogs\PostRepository; use Skobkin\Bundle\PointToolsBundle\Repository\Blogs\PostRepository;
use Skobkin\Bundle\PointToolsBundle\Exception\Api\InvalidResponseException; use Skobkin\Bundle\PointToolsBundle\Service\Factory\{AbstractFactory, UserFactory};
use Skobkin\Bundle\PointToolsBundle\Service\Factory\AbstractFactory;
use Skobkin\Bundle\PointToolsBundle\Service\Factory\UserFactory;
class PostFactory extends AbstractFactory class PostFactory extends AbstractFactory
{ {
/** /** @var EntityManagerInterface */
* @var EntityManagerInterface
*/
private $em; private $em;
/** /** @var PostRepository */
* @var PostRepository
*/
private $postRepository; private $postRepository;
/** /** @var UserFactory */
* @var UserFactory
*/
private $userFactory; private $userFactory;
/** /** @var FileFactory */
* @var FileFactory
*/
private $fileFactory; private $fileFactory;
/** /** @var CommentFactory */
* @var CommentFactory
*/
private $commentFactory; private $commentFactory;
/** /** @var TagFactory */
* @var TagFactory
*/
private $tagFactory; private $tagFactory;

View file

@ -9,9 +9,7 @@ use Skobkin\Bundle\PointToolsBundle\Service\Factory\AbstractFactory;
class TagFactory extends AbstractFactory class TagFactory extends AbstractFactory
{ {
/** /** @var TagRepository */
* @var TagRepository
*/
private $tagRepository; private $tagRepository;

View file

@ -10,9 +10,7 @@ use unreal4u\TelegramAPI\Telegram\Types\Message;
class AccountFactory extends AbstractFactory class AccountFactory extends AbstractFactory
{ {
/** /** @var AccountRepository */
* @var AccountRepository
*/
private $accountRepo; private $accountRepo;
@ -30,12 +28,12 @@ class AccountFactory extends AbstractFactory
} }
// Setting/updating account data // Setting/updating account data
$account $account->updateFromMessageData(
->setFirstName($message->from->first_name) $message->from->first_name,
->setLastName($message->from->last_name) $message->from->last_name,
->setUsername($message->from->username) $message->from->username,
->setChatId($message->chat->id) $message->chat->id
; );
return $account; return $account;
} }

View file

@ -3,34 +3,23 @@
namespace Skobkin\Bundle\PointToolsBundle\Service; namespace Skobkin\Bundle\PointToolsBundle\Service;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Skobkin\Bundle\PointToolsBundle\Entity\Subscription; use Skobkin\Bundle\PointToolsBundle\Entity\{Subscription, SubscriptionEvent, User};
use Skobkin\Bundle\PointToolsBundle\Entity\SubscriptionEvent;
use Skobkin\Bundle\PointToolsBundle\Entity\User;
use Skobkin\Bundle\PointToolsBundle\Event\UserSubscribersUpdatedEvent; use Skobkin\Bundle\PointToolsBundle\Event\UserSubscribersUpdatedEvent;
use Skobkin\Bundle\PointToolsBundle\Repository\SubscriptionEventRepository; use Skobkin\Bundle\PointToolsBundle\Repository\{SubscriptionEventRepository, SubscriptionRepository};
use Skobkin\Bundle\PointToolsBundle\Repository\SubscriptionRepository;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class SubscriptionsManager class SubscriptionsManager
{ {
/** /** @var SubscriptionRepository */
* @var SubscriptionRepository
*/
private $subscriptionRepo; private $subscriptionRepo;
/** /** @var SubscriptionEventRepository */
* @var SubscriptionEventRepository
*/
private $subscriptionRecordRepo; private $subscriptionRecordRepo;
/** /** @var EventDispatcherInterface */
* @var EventDispatcherInterface
*/
private $eventDispatcher; private $eventDispatcher;
/** /** @var LoggerInterface */
* @var LoggerInterface
*/
private $logger; private $logger;

View file

@ -12,14 +12,10 @@ class IncomingUpdateDispatcher
const CHAT_TYPE_PRIVATE = 'private'; const CHAT_TYPE_PRIVATE = 'private';
const CHAT_TYPE_GROUP = 'group'; const CHAT_TYPE_GROUP = 'group';
/** /** @var InlineQueryProcessor */
* @var InlineQueryProcessor
*/
private $inlineQueryProcessor; private $inlineQueryProcessor;
/** /** @var PrivateMessageProcessor */
* @var PrivateMessageProcessor
*/
private $privateMessageProcessor; private $privateMessageProcessor;

View file

@ -10,14 +10,10 @@ use unreal4u\TelegramAPI\TgLog;
class InlineQueryProcessor class InlineQueryProcessor
{ {
/** /** @var UserRepository */
* @var UserRepository
*/
private $userRepo; private $userRepo;
/** /** @var TgLog */
* @var TgLog
*/
private $client; private $client;

View file

@ -17,14 +17,10 @@ class MessageSender
public const PARSE_MARKDOWN = 'Markdown'; public const PARSE_MARKDOWN = 'Markdown';
public const PARSE_HTML5 = 'HTML'; public const PARSE_HTML5 = 'HTML';
/** /** @var TgLog */
* @var TgLog
*/
private $client; private $client;
/** /** @var \Twig_Environment */
* @var \Twig_Environment
*/
private $twig; private $twig;
/** /**

View file

@ -10,14 +10,10 @@ use Skobkin\Bundle\PointToolsBundle\Repository\Telegram\AccountRepository;
*/ */
class Notifier class Notifier
{ {
/** /** @var AccountRepository */
* @var AccountRepository
*/
private $accountsRepo; private $accountsRepo;
/** /** @var MessageSender */
* @var MessageSender
*/
private $messenger; private $messenger;

View file

@ -3,67 +3,41 @@
namespace Skobkin\Bundle\PointToolsBundle\Service\Telegram; namespace Skobkin\Bundle\PointToolsBundle\Service\Telegram;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Skobkin\Bundle\PointToolsBundle\Entity\Telegram\Account; use Skobkin\Bundle\PointToolsBundle\Entity\{Telegram\Account, User};
use Skobkin\Bundle\PointToolsBundle\Entity\User;
use Skobkin\Bundle\PointToolsBundle\Exception\Telegram\CommandProcessingException; use Skobkin\Bundle\PointToolsBundle\Exception\Telegram\CommandProcessingException;
use Skobkin\Bundle\PointToolsBundle\Repository\SubscriptionEventRepository; use Skobkin\Bundle\PointToolsBundle\Repository\{SubscriptionEventRepository, SubscriptionRepository, Telegram\AccountRepository, UserRepository};
use Skobkin\Bundle\PointToolsBundle\Repository\SubscriptionRepository;
use Skobkin\Bundle\PointToolsBundle\Repository\Telegram\AccountRepository;
use Skobkin\Bundle\PointToolsBundle\Repository\UserRepository;
use Skobkin\Bundle\PointToolsBundle\Service\Factory\Telegram\AccountFactory; use Skobkin\Bundle\PointToolsBundle\Service\Factory\Telegram\AccountFactory;
use Skobkin\Bundle\PointToolsBundle\Service\Api\UserApi; use Skobkin\Bundle\PointToolsBundle\Service\Api\UserApi;
use unreal4u\TelegramAPI\Telegram\Types\Message; use unreal4u\TelegramAPI\Telegram\Types\{Message, ReplyKeyboardMarkup, ReplyKeyboardRemove};
use unreal4u\TelegramAPI\Telegram\Types\ReplyKeyboardMarkup;
use unreal4u\TelegramAPI\Telegram\Types\ReplyKeyboardRemove;
/** /** Processes all private messages */
* Processes all private messages
*/
class PrivateMessageProcessor class PrivateMessageProcessor
{ {
/** /** @var MessageSender */
* @var MessageSender
*/
private $messenger; private $messenger;
/** /** @var UserApi */
* @var UserApi
*/
private $userApi; private $userApi;
/** /** @var AccountFactory */
* @var AccountFactory
*/
private $accountFactory; private $accountFactory;
/** /** @var EntityManagerInterface */
* @var EntityManagerInterface
*/
private $em; private $em;
/** /** @var UserRepository */
* @var UserRepository
*/
private $userRepo; private $userRepo;
/** /** @var AccountRepository */
* @var AccountRepository
*/
private $accountRepo; private $accountRepo;
/** /** @var SubscriptionRepository */
* @var SubscriptionRepository
*/
private $subscriptionRepo; private $subscriptionRepo;
/** /** @var SubscriptionEventRepository */
* @var SubscriptionEventRepository
*/
private $subscriptionEventRepo; private $subscriptionEventRepo;
/** /** @var int */
* @var int
*/
private $pointUserId; private $pointUserId;
@ -75,7 +49,8 @@ class PrivateMessageProcessor
SubscriptionEventRepository $subscriptionRecordRepository, SubscriptionEventRepository $subscriptionRecordRepository,
MessageSender $messageSender, MessageSender $messageSender,
UserApi $userApi, UserApi $userApi,
AccountFactory $accountFactory AccountFactory $accountFactory,
int $appUserId
) { ) {
$this->em = $em; $this->em = $em;
$this->userRepo = $userRepository; $this->userRepo = $userRepository;
@ -85,11 +60,7 @@ class PrivateMessageProcessor
$this->messenger = $messageSender; $this->messenger = $messageSender;
$this->userApi = $userApi; $this->userApi = $userApi;
$this->accountFactory = $accountFactory; $this->accountFactory = $accountFactory;
} $this->pointUserId = $appUserId;
public function setPointUserId(int $pointUserId)
{
$this->pointUserId = $pointUserId;
} }
public function process(Message $message): void public function process(Message $message): void