Skip to content

Commit

Permalink
Use Closure instead of callable.
Browse files Browse the repository at this point in the history
  • Loading branch information
demiankatz committed Nov 27, 2024
1 parent 19cc7f5 commit 5833855
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 52 deletions.
32 changes: 8 additions & 24 deletions module/VuFind/src/VuFind/Auth/ILSAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@

namespace VuFind\Auth;

use Closure;
use Laminas\Config\Config;
use VuFind\Crypt\BlockCipher;
use VuFind\Db\Entity\UserEntityInterface;
use VuFind\Db\Service\DbServiceAwareInterface;
use VuFind\Db\Service\DbServiceAwareTrait;
Expand All @@ -52,20 +52,6 @@ class ILSAuthenticator implements DbServiceAwareInterface
{
use DbServiceAwareTrait;

/**
* Callback for retrieving the authentication manager
*
* @var callable
*/
protected $authManagerCallback;

/**
* BlockCipher object factory (takes algorithm as argument)
*
* @var callable
*/
protected $cipherFactory;

/**
* Authentication manager
*
Expand Down Expand Up @@ -97,21 +83,19 @@ class ILSAuthenticator implements DbServiceAwareInterface
/**
* Constructor
*
* @param callable $authCB Auth manager callback
* @param callable $cipherFactory BlockCipher object factory (takes algorithm as argument)
* @param ILSConnection $catalog ILS connection
* @param ?EmailAuthenticator $emailAuthenticator Email authenticator
* @param ?Config $config Configuration from config.ini
* @param Closure $authManagerCallback Auth manager callback
* @param Closure $cipherFactory BlockCipher object factory (takes algorithm as argument)
* @param ILSConnection $catalog ILS connection
* @param ?EmailAuthenticator $emailAuthenticator Email authenticator
* @param ?Config $config Configuration from config.ini
*/
public function __construct(
callable $authCB,
callable $cipherFactory,
protected Closure $authManagerCallback,
protected Closure $cipherFactory,
protected ILSConnection $catalog,
protected ?EmailAuthenticator $emailAuthenticator = null,
protected ?Config $config = null
) {
$this->authManagerCallback = $authCB;
$this->cipherFactory = $cipherFactory;
}

/**
Expand Down
17 changes: 11 additions & 6 deletions module/VuFind/src/VuFind/Auth/ILSAuthenticatorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

namespace VuFind\Auth;

use Closure;
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
use Laminas\ServiceManager\Factory\FactoryInterface;
Expand Down Expand Up @@ -71,13 +72,17 @@ public function __invoke(
}
$service = new $requestedName(
// Use a callback to retrieve authentication manager to break a circular reference:
function () use ($container) {
return $container->get(\VuFind\Auth\Manager::class);
},
Closure::fromCallable(
function () use ($container) {
return $container->get(\VuFind\Auth\Manager::class);
}
),
// Use a callback to build BlockCipher objects:
function (string $algo) use ($container) {
return $container->get(BlockCipher::class)->setAlgorithm($algo);
},
Closure::fromCallable(
function (string $algo) use ($container) {
return $container->get(BlockCipher::class)->setAlgorithm($algo);
}
),
$container->get(\VuFind\ILS\Connection::class),
$container->get(\VuFind\Auth\EmailAuthenticator::class),
$container->get(\VuFind\Config\PluginManager::class)->get('config')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

namespace VuFindTest\Auth;

use Closure;
use PHPUnit\Framework\MockObject\MockObject;
use VuFind\Auth\EmailAuthenticator;
use VuFind\Auth\ILSAuthenticator;
Expand Down Expand Up @@ -262,12 +263,16 @@ protected function getAuthenticator(
$connection = $this->getMockConnection();
}
return new ILSAuthenticator(
function () use ($manager) {
return $manager;
},
function (string $algo) {
return (new BlockCipher())->setAlgorithm($algo);
},
Closure::fromCallable(
function () use ($manager) {
return $manager;
}
),
Closure::fromCallable(
function (string $algo) {
return (new BlockCipher())->setAlgorithm($algo);
}
),
$connection,
$emailAuth ?? $this->createMock(EmailAuthenticator::class),
new \Laminas\Config\Config($config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

namespace VuFindConsole\Command\Util;

use Closure;
use InvalidArgumentException;
use Laminas\Config\Config;
use Symfony\Component\Console\Attribute\AsCommand;
Expand Down Expand Up @@ -63,20 +64,13 @@
)]
class SwitchDbHashCommand extends Command
{
/**
* Callback to generate a BlockCipher object (must take two arguments: algorithm and key)
*
* @var callable
*/
protected $cipherFactory;

/**
* Constructor
*
* @param Config $config VuFind configuration
* @param UserServiceInterface $userService User database service
* @param UserCardServiceInterface $userCardService UserCard database service
* @param callable $cipherFactory Callback to generate a BlockCipher object (must
* @param Closure $cipherFactory Callback to generate a BlockCipher object (must
* take two arguments: algorithm and key)
* @param ?string $name The name of the command; passing null means
* it must be set in configure()
Expand All @@ -86,11 +80,10 @@ public function __construct(
protected Config $config,
protected UserServiceInterface $userService,
protected UserCardServiceInterface $userCardService,
callable $cipherFactory,
protected Closure $cipherFactory,
?string $name = null,
protected ?PathResolver $pathResolver = null
) {
$this->cipherFactory = $cipherFactory;
parent::__construct($name);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

namespace VuFindConsole\Command\Util;

use Closure;
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
use Laminas\ServiceManager\Factory\FactoryInterface;
Expand Down Expand Up @@ -74,9 +75,11 @@ public function __invoke(
$config,
$serviceManager->get(UserServiceInterface::class),
$serviceManager->get(UserCardServiceInterface::class),
function ($algo, $key) use ($container) {
return $container->get(BlockCipher::class)->setAlgorithm($algo)->setKey($key);
},
Closure::fromCallable(
function ($algo, $key) use ($container) {
return $container->get(BlockCipher::class)->setAlgorithm($algo)->setKey($key);
}
),
null,
$container->get(\VuFind\Config\PathResolver::class),
...($options ?? [])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

namespace VuFindTest\Command\Util;

use Closure;
use Laminas\Config\Config;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\Console\Tester\CommandTester;
Expand Down Expand Up @@ -108,9 +109,11 @@ protected function getMockCommand(
new Config($config),
$userService ?? $this->getMockUserService(),
$cardService ?? $this->getMockCardService(),
function ($algo, $key) {
return (new BlockCipher())->setAlgorithm($algo)->setKey($key);
},
Closure::fromCallable(
function ($algo, $key) {
return (new BlockCipher())->setAlgorithm($algo)->setKey($key);
}
),
]
)->onlyMethods(['getConfigWriter'])
->getMock();
Expand Down

0 comments on commit 5833855

Please sign in to comment.