2015-05-28 22:47:06 +00:00
|
|
|
<?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;
|
2015-05-28 22:47:06 +00:00
|
|
|
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;
|
2015-05-28 22:47:06 +00:00
|
|
|
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')
|
2016-07-29 19:03:43 +00:00
|
|
|
->addOption(
|
|
|
|
'all-users',
|
|
|
|
null,
|
|
|
|
InputOption::VALUE_NONE,
|
|
|
|
'If set, command will check subscribers of all service users instead of service subscribers only'
|
|
|
|
)
|
2015-05-28 22:47:06 +00:00
|
|
|
->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
|
|
|
/**
|
2016-12-12 17:57:03 +00:00
|
|
|
* @param InputInterface $input
|
|
|
|
* @param OutputInterface $output
|
|
|
|
*
|
2015-05-30 22:50:35 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2015-05-28 22:47:06 +00:00
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
2015-05-30 22:50:35 +00:00
|
|
|
$log = $this->getContainer()->get('logger');
|
2016-07-29 19:03:43 +00:00
|
|
|
$em = $this->getContainer()->get('doctrine.orm.entity_manager');
|
|
|
|
$userRepository = $em->getRepository('SkobkinPointToolsBundle:User');
|
2015-05-30 22:50:35 +00:00
|
|
|
|
2017-01-09 01:21:31 +00:00
|
|
|
$log->debug('UpdateSubscriptionsCommand started.');
|
2015-05-30 22:50:35 +00:00
|
|
|
|
2015-05-28 22:47:06 +00:00
|
|
|
/** @var UserApi $api */
|
2017-01-08 23:26:06 +00:00
|
|
|
$api = $this->getContainer()->get('app.point.api_user');
|
2015-05-28 22:47:06 +00:00
|
|
|
/** @var SubscriptionsManager $subscriptionsManager */
|
2017-01-08 23:26:06 +00:00
|
|
|
$subscriptionsManager = $this->getContainer()->get('app.point.subscriptions_manager');
|
2015-05-28 22:47:06 +00:00
|
|
|
|
2015-06-02 01:59:07 +00:00
|
|
|
try {
|
|
|
|
$serviceUserId = $this->getContainer()->getParameter('point_id');
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
$log->alert('Could not get point_id parameter from config file', ['exception_message' => $e->getMessage()]);
|
2016-12-12 17:57:03 +00:00
|
|
|
return 1;
|
2015-06-02 01:59:07 +00:00
|
|
|
}
|
|
|
|
|
2017-01-06 21:38:20 +00:00
|
|
|
// Beginning transaction for all changes
|
|
|
|
$em->beginTransaction();
|
|
|
|
|
2016-07-29 19:03:43 +00:00
|
|
|
if ($input->getOption('all-users')) {
|
|
|
|
$usersForUpdate = $userRepository->findAll();
|
|
|
|
} else {
|
|
|
|
$serviceUser = $userRepository->find($serviceUserId);
|
2015-05-28 22:47:06 +00:00
|
|
|
|
2016-07-29 19:03:43 +00:00
|
|
|
if (!$serviceUser) {
|
|
|
|
$log->info('Service user not found');
|
|
|
|
// @todo Retrieving user
|
2015-05-30 22:50:35 +00:00
|
|
|
|
2016-12-12 17:57:03 +00:00
|
|
|
return 1;
|
2016-07-29 19:03:43 +00:00
|
|
|
}
|
2015-05-28 22:47:06 +00:00
|
|
|
|
2017-01-09 01:21:31 +00:00
|
|
|
$log->info('Getting service subscribers');
|
2015-05-30 05:29:40 +00:00
|
|
|
|
2016-07-29 19:03:43 +00:00
|
|
|
try {
|
|
|
|
$usersForUpdate = $api->getUserSubscribersById($serviceUserId);
|
|
|
|
} catch (\Exception $e) {
|
2017-01-09 01:21:31 +00:00
|
|
|
$log->warning(
|
|
|
|
'Error while getting service subscribers. Fallback to local list.',
|
|
|
|
[
|
|
|
|
'user_login' => $serviceUser->getLogin(),
|
|
|
|
'user_id' => $serviceUser->getId(),
|
|
|
|
'message' => $e->getMessage(),
|
|
|
|
'file' => $e->getFile(),
|
|
|
|
'line' => $e->getLine()
|
|
|
|
]
|
|
|
|
);
|
2015-05-30 22:50:35 +00:00
|
|
|
|
2016-07-29 19:03:43 +00:00
|
|
|
$usersForUpdate = [];
|
2015-06-02 02:39:09 +00:00
|
|
|
|
2016-07-29 19:03:43 +00:00
|
|
|
foreach ($serviceUser->getSubscribers() as $subscription) {
|
|
|
|
$usersForUpdate[] = $subscription->getSubscriber();
|
|
|
|
}
|
2015-06-02 02:39:09 +00:00
|
|
|
|
2016-07-29 19:03:43 +00:00
|
|
|
if (!count($usersForUpdate)) {
|
|
|
|
$log->info('No local subscribers. Finishing.');
|
2016-12-12 17:57:03 +00:00
|
|
|
return 0;
|
2016-07-29 19:03:43 +00:00
|
|
|
}
|
2015-06-02 02:39:09 +00:00
|
|
|
}
|
2015-05-30 05:29:40 +00:00
|
|
|
|
2017-01-09 01:21:31 +00:00
|
|
|
$log->debug('Updating service subscribers');
|
2015-05-28 22:47:06 +00:00
|
|
|
|
2016-07-29 19:03:43 +00:00
|
|
|
// Updating service subscribers
|
|
|
|
try {
|
|
|
|
$subscriptionsManager->updateUserSubscribers($serviceUser, $usersForUpdate);
|
|
|
|
} catch (\Exception $e) {
|
2017-01-09 01:21:31 +00:00
|
|
|
$log->error(
|
|
|
|
'Error while updating service subscribers',
|
|
|
|
[
|
|
|
|
'user_login' => $serviceUser->getLogin(),
|
|
|
|
'user_id' => $serviceUser->getId(),
|
|
|
|
'message' => $e->getMessage(),
|
|
|
|
'file' => $e->getFile(),
|
|
|
|
'line' => $e->getLine()
|
|
|
|
]
|
|
|
|
);
|
2015-05-30 22:50:35 +00:00
|
|
|
|
2016-12-12 17:57:03 +00:00
|
|
|
return 1;
|
2016-07-29 19:03:43 +00:00
|
|
|
}
|
2015-05-30 22:50:35 +00:00
|
|
|
}
|
2015-05-28 22:47:06 +00:00
|
|
|
|
2017-01-09 01:21:31 +00:00
|
|
|
$log->info('Processing users subscribers');
|
2015-05-30 05:29:40 +00:00
|
|
|
|
2016-07-29 19:03:43 +00:00
|
|
|
// Updating users subscribers
|
|
|
|
foreach ($usersForUpdate as $user) {
|
2017-01-09 01:21:31 +00:00
|
|
|
$log->info('Processing @'.$user->getLogin());
|
2015-05-30 05:29:40 +00:00
|
|
|
|
|
|
|
try {
|
2015-06-02 01:59:07 +00:00
|
|
|
$userCurrentSubscribers = $api->getUserSubscribersById($user->getId());
|
2015-05-30 05:29:40 +00:00
|
|
|
} catch (\Exception $e) {
|
2017-01-09 01:21:31 +00:00
|
|
|
$log->error(
|
|
|
|
'Error while getting subscribers. Skipping.',
|
|
|
|
[
|
|
|
|
'user_login' => $user->getLogin(),
|
|
|
|
'user_id' => $user->getId(),
|
|
|
|
'message' => $e->getMessage(),
|
|
|
|
'file' => $e->getFile(),
|
|
|
|
'line' => $e->getLine()
|
|
|
|
]
|
|
|
|
);
|
2015-05-30 22:50:35 +00:00
|
|
|
|
2015-05-30 05:29:40 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-01-09 01:21:31 +00:00
|
|
|
$log->debug('Updating user subscribers');
|
2015-05-28 22:47:06 +00:00
|
|
|
|
2015-05-30 22:50:35 +00:00
|
|
|
try {
|
|
|
|
// Updating user subscribers
|
|
|
|
$subscriptionsManager->updateUserSubscribers($user, $userCurrentSubscribers);
|
|
|
|
} catch (\Exception $e) {
|
2017-01-09 01:21:31 +00:00
|
|
|
$log->error(
|
|
|
|
'Error while updating user subscribers',
|
|
|
|
[
|
|
|
|
'user_login' => $user->getLogin(),
|
|
|
|
'user_id' => $user->getId(),
|
|
|
|
'message' => $e->getMessage(),
|
|
|
|
'file' => $e->getFile(),
|
|
|
|
'line' => $e->getLine()
|
|
|
|
]
|
|
|
|
);
|
2015-05-30 22:50:35 +00:00
|
|
|
}
|
2015-05-28 22:47:06 +00:00
|
|
|
|
2015-05-31 07:49:45 +00:00
|
|
|
// @todo move to the config
|
2015-06-23 09:39:30 +00:00
|
|
|
usleep(500000);
|
2015-05-28 22:47:06 +00:00
|
|
|
}
|
2016-12-12 17:57:03 +00:00
|
|
|
|
2017-01-06 21:38:20 +00:00
|
|
|
// Flushing all changes at once to database
|
|
|
|
$em->flush();
|
|
|
|
$em->commit();
|
|
|
|
|
2017-01-09 01:21:31 +00:00
|
|
|
$log->debug('Finished');
|
|
|
|
|
2016-12-12 17:57:03 +00:00
|
|
|
return 0;
|
2015-05-28 22:47:06 +00:00
|
|
|
}
|
|
|
|
}
|