phpmyadmin/libraries/classes/Plugins/AuthenticationPluginFactory.php
Maurício Meneghini Fauth a2a3f6d26d
Create a factory for auth plugin creation
Extracts the factory method from the Plugins class into a new class.

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
2022-11-16 19:32:10 -03:00

42 lines
1.0 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpMyAdmin\Plugins;
use PhpMyAdmin\Exceptions\AuthenticationPluginException;
use function __;
use function class_exists;
use function is_subclass_of;
use function strtolower;
use function ucfirst;
class AuthenticationPluginFactory
{
/** @var AuthenticationPlugin|null */
private $plugin = null;
/**
* @throws AuthenticationPluginException
*/
public function create(): AuthenticationPlugin
{
if ($this->plugin instanceof AuthenticationPlugin) {
return $this->plugin;
}
$authType = $GLOBALS['cfg']['Server']['auth_type'];
$class = 'PhpMyAdmin\\Plugins\\Auth\\Authentication' . ucfirst(strtolower($authType));
if (! class_exists($class) || ! is_subclass_of($class, AuthenticationPlugin::class)) {
throw new AuthenticationPluginException(
__('Invalid authentication method set in configuration:') . ' ' . $authType
);
}
$this->plugin = new $class();
return $this->plugin;
}
}