Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 40df2d3

Browse files
committed
Provide type-safety for user factories
Wraps `$userFactory` constructor arguments in a callable that provides a return type hint, as well as type hints and optional arguments for all parameters.
1 parent 27e0485 commit 40df2d3

File tree

5 files changed

+53
-9
lines changed

5 files changed

+53
-9
lines changed

src/DefaultUser.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,29 @@
99

1010
namespace Zend\Expressive\Authentication;
1111

12+
/**
13+
* Default implementation of UserInterface.
14+
*
15+
* This implementation is modeled as immutable, to prevent propagation of
16+
* user state changes.
17+
*
18+
* We recommend that any details injected are serializable.
19+
*/
1220
final class DefaultUser implements UserInterface
1321
{
22+
/**
23+
* @var string
24+
*/
1425
private $identity;
1526

27+
/**
28+
* @var string[]
29+
*/
1630
private $roles;
1731

32+
/**
33+
* @var array
34+
*/
1835
private $details;
1936

2037
public function __construct(string $identity, array $roles = [], array $details = [])
@@ -39,6 +56,11 @@ public function getDetails() : array
3956
return $this->details;
4057
}
4158

59+
/**
60+
* @param mixed $default Default value to return if no detail matching
61+
* $name is discovered.
62+
* @return mixed
63+
*/
4264
public function getDetail(string $name, $default = null)
4365
{
4466
return $this->details[$name] ?? $default;

src/DefaultUserFactory.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* @see https://github.com/zendframework/zend-expressive-authentication for the canonical source repository
4-
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (https://www.zend.com)
4+
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
55
* @license https://github.com/zendframework/zend-expressive-authentication/blob/master/LICENSE.md New BSD License
66
*/
77

@@ -11,11 +11,16 @@
1111

1212
use Psr\Container\ContainerInterface;
1313

14+
/**
15+
* Produces a callable factory capable of itself producing a UserInterface
16+
* instance; this approach is used to allow substituting alternative user
17+
* implementations without requiring extensions to existing repositories.
18+
*/
1419
class DefaultUserFactory
1520
{
1621
public function __invoke(ContainerInterface $container) : callable
1722
{
18-
return function (string $identity, array $roles = [], array $details = []) {
23+
return function (string $identity, array $roles = [], array $details = []) : UserInterface {
1924
return new DefaultUser($identity, $roles, $details);
2025
};
2126
}

src/UserInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* @see https://github.com/zendframework/zend-expressive-authentication for the canonical source repository
4-
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (https://www.zend.com)
4+
* @copyright Copyright (c) 2017-2018 Zend Technologies USA Inc. (https://www.zend.com)
55
* @license https://github.com/zendframework/zend-expressive-authentication/blob/master/LICENSE.md New BSD License
66
*/
77

src/UserRepository/Htpasswd.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* @see https://github.com/zendframework/zend-expressive-authentication for the canonical source repository
4-
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (https://www.zend.com)
4+
* @copyright Copyright (c) 2017-2018 Zend Technologies USA Inc. (https://www.zend.com)
55
* @license https://github.com/zendframework/zend-expressive-authentication/blob/master/LICENSE.md New BSD License
66
*/
77

@@ -42,7 +42,15 @@ public function __construct(string $filename, callable $userFactory)
4242
));
4343
}
4444
$this->filename = $filename;
45-
$this->userFactory = $userFactory;
45+
46+
// Provide type safety for the composed user factory.
47+
$this->userFactory = function (
48+
string $identity,
49+
array $roles = [],
50+
array $details = []
51+
) use ($userFactory) : UserInterface {
52+
return $userFactory($identity, $roles, $details);
53+
};
4654
}
4755

4856
/**

src/UserRepository/PdoDatabase.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* @see https://github.com/zendframework/zend-expressive-authentication for the canonical source repository
4-
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (https://www.zend.com)
4+
* @copyright Copyright (c) 2017-2018 Zend Technologies USA Inc. (https://www.zend.com)
55
* @license https://github.com/zendframework/zend-expressive-authentication/blob/master/LICENSE.md New BSD License
66
*/
77

@@ -17,7 +17,8 @@
1717

1818
/**
1919
* Adapter for PDO database
20-
* It supports only bcrypt hash password for security reason
20+
*
21+
* It supports only bcrypt password hashing for security reasons.
2122
*/
2223
class PdoDatabase implements UserRepositoryInterface
2324
{
@@ -43,7 +44,15 @@ public function __construct(
4344
) {
4445
$this->pdo = $pdo;
4546
$this->config = $config;
46-
$this->userFactory = $userFactory;
47+
48+
// Provide type safety for the composed user factory.
49+
$this->userFactory = function (
50+
string $identity,
51+
array $roles = [],
52+
array $details = []
53+
) use ($userFactory) : UserInterface {
54+
return $userFactory($identity, $roles, $details);
55+
};
4756
}
4857

4958
/**
@@ -97,7 +106,7 @@ protected function getUserRoles(string $identity) : array
97106

98107
if (false === strpos($this->config['sql_get_roles'], ':identity')) {
99108
throw new Exception\InvalidConfigException(
100-
'The sql_get_roles configuration setting must include a :identity parameter'
109+
'The sql_get_roles configuration setting must include an :identity parameter'
101110
);
102111
}
103112

0 commit comments

Comments
 (0)