From 0623ad8a8ebc6b8a2d449eca6d7f85210af858ee Mon Sep 17 00:00:00 2001 From: Jonas Raoni Soares da Silva Date: Sat, 15 Apr 2023 21:36:00 +0300 Subject: [PATCH] #229 Formatting --- OrcidProfileHandler.php | 42 ++-- classes/OrcidValidator.php | 52 ++--- classes/form/OrcidProfileSettingsForm.php | 227 +++++++++++----------- classes/form/OrcidProfileStatusForm.php | 138 ++++++------- 4 files changed, 211 insertions(+), 248 deletions(-) diff --git a/OrcidProfileHandler.php b/OrcidProfileHandler.php index a3c4f561..65de972d 100644 --- a/OrcidProfileHandler.php +++ b/OrcidProfileHandler.php @@ -16,7 +16,6 @@ namespace APP\plugins\generic\orcidProfile; - use APP\core\Application; use APP\core\Request; use APP\facades\Repo; @@ -25,7 +24,6 @@ use Carbon\Carbon; use Exception; use PKP\core\Core; -use PKP\plugins\Hook; use PKP\plugins\PluginRegistry; use PKP\security\authorization\PKPSiteAccessPolicy; use PKP\security\authorization\UserRequiredPolicy; @@ -39,8 +37,6 @@ class OrcidProfileHandler extends Handler private bool $isSandBox; private OrcidProfilePlugin $plugin; - - public function __construct() { $request = Application::get()->getRequest(); @@ -88,7 +84,6 @@ public function authorize($request, &$args, $roleAssignments) public function orcidAuthorize($args, $request) { $context = $request->getContext(); - $op = $request->getRequestedOp(); $contextId = ($context == null) ? \PKP\core\PKPApplication::CONTEXT_ID_NONE : $context->getId(); $httpClient = Application::get()->getHttpClient(); @@ -156,17 +151,17 @@ public function orcidAuthorize($args, $request) // Suppress errors for nonexistent array indexes echo ' - - '; + + '; break; case 'profile': @@ -177,11 +172,11 @@ public function orcidAuthorize($args, $request) // Reload the public profile tab (incl. form) echo ' - - '; + + '; break; default: throw new Exception('Invalid target'); @@ -333,12 +328,9 @@ public function orcidVerify($args, $request) 'verifySuccess' => true, 'orcidIcon' => $this->plugin->getIcon() ]); - - } catch (\GuzzleHttp\Exception\ClientException $exception) { $this->plugin->logInfo("Publication fail:".$exception->getMessage()); $templateMgr->assign('orcidAPIError',$exception->getMessage()); - } $templateMgr->assign('authFailure', true); $templateMgr->display($templatePath); @@ -376,6 +368,4 @@ public function about($args, $request) $templateMgr->assign('isMemberApi', $this->plugin->isMemberApiEnabled($contextId)); $templateMgr->display($this->plugin->getTemplateResource('orcidAbout.tpl')); } - - } diff --git a/classes/OrcidValidator.php b/classes/OrcidValidator.php index d14b61ea..2963c966 100644 --- a/classes/OrcidValidator.php +++ b/classes/OrcidValidator.php @@ -16,37 +16,27 @@ use APP\plugins\generic\orcidProfile\OrcidProfilePlugin; class OrcidValidator { - public $plugin; - /** - * OrcidValidator constructor. - * @param OrcidProfilePlugin $plugin - */ - function __construct(&$plugin) { - $this->plugin =& $plugin; - } + /** + * OrcidValidator constructor. + */ + public function __construct(public OrcidProfilePlugin $plugin) { + } - /** - * @param string $str - * @return bool - */ - public function validateClientId($str) { - $valid = false; - if (preg_match('/^APP-[\da-zA-Z]{16}|(\d{4}-){3,}\d{3}[\dX]/', $str) == 1) { - $valid = true; - } - return $valid; - } - - /** - * @param string $str - * @return bool - */ - public function validateClientSecret($str) { - $valid = false; - if (preg_match('/^(\d|-|[a-f]){36,64}/', $str) == 1) { - $valid = true; - } - return $valid; - } + /** + * @param string $str + * @return bool + */ + public function validateClientId(?string $str): bool + { + return (bool) preg_match('/^APP-[\da-zA-Z]{16}|(\d{4}-){3,}\d{3}[\dX]/', (string) $str); + } + /** + * @param string $str + * @return bool + */ + public function validateClientSecret(?string $str): bool + { + return (bool) preg_match('/^(\d|-|[a-f]){36,64}/', (string) $str); + } } diff --git a/classes/form/OrcidProfileSettingsForm.php b/classes/form/OrcidProfileSettingsForm.php index 127fa4c6..38ef162d 100644 --- a/classes/form/OrcidProfileSettingsForm.php +++ b/classes/form/OrcidProfileSettingsForm.php @@ -21,123 +21,118 @@ use APP\plugins\generic\orcidProfile\classes\OrcidValidator; use APP\plugins\generic\orcidProfile\OrcidProfilePlugin; use APP\template\TemplateManager; -use PKP\plugins\Plugin; class OrcidProfileSettingsForm extends Form { - const CONFIG_VARS = array( - 'orcidProfileAPIPath' => 'string', - 'orcidClientId' => 'string', - 'orcidClientSecret' => 'string', - 'sendMailToAuthorsOnPublication' => 'bool', - 'logLevel' => 'string', - 'isSandBox' => 'bool', - 'country' => 'string', - 'city' => 'string' - - ); - /** @var $contextId int */ - var $contextId; - - /** @var $plugin object */ - var $plugin; - - var $validator; - - /** - * Constructor - * @param OrcidProfilePlugin $plugin - * @param int $contextId - */ - function __construct($plugin, $contextId) { - $this->contextId = $contextId; - $this->plugin = $plugin; - $orcidValidator = new OrcidValidator($plugin); - $this->validator = $orcidValidator; - parent::__construct($plugin->getTemplateResource('settingsForm.tpl')); - $this->addCheck(new \PKP\form\validation\FormValidatorPost($this)); - $this->addCheck(new \PKP\form\validation\FormValidatorCSRF($this)); - - if (!$this->plugin->isGloballyConfigured()) { - $this->addCheck(new \PKP\form\validation\FormValidator($this, 'orcidProfileAPIPath', 'required', 'plugins.generic.orcidProfile.manager.settings.orcidAPIPathRequired')); - $this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'orcidClientId', 'required', 'plugins.generic.orcidProfile.manager.settings.orcidClientId.error', function ($clientId) { - return $this->validator->validateClientId($clientId); - })); - $this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'orcidClientSecret', 'required', 'plugins.generic.orcidProfile.manager.settings.orcidClientSecret.error', function ($clientSecret) { - return $this->validator->validateClientSecret($clientSecret); - })); - } - - } - - /** - * Initialize form data. - */ - function initData() { - $contextId = $this->contextId; - $plugin =& $this->plugin; - $this->_data = array(); - foreach (self::CONFIG_VARS as $configVar => $type) { - $this->_data[$configVar] = $plugin->getSetting($contextId, $configVar); - } - } - - /** - * Assign form data to user-submitted data. - */ - function readInputData() { - $this->readUserVars(array_keys(self::CONFIG_VARS)); - } - - /** - * Fetch the form. - * @copydoc Form::fetch() - */ - function fetch($request, $template = null, $display = false) { - $templateMgr = TemplateManager::getManager($request); - $templateMgr->assign('globallyConfigured', $this->plugin->isGloballyConfigured()); - $templateMgr->assign('pluginName', $this->plugin->getName()); - $templateMgr->assign('applicationName', Application::get()->getName()); - return parent::fetch($request, $template, $display); - } - - /** - * @copydoc Form::execute() - */ - function execute(...$functionArgs) { - $plugin =& $this->plugin; - $contextId = $this->contextId; - foreach (self::CONFIG_VARS as $configVar => $type) { - if ($configVar === 'orcidProfileAPIPath') { - $plugin->updateSetting($contextId, $configVar, trim($this->getData($configVar), "\"\';"), $type); - } else { - $plugin->updateSetting($contextId, $configVar, $this->getData($configVar), $type); - } - } - if (strpos($this->getData("orcidProfileAPIPath"), "sandbox.orcid.org") == true) { - $plugin->updateSetting($contextId, "isSandBox", true, "bool"); - } - - parent::execute(...$functionArgs); - } - - public function _checkPrerequisites() { - $messages = array(); - - $clientId = $this->getData('orcidClientId'); - if (!$this->validator->validateClientId($clientId)) { - $messages[] = __('plugins.generic.orcidProfile.manager.settings.orcidClientId.error'); - } - $clientSecret = $this->getData('orcidClientSecret'); - if (!$this->validator->validateClientSecret($clientSecret)) { - $messages[] = __('plugins.generic.orcidProfile.manager.settings.orcidClientSecret.error'); - } - if (strlen($clientId) == 0 or strlen($clientSecret) == 0) { - $this->plugin->setEnabled(false); - } - return $messages; - } - - + public const CONFIG_VARS = [ + 'orcidProfileAPIPath' => 'string', + 'orcidClientId' => 'string', + 'orcidClientSecret' => 'string', + 'sendMailToAuthorsOnPublication' => 'bool', + 'logLevel' => 'string', + 'isSandBox' => 'bool', + 'country' => 'string', + 'city' => 'string' + ]; + + /** @var $contextId int */ + public $contextId; + + /** @var $plugin object */ + public $plugin; + + public $validator; + + /** + * Constructor + * @param OrcidProfilePlugin $plugin + * @param int $contextId + */ + public function __construct($plugin, $contextId) { + $this->contextId = $contextId; + $this->plugin = $plugin; + $orcidValidator = new OrcidValidator($plugin); + $this->validator = $orcidValidator; + parent::__construct($plugin->getTemplateResource('settingsForm.tpl')); + $this->addCheck(new \PKP\form\validation\FormValidatorPost($this)); + $this->addCheck(new \PKP\form\validation\FormValidatorCSRF($this)); + + if (!$this->plugin->isGloballyConfigured()) { + $this->addCheck(new \PKP\form\validation\FormValidator($this, 'orcidProfileAPIPath', 'required', 'plugins.generic.orcidProfile.manager.settings.orcidAPIPathRequired')); + $this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'orcidClientId', 'required', 'plugins.generic.orcidProfile.manager.settings.orcidClientId.error', function ($clientId) { + return $this->validator->validateClientId($clientId); + })); + $this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'orcidClientSecret', 'required', 'plugins.generic.orcidProfile.manager.settings.orcidClientSecret.error', function ($clientSecret) { + return $this->validator->validateClientSecret($clientSecret); + })); + } + } + + /** + * Initialize form data. + */ + public function initData() { + $contextId = $this->contextId; + $plugin =& $this->plugin; + $this->_data = []; + foreach (self::CONFIG_VARS as $configVar => $type) { + $this->_data[$configVar] = $plugin->getSetting($contextId, $configVar); + } + } + + /** + * Assign form data to user-submitted data. + */ + public function readInputData() { + $this->readUserVars(array_keys(self::CONFIG_VARS)); + } + + /** + * Fetch the form. + * @copydoc Form::fetch() + */ + public function fetch($request, $template = null, $display = false) { + $templateMgr = TemplateManager::getManager($request); + $templateMgr->assign('globallyConfigured', $this->plugin->isGloballyConfigured()); + $templateMgr->assign('pluginName', $this->plugin->getName()); + $templateMgr->assign('applicationName', Application::get()->getName()); + return parent::fetch($request, $template, $display); + } + + /** + * @copydoc Form::execute() + */ + public function execute(...$functionArgs) { + $plugin =& $this->plugin; + $contextId = $this->contextId; + foreach (self::CONFIG_VARS as $configVar => $type) { + if ($configVar === 'orcidProfileAPIPath') { + $plugin->updateSetting($contextId, $configVar, trim($this->getData($configVar), "\"\';"), $type); + } else { + $plugin->updateSetting($contextId, $configVar, $this->getData($configVar), $type); + } + } + if (strpos($this->getData("orcidProfileAPIPath"), "sandbox.orcid.org") == true) { + $plugin->updateSetting($contextId, "isSandBox", true, "bool"); + } + + parent::execute(...$functionArgs); + } + + public function _checkPrerequisites() { + $messages = []; + + $clientId = $this->getData('orcidClientId'); + if (!$this->validator->validateClientId($clientId)) { + $messages[] = __('plugins.generic.orcidProfile.manager.settings.orcidClientId.error'); + } + $clientSecret = $this->getData('orcidClientSecret'); + if (!$this->validator->validateClientSecret($clientSecret)) { + $messages[] = __('plugins.generic.orcidProfile.manager.settings.orcidClientSecret.error'); + } + if (strlen($clientId) == 0 or strlen($clientSecret) == 0) { + $this->plugin->setEnabled(false); + } + return $messages; + } } - diff --git a/classes/form/OrcidProfileStatusForm.php b/classes/form/OrcidProfileStatusForm.php index ab208d68..9e3036f8 100644 --- a/classes/form/OrcidProfileStatusForm.php +++ b/classes/form/OrcidProfileStatusForm.php @@ -22,81 +22,69 @@ use APP\plugins\generic\orcidProfile\OrcidProfilePlugin; use APP\template\TemplateManager; - class OrcidProfileStatusForm extends Form { - const CONFIG_VARS = array( - 'orcidProfileAPIPath' => 'string', - 'orcidClientId' => 'string', - 'orcidClientSecret' => 'string', - 'sendMailToAuthorsOnPublication' => 'bool', - 'logLevel' => 'string', - 'isSandBox' => 'bool' - ); - - /** @var $contextId int */ - var $contextId; - - /** @var $plugin object */ - var $plugin; - - /** @var OrcidValidator */ - var $validator; - - /** - * Constructor - * @param OrcidProfilePlugin $plugin - * @param int $contextId - */ - function __construct($plugin, $contextId) { - $this->contextId = $contextId; - $this->plugin = $plugin; - $orcidValidator = new OrcidValidator($plugin); - $this->validator = $orcidValidator; - parent::__construct($plugin->getTemplateResource('statusForm.tpl')); - - if (!$this->plugin->isGloballyConfigured()) { - - } - - } - - /** - * Initialize form data. - */ - function initData() { - $contextId = $this->contextId; - $plugin =& $this->plugin; - $this->_data = array(); - foreach (self::CONFIG_VARS as $configVar => $type) { - $this->_data[$configVar] = $plugin->getSetting($contextId, $configVar); - } - } - - - /** - * Fetch the form. - * @copydoc Form::fetch() - */ - function fetch($request, $template = null, $display = false) { - $contextId = $request->getContext()->getId(); - $clientId = $this->plugin->getSetting($contextId, 'orcidClientId'); - $clientSecret = $this->plugin->getSetting($contextId, 'orcidClientSecret'); - - $templateMgr = TemplateManager::getManager($request); - $aboutUrl = $request->getDispatcher()->url($request, Application::ROUTE_PAGE, null, 'orcidapi', 'about', null); - $templateMgr->assign(array( - 'globallyConfigured' => $this->plugin->isGloballyConfigured(), - 'orcidAboutUrl' => $aboutUrl, - 'pluginEnabled' => $this->plugin->getEnabled($contextId), - 'clientIdValid' => $this->validator->validateClientId($clientId), - 'clientSecretValid' => $this->validator->validateClientSecret($clientSecret), - - - )); - return parent::fetch($request, $template, $display); - } - - + public const CONFIG_VARS = [ + 'orcidProfileAPIPath' => 'string', + 'orcidClientId' => 'string', + 'orcidClientSecret' => 'string', + 'sendMailToAuthorsOnPublication' => 'bool', + 'logLevel' => 'string', + 'isSandBox' => 'bool' + ]; + + /** @var $contextId int */ + public $contextId; + + /** @var $plugin object */ + public $plugin; + + /** @var OrcidValidator */ + public $validator; + + /** + * Constructor + * @param OrcidProfilePlugin $plugin + * @param int $contextId + */ + public function __construct($plugin, $contextId) { + $this->contextId = $contextId; + $this->plugin = $plugin; + $orcidValidator = new OrcidValidator($plugin); + $this->validator = $orcidValidator; + parent::__construct($plugin->getTemplateResource('statusForm.tpl')); + } + + /** + * Initialize form data. + */ + public function initData() { + $contextId = $this->contextId; + $plugin =& $this->plugin; + $this->_data = []; + foreach (self::CONFIG_VARS as $configVar => $type) { + $this->_data[$configVar] = $plugin->getSetting($contextId, $configVar); + } + } + + /** + * Fetch the form. + * @copydoc Form::fetch() + */ + public function fetch($request, $template = null, $display = false) { + $contextId = $request->getContext()->getId(); + $clientId = $this->plugin->getSetting($contextId, 'orcidClientId'); + $clientSecret = $this->plugin->getSetting($contextId, 'orcidClientSecret'); + + $templateMgr = TemplateManager::getManager($request); + $aboutUrl = $request->getDispatcher()->url($request, Application::ROUTE_PAGE, null, 'orcidapi', 'about', null); + $templateMgr->assign([ + 'globallyConfigured' => $this->plugin->isGloballyConfigured(), + 'orcidAboutUrl' => $aboutUrl, + 'pluginEnabled' => $this->plugin->getEnabled($contextId), + 'clientIdValid' => $this->validator->validateClientId($clientId), + 'clientSecretValid' => $this->validator->validateClientSecret($clientSecret), + ]); + return parent::fetch($request, $template, $display); + } } -