2015-05-28 22:47:06 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Skobkin\Bundle\PointToolsBundle\Command;
|
|
|
|
|
2017-01-09 02:22:56 +00:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use Skobkin\Bundle\PointToolsBundle\Entity\Subscription;
|
|
|
|
use Skobkin\Bundle\PointToolsBundle\Entity\User;
|
|
|
|
use Skobkin\Bundle\PointToolsBundle\Repository\UserRepository;
|
2015-05-28 22:47:06 +00:00
|
|
|
use Skobkin\Bundle\PointToolsBundle\Service\SubscriptionsManager;
|
|
|
|
use Skobkin\Bundle\PointToolsBundle\Service\UserApi;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
|
2017-01-09 02:35:18 +00:00
|
|
|
use Symfony\Component\Console\Helper\ProgressBar;
|
2015-05-28 22:47:06 +00:00
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
2017-01-09 02:46:52 +00:00
|
|
|
/**
|
|
|
|
* @todo https://symfony.com/doc/current/console/lockable_trait.html
|
|
|
|
*/
|
2015-05-28 22:47:06 +00:00
|
|
|
class UpdateSubscriptionsCommand extends ContainerAwareCommand
|
|
|
|
{
|
2017-01-09 02:22:56 +00:00
|
|
|
/**
|
|
|
|
* @var EntityManagerInterface
|
|
|
|
*/
|
|
|
|
private $em;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var LoggerInterface
|
|
|
|
*/
|
|
|
|
private $logger;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var UserRepository
|
|
|
|
*/
|
|
|
|
private $userRepo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var InputInterface
|
|
|
|
*/
|
|
|
|
private $input;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var UserApi
|
|
|
|
*/
|
|
|
|
private $api;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $apiDelay = 500000;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var SubscriptionsManager
|
|
|
|
*/
|
|
|
|
private $subscriptionManager;
|
|
|
|
|
2017-01-09 02:35:18 +00:00
|
|
|
/**
|
|
|
|
* @var ProgressBar
|
|
|
|
*/
|
|
|
|
private $progress;
|
|
|
|
|
2017-01-09 02:22:56 +00:00
|
|
|
|
|
|
|
public function setLogger(LoggerInterface $logger)
|
|
|
|
{
|
|
|
|
$this->logger = $logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setEntityManager(EntityManagerInterface $em)
|
|
|
|
{
|
|
|
|
$this->em = $em;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setApiClient(UserApi $userApi)
|
|
|
|
{
|
|
|
|
$this->api = $userApi;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setApiDelay(int $microSecs)
|
|
|
|
{
|
|
|
|
$this->apiDelay = $microSecs;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setSubscriptionManager(SubscriptionsManager $subscriptionsManager)
|
|
|
|
{
|
|
|
|
$this->subscriptionManager = $subscriptionsManager;
|
|
|
|
}
|
|
|
|
|
2015-05-28 22:47:06 +00:00
|
|
|
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
|
|
|
|
*
|
2017-01-09 02:53:58 +00:00
|
|
|
* @return int
|
2015-05-30 22:50:35 +00:00
|
|
|
*/
|
2015-05-28 22:47:06 +00:00
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
2017-01-09 02:22:56 +00:00
|
|
|
$this->input = $input;
|
|
|
|
$this->userRepo = $this->em->getRepository('SkobkinPointToolsBundle:User');
|
2015-05-30 22:50:35 +00:00
|
|
|
|
2017-01-09 02:22:56 +00:00
|
|
|
$this->logger->debug('UpdateSubscriptionsCommand started.');
|
2015-05-28 22:47:06 +00:00
|
|
|
|
2015-06-02 01:59:07 +00:00
|
|
|
try {
|
2017-01-09 02:22:56 +00:00
|
|
|
$appUserId = $this->getContainer()->getParameter('point_id');
|
2015-06-02 01:59:07 +00:00
|
|
|
} catch (\InvalidArgumentException $e) {
|
2017-01-09 02:22:56 +00:00
|
|
|
$this->logger->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-09 02:35:18 +00:00
|
|
|
$this->progress = new ProgressBar($output);
|
|
|
|
$this->progress->setFormat('debug');
|
|
|
|
|
2017-01-06 21:38:20 +00:00
|
|
|
// Beginning transaction for all changes
|
2017-01-09 02:22:56 +00:00
|
|
|
$this->em->beginTransaction();
|
2017-01-06 21:38:20 +00:00
|
|
|
|
2017-01-09 02:35:18 +00:00
|
|
|
$this->progress->setMessage('Getting service subscribers');
|
|
|
|
|
2017-01-09 02:22:56 +00:00
|
|
|
try {
|
|
|
|
$usersForUpdate = $this->getUsersForUpdate($appUserId);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->logger->error('Error while getting service subscribers', ['exception' => get_class($e), 'message' => $e->getMessage()]);
|
2015-05-28 22:47:06 +00:00
|
|
|
|
2017-01-09 02:22:56 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2015-05-30 22:50:35 +00:00
|
|
|
|
2017-01-09 02:22:56 +00:00
|
|
|
if (0 === count($usersForUpdate)) {
|
|
|
|
$this->logger->info('No local subscribers. Finishing.');
|
2015-05-28 22:47:06 +00:00
|
|
|
|
2017-01-09 02:22:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->logger->info('Processing users subscribers');
|
2017-01-09 02:35:18 +00:00
|
|
|
$this->progress->setMessage('Processing users subscribers');
|
|
|
|
$this->progress->start(count($usersForUpdate));
|
2017-01-09 02:22:56 +00:00
|
|
|
|
|
|
|
$this->updateUsersSubscribers($usersForUpdate);
|
|
|
|
|
2017-01-09 02:35:18 +00:00
|
|
|
$this->progress->finish();
|
|
|
|
|
2017-01-09 02:22:56 +00:00
|
|
|
// Flushing all changes at once to database
|
|
|
|
$this->em->flush();
|
|
|
|
$this->em->commit();
|
|
|
|
|
|
|
|
$this->logger->debug('Finished');
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param User[] $users
|
|
|
|
*/
|
|
|
|
private function updateUsersSubscribers(array $users)
|
|
|
|
{
|
|
|
|
// Updating users subscribers
|
|
|
|
foreach ($users as $user) {
|
|
|
|
$this->logger->info('Processing @'.$user->getLogin());
|
2015-05-30 05:29:40 +00:00
|
|
|
|
2016-07-29 19:03:43 +00:00
|
|
|
try {
|
2017-01-09 02:22:56 +00:00
|
|
|
$userCurrentSubscribers = $this->api->getUserSubscribersById($user->getId());
|
2016-07-29 19:03:43 +00:00
|
|
|
} catch (\Exception $e) {
|
2017-01-09 02:22:56 +00:00
|
|
|
$this->logger->error(
|
|
|
|
'Error while getting subscribers. Skipping.',
|
2017-01-09 01:21:31 +00:00
|
|
|
[
|
2017-01-09 02:22:56 +00:00
|
|
|
'user_login' => $user->getLogin(),
|
|
|
|
'user_id' => $user->getId(),
|
2017-01-09 01:21:31 +00:00
|
|
|
'message' => $e->getMessage(),
|
|
|
|
'file' => $e->getFile(),
|
2017-01-09 02:22:56 +00:00
|
|
|
'line' => $e->getLine(),
|
2017-01-09 01:21:31 +00:00
|
|
|
]
|
|
|
|
);
|
2015-05-30 22:50:35 +00:00
|
|
|
|
2017-01-09 02:22:56 +00:00
|
|
|
continue;
|
2015-06-02 02:39:09 +00:00
|
|
|
}
|
2015-05-30 05:29:40 +00:00
|
|
|
|
2017-01-09 02:22:56 +00:00
|
|
|
$this->logger->debug('Updating user subscribers');
|
2015-05-28 22:47:06 +00:00
|
|
|
|
2016-07-29 19:03:43 +00:00
|
|
|
try {
|
2017-01-09 02:22:56 +00:00
|
|
|
// Updating user subscribers
|
|
|
|
$this->subscriptionManager->updateUserSubscribers($user, $userCurrentSubscribers);
|
2016-07-29 19:03:43 +00:00
|
|
|
} catch (\Exception $e) {
|
2017-01-09 02:22:56 +00:00
|
|
|
$this->logger->error(
|
|
|
|
'Error while updating user subscribers',
|
2017-01-09 01:21:31 +00:00
|
|
|
[
|
2017-01-09 02:22:56 +00:00
|
|
|
'user_login' => $user->getLogin(),
|
|
|
|
'user_id' => $user->getId(),
|
2017-01-09 01:21:31 +00:00
|
|
|
'message' => $e->getMessage(),
|
|
|
|
'file' => $e->getFile(),
|
2017-01-09 02:22:56 +00:00
|
|
|
'line' => $e->getLine(),
|
2017-01-09 01:21:31 +00:00
|
|
|
]
|
|
|
|
);
|
2016-07-29 19:03:43 +00:00
|
|
|
}
|
2017-01-09 02:22:56 +00:00
|
|
|
|
2017-01-09 02:35:18 +00:00
|
|
|
$this->progress->advance();
|
|
|
|
|
2017-01-09 02:22:56 +00:00
|
|
|
usleep($this->apiDelay);
|
2015-05-30 22:50:35 +00:00
|
|
|
}
|
2017-01-09 02:22:56 +00:00
|
|
|
}
|
2015-05-28 22:47:06 +00:00
|
|
|
|
2017-01-09 02:22:56 +00:00
|
|
|
private function getUsersForUpdate(int $appUserId): array
|
|
|
|
{
|
|
|
|
if ($this->input->getOption('all-users')) {
|
|
|
|
$usersForUpdate = $this->userRepo->findAll();
|
|
|
|
} else {
|
|
|
|
/** @var User $serviceUser */
|
|
|
|
$serviceUser = $this->userRepo->find($appUserId);
|
2015-05-30 05:29:40 +00:00
|
|
|
|
2017-01-09 02:22:56 +00:00
|
|
|
if (!$serviceUser) {
|
|
|
|
$this->logger->info('Service user not found');
|
|
|
|
// @todo Retrieving user
|
|
|
|
|
|
|
|
throw new \RuntimeException('Service user not found in the database');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->logger->info('Getting service subscribers');
|
2015-05-30 05:29:40 +00:00
|
|
|
|
|
|
|
try {
|
2017-01-09 02:22:56 +00:00
|
|
|
$usersForUpdate = $this->api->getUserSubscribersById($appUserId);
|
2015-05-30 05:29:40 +00:00
|
|
|
} catch (\Exception $e) {
|
2017-01-09 02:22:56 +00:00
|
|
|
$this->logger->warning(
|
|
|
|
'Error while getting service subscribers. Fallback to local list.',
|
2017-01-09 01:21:31 +00:00
|
|
|
[
|
2017-01-09 02:22:56 +00:00
|
|
|
'user_login' => $serviceUser->getLogin(),
|
|
|
|
'user_id' => $serviceUser->getId(),
|
2017-01-09 01:21:31 +00:00
|
|
|
'message' => $e->getMessage(),
|
|
|
|
'file' => $e->getFile(),
|
2017-01-09 02:22:56 +00:00
|
|
|
'line' => $e->getLine(),
|
2017-01-09 01:21:31 +00:00
|
|
|
]
|
|
|
|
);
|
2015-05-30 22:50:35 +00:00
|
|
|
|
2017-01-09 02:22:56 +00:00
|
|
|
$usersForUpdate = [];
|
|
|
|
|
|
|
|
/** @var Subscription $subscription */
|
2017-01-09 03:03:29 +00:00
|
|
|
foreach ((array) $serviceUser->getSubscribers() as $subscription) {
|
2017-01-09 02:22:56 +00:00
|
|
|
$usersForUpdate[] = $subscription->getSubscriber();
|
|
|
|
}
|
2015-05-30 05:29:40 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 02:22:56 +00:00
|
|
|
$this->logger->debug('Updating service subscribers');
|
2015-05-28 22:47:06 +00:00
|
|
|
|
2017-01-09 02:22:56 +00:00
|
|
|
// Updating service subscribers
|
2015-05-30 22:50:35 +00:00
|
|
|
try {
|
2017-01-09 02:22:56 +00:00
|
|
|
$this->subscriptionManager->updateUserSubscribers($serviceUser, $usersForUpdate);
|
2015-05-30 22:50:35 +00:00
|
|
|
} catch (\Exception $e) {
|
2017-01-09 02:22:56 +00:00
|
|
|
$this->logger->error(
|
|
|
|
'Error while updating service subscribers',
|
2017-01-09 01:21:31 +00:00
|
|
|
[
|
2017-01-09 02:22:56 +00:00
|
|
|
'user_login' => $serviceUser->getLogin(),
|
|
|
|
'user_id' => $serviceUser->getId(),
|
2017-01-09 01:21:31 +00:00
|
|
|
'message' => $e->getMessage(),
|
|
|
|
'file' => $e->getFile(),
|
2017-01-09 02:22:56 +00:00
|
|
|
'line' => $e->getLine(),
|
2017-01-09 01:21:31 +00:00
|
|
|
]
|
|
|
|
);
|
2015-05-28 22:47:06 +00:00
|
|
|
|
2017-01-09 02:22:56 +00:00
|
|
|
throw $e;
|
|
|
|
}
|
2015-05-28 22:47:06 +00:00
|
|
|
}
|
2016-12-12 17:57:03 +00:00
|
|
|
|
2017-01-09 02:22:56 +00:00
|
|
|
return $usersForUpdate;
|
2015-05-28 22:47:06 +00:00
|
|
|
}
|
|
|
|
}
|