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' )
-> 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
*/
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' );
$log -> info ( 'UpdateSubscriptionsCommand started.' );
2015-05-28 22:47:06 +00:00
/** @var UserApi $api */
$api = $this -> getContainer () -> get ( 'skobkin_point_tools.api_user' );
/** @var SubscriptionsManager $subscriptionsManager */
$subscriptionsManager = $this -> getContainer () -> get ( 'skobkin_point_tools.subscriptions_manager' );
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 ()]);
return false ;
}
$serviceUser = $this -> getContainer () -> get ( 'doctrine.orm.entity_manager' ) -> getRepository ( 'SkobkinPointToolsBundle:User' ) -> find ( $serviceUserId );
2015-05-28 22:47:06 +00:00
if ( ! $serviceUser ) {
2015-05-30 22:50:35 +00:00
$log -> info ( 'Service user not found' );
2015-05-28 22:47:06 +00:00
// @todo Retrieving user
2015-05-30 22:50:35 +00:00
2015-05-30 05:29:40 +00:00
return false ;
2015-05-28 22:47:06 +00:00
}
2015-05-30 05:29:40 +00:00
if ( $output -> isVerbose ()) {
$output -> writeln ( 'Getting service subscribers' );
}
try {
2015-06-02 01:59:07 +00:00
$serviceSubscribers = $api -> getUserSubscribersById ( $serviceUserId );
2015-05-30 05:29:40 +00:00
} catch ( \Exception $e ) {
$output -> writeln ( 'Error while getting service subscribers' );
2015-06-02 02:32:45 +00:00
$log -> error ( 'Error while getting 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
2015-06-02 02:39:09 +00:00
$serviceSubscribers = [];
foreach ( $serviceUser -> getSubscribers () as $subscription ) {
$serviceSubscribers [] = $subscription -> getSubscriber ();
}
$output -> writeln ( 'Fallback to local list' );
$log -> error ( 'Fallback to local list' );
if ( ! count ( $serviceSubscribers )) {
$log -> info ( 'No local subscribers. Finishing.' );
return false ;
}
2015-05-30 05:29:40 +00:00
}
if ( $output -> isVerbose ()) {
$output -> writeln ( 'Updating service subscribers' );
}
2015-05-28 22:47:06 +00:00
// Updating service subscribers
2015-05-30 22:50:35 +00:00
try {
$subscriptionsManager -> updateUserSubscribers ( $serviceUser , $serviceSubscribers );
} catch ( \Exception $e ) {
2015-06-02 02:32:45 +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
return false ;
}
2015-05-28 22:47:06 +00:00
2015-05-30 05:29:40 +00:00
if ( $output -> isVerbose ()) {
$output -> writeln ( 'Processing service subscribers' );
}
2015-05-28 22:47:06 +00:00
// Updating service users subscribers
foreach ( $serviceSubscribers as $user ) {
2015-05-30 05:29:40 +00:00
$output -> writeln ( ' Processing @' . $user -> getLogin ());
2015-05-30 22:50:35 +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 ) {
$output -> writeln ( ' Error while getting subscribers. Skipping.' );
2015-06-02 02:32:45 +00:00
$log -> error ( 'Error while getting 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-30 05:29:40 +00:00
continue ;
}
if ( $output -> isVerbose ()) {
2015-05-30 22:50:35 +00:00
$output -> writeln ( ' Updating user subscribers' );
2015-05-30 05:29:40 +00:00
}
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 ) {
2015-06-02 02:32:45 +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
usleep ( 200000 );
2015-05-28 22:47:06 +00:00
}
}
}