2022-03-31 11:28:26 +09:00
< ? php
declare ( strict_types = 1 );
namespace Plugin\Pinboard\Controller ;
use App\Core\Cache ;
use App\Core\Controller ;
use App\Core\DB ;
use App\Core\Form ;
use function App\Core\I18n\_m ;
use App\Core\Router ;
use App\Util\Common ;
use App\Util\Exception\BugFoundException ;
use App\Util\Exception\ClientException ;
use Plugin\Pinboard as P ;
use Plugin\Pinboard\Entity\Token ;
use Symfony\Component\Form\Extension\Core\Type\SubmitType ;
use Symfony\Component\Form\Extension\Core\Type\TextType ;
use Symfony\Component\Form\SubmitButton ;
use Symfony\Component\HttpFoundation\Request ;
class Settings extends Controller
{
public static function setup ()
{
$user = Common :: ensureLoggedIn ();
2022-04-01 06:06:37 +09:00
$token = Token :: get ( id : null , token : null , user : $user );
2022-03-31 11:28:26 +09:00
$enabled = ( $token ? -> getEnabled () ? ? false );
$form = Form :: create ([
[ 'token' , TextType :: class , [ 'label' => _m ( 'The token used to authenticate you via the Pinboard-compatible API' ), 'data' => $token ? -> getUserTokenString (), 'disabled' => true ]],
[ 'enable' , SubmitType :: class , [ 'label' => $enabled ? _m ( 'Disable' ) : _m ( 'Enable' ), 'attr' => [ 'alt' => $enabled ? _m ( 'Disable the use of the Pinboard-compatible API' ) : _m ( 'Enable the use of the Pinboard-compatible API' )]]],
[ 'regenerate' , SubmitType :: class , [ 'label' => _m ( 'Regenerate Token' ), 'disabled' => ! $enabled ]],
], form_options : [ 'action' => Router :: url ( P\Pinboard :: controller_route )]);
return [
'_template' => 'pinboard/settings.html.twig' ,
'form_view' => $form -> createView (),
'form' => $form ,
'token' => $token ,
'was_enabled' => $enabled ,
];
}
public static function onPost ( Request $request )
{
$user = Common :: ensureLoggedIn ();
$params = self :: setup ();
$form = $params [ 'form' ];
$form -> handleRequest ( $request );
if ( $form -> isSubmitted () && $form -> isValid ()) {
/** @var SubmitButton $enable_button */
$enable_button = $form -> get ( 'enable' );
/** @var SubmitButton $regenerate_button */
$regenerate_button = $form -> get ( 'regenerate' );
2022-10-20 06:38:49 +09:00
/** @var Token $token */
$token = $params [ 'token' ];
2022-03-31 11:28:26 +09:00
if ( $enable_button -> isClicked ()) {
if ( $params [ 'was_enabled' ]) {
if ( \is_null ( $params [ 'token' ])) {
throw new BugFoundException ( 'Pinboard API can not be enabled if no token is present' );
} else {
2022-10-20 06:38:49 +09:00
DB :: refresh ( $token );
2022-03-31 11:28:26 +09:00
$token -> setEnabled ( false );
}
} else {
if ( \is_null ( $params [ 'token' ])) {
DB :: persist ( $token = Token :: create ([ 'actor_id' => $user -> getId (), 'token' => Token :: generateTokenString (), 'enabled' => true ]));
} else {
2022-10-20 06:38:49 +09:00
DB :: refresh ( $params [ 'token' ]);
2022-03-31 11:28:26 +09:00
$token -> setEnabled ( true );
}
}
} elseif ( $regenerate_button -> isClicked ()) {
if ( \is_null ( $params [ 'token' ])) {
throw new ClientException ( _m ( 'Can not regenerate token when no token exists. Enable Pinboard first' ));
} else {
2022-10-20 06:38:49 +09:00
DB :: refresh ( $params [ 'token' ]);
2022-03-31 11:28:26 +09:00
$token -> setToken ( Token :: generateTokenString ());
}
} else {
throw new ClientException ( _m ( 'Invalid form submission' ));
}
2022-04-01 06:06:37 +09:00
Cache :: set ( Token :: cacheKeys ( $user -> getId ())[ 'user-token' ], $token );
2022-03-31 11:28:26 +09:00
DB :: flush ();
return Form :: forceRedirect ( $form , $request );
}
throw new ClientException ( _m ( 'Do not GET this page' ));
}
}