point-tools/src/Skobkin/Bundle/PointToolsBundle/Command/UpdateSubscriptionsCommand.php

128 lines
4.4 KiB
PHP
Raw Normal View History

<?php
namespace Skobkin\Bundle\PointToolsBundle\Command;
use Skobkin\Bundle\PointToolsBundle\Service\SubscriptionsManager;
use Skobkin\Bundle\PointToolsBundle\Service\UserApi;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
2015-05-30 22:50:35 +00:00
use Symfony\Component\Console\Input\Input;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
2015-05-30 22:50:35 +00:00
use Symfony\Component\Console\Output\Output;
use Symfony\Component\Console\Output\OutputInterface;
class UpdateSubscriptionsCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('point:update:subscriptions')
->setDescription('Update subscriptions of users subscribed to service')
->addOption(
'check-only',
null,
InputOption::VALUE_NONE,
'If set, command will not perform write operations in the database'
)
// @todo add option for checking only selected user
;
}
2015-05-30 22:50:35 +00:00
/**
* @param Input $input
* @param Output $output
* @return bool
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
2015-05-30 22:50:35 +00:00
$log = $this->getContainer()->get('logger');
$log->info('UpdateSubscriptionsCommand started.');
/** @var UserApi $api */
$api = $this->getContainer()->get('skobkin_point_tools.api_user');
/** @var SubscriptionsManager $subscriptionsManager */
$subscriptionsManager = $this->getContainer()->get('skobkin_point_tools.subscriptions_manager');
$serviceUserName = $this->getContainer()->getParameter('point_login');
$serviceUser = $this->getContainer()->get('doctrine.orm.entity_manager')->getRepository('SkobkinPointToolsBundle:User')->findOneBy(['login' => $serviceUserName]);
if (!$serviceUser) {
2015-05-30 22:50:35 +00:00
$log->info('Service user not found');
// @todo Retrieving user
2015-05-30 22:50:35 +00:00
return false;
}
if ($output->isVerbose()) {
$output->writeln('Getting service subscribers');
}
try {
$serviceSubscribers = $api->getUserSubscribersByLogin($serviceUserName);
} catch (\Exception $e) {
2015-05-30 22:50:35 +00:00
// @todo fallback to the local subscribers list
$output->writeln('Error while getting service subscribers');
2015-05-30 22:50:35 +00:00
$log->error('Error while getting service subscribers.' . PHP_EOL .
$e->getMessage() . PHP_EOL .
$e->getFile() . ':' . $e->getLine()
);
return false;
}
if ($output->isVerbose()) {
$output->writeln('Updating service subscribers');
}
// Updating service subscribers
2015-05-30 22:50:35 +00:00
try {
$subscriptionsManager->updateUserSubscribers($serviceUser, $serviceSubscribers);
} catch (\Exception $e) {
$log->error('Error while updating service subscribers' . PHP_EOL .
$e->getMessage() . PHP_EOL .
$e->getFile() . ':' . $e->getLine()
);
return false;
}
if ($output->isVerbose()) {
$output->writeln('Processing service subscribers');
}
// Updating service users subscribers
foreach ($serviceSubscribers as $user) {
$output->writeln(' Processing @' . $user->getLogin());
2015-05-30 22:50:35 +00:00
$log->info('Processing @' . $user->getLogin());
try {
$userCurrentSubscribers = $api->getUserSubscribersByLogin($user->getLogin());
} catch (\Exception $e) {
$output->writeln(' Error while getting subscribers. Skipping.');
2015-05-30 22:50:35 +00:00
$log->error('Error while getting subscribers.' . PHP_EOL .
$e->getMessage() . PHP_EOL .
$e->getFile() . ':' . $e->getLine()
);
continue;
}
if ($output->isVerbose()) {
2015-05-30 22:50:35 +00:00
$output->writeln(' Updating user subscribers');
}
2015-05-30 22:50:35 +00:00
try {
// Updating user subscribers
$subscriptionsManager->updateUserSubscribers($user, $userCurrentSubscribers);
} catch (\Exception $e) {
$log->error('Error while updating user subscribers' . PHP_EOL .
$e->getMessage() . PHP_EOL .
$e->getFile() . ':' . $e->getLine()
);
}
// @todo some pause for lower API load
}
}
}