UpdateSubscriptionsCommand "--all-users" option.

This commit is contained in:
Alexey Skobkin 2016-07-29 22:03:43 +03:00
parent a8cffc776d
commit 6abb1f647c

View file

@ -18,6 +18,12 @@ class UpdateSubscriptionsCommand extends ContainerAwareCommand
$this $this
->setName('point:update:subscriptions') ->setName('point:update:subscriptions')
->setDescription('Update subscriptions of users subscribed to service') ->setDescription('Update subscriptions of users subscribed to service')
->addOption(
'all-users',
null,
InputOption::VALUE_NONE,
'If set, command will check subscribers of all service users instead of service subscribers only'
)
->addOption( ->addOption(
'check-only', 'check-only',
null, null,
@ -36,6 +42,8 @@ class UpdateSubscriptionsCommand extends ContainerAwareCommand
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
$log = $this->getContainer()->get('logger'); $log = $this->getContainer()->get('logger');
$em = $this->getContainer()->get('doctrine.orm.entity_manager');
$userRepository = $em->getRepository('SkobkinPointToolsBundle:User');
$log->info('UpdateSubscriptionsCommand started.'); $log->info('UpdateSubscriptionsCommand started.');
@ -51,7 +59,10 @@ class UpdateSubscriptionsCommand extends ContainerAwareCommand
return false; return false;
} }
$serviceUser = $this->getContainer()->get('doctrine.orm.entity_manager')->getRepository('SkobkinPointToolsBundle:User')->find($serviceUserId); if ($input->getOption('all-users')) {
$usersForUpdate = $userRepository->findAll();
} else {
$serviceUser = $userRepository->find($serviceUserId);
if (!$serviceUser) { if (!$serviceUser) {
$log->info('Service user not found'); $log->info('Service user not found');
@ -65,21 +76,21 @@ class UpdateSubscriptionsCommand extends ContainerAwareCommand
} }
try { try {
$serviceSubscribers = $api->getUserSubscribersById($serviceUserId); $usersForUpdate = $api->getUserSubscribersById($serviceUserId);
} catch (\Exception $e) { } catch (\Exception $e) {
$output->writeln('Error while getting service subscribers'); $output->writeln('Error while getting service subscribers');
$log->error('Error while getting service subscribers.', ['user_login' => $serviceUser->getLogin(), 'user_id' => $serviceUser->getId(), 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]); $log->error('Error while getting service subscribers.', ['user_login' => $serviceUser->getLogin(), 'user_id' => $serviceUser->getId(), 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
$serviceSubscribers = []; $usersForUpdate = [];
foreach ($serviceUser->getSubscribers() as $subscription) { foreach ($serviceUser->getSubscribers() as $subscription) {
$serviceSubscribers[] = $subscription->getSubscriber(); $usersForUpdate[] = $subscription->getSubscriber();
} }
$output->writeln('Fallback to local list'); $output->writeln('Fallback to local list');
$log->error('Fallback to local list'); $log->error('Fallback to local list');
if (!count($serviceSubscribers)) { if (!count($usersForUpdate)) {
$log->info('No local subscribers. Finishing.'); $log->info('No local subscribers. Finishing.');
return false; return false;
} }
@ -91,19 +102,20 @@ class UpdateSubscriptionsCommand extends ContainerAwareCommand
// Updating service subscribers // Updating service subscribers
try { try {
$subscriptionsManager->updateUserSubscribers($serviceUser, $serviceSubscribers); $subscriptionsManager->updateUserSubscribers($serviceUser, $usersForUpdate);
} catch (\Exception $e) { } catch (\Exception $e) {
$log->error('Error while updating service subscribers', ['user_login' => $serviceUser->getLogin(), 'user_id' => $serviceUser->getId(), 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]); $log->error('Error while updating service subscribers', ['user_login' => $serviceUser->getLogin(), 'user_id' => $serviceUser->getId(), 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
return false; return false;
} }
if ($output->isVerbose()) {
$output->writeln('Processing service subscribers');
} }
// Updating service users subscribers if ($output->isVerbose()) {
foreach ($serviceSubscribers as $user) { $output->writeln('Processing users subscribers');
}
// Updating users subscribers
foreach ($usersForUpdate as $user) {
$output->writeln(' Processing @' . $user->getLogin()); $output->writeln(' Processing @' . $user->getLogin());
$log->info('Processing @' . $user->getLogin()); $log->info('Processing @' . $user->getLogin());