1 | <?php |
||
2 | |||
3 | /* |
||
4 | * @copyright 2018 Hilmi Erdem KEREN |
||
5 | * @license MIT |
||
6 | */ |
||
7 | |||
8 | namespace Erdemkeren\Otp; |
||
9 | |||
10 | /** |
||
11 | * Class PasswordGeneratorManager. |
||
12 | */ |
||
13 | final class PasswordGeneratorManager implements PasswordGeneratorManagerInterface |
||
14 | { |
||
15 | /** |
||
16 | * The password generator registry. |
||
17 | * |
||
18 | * @var array |
||
19 | */ |
||
20 | private static $generators = []; |
||
21 | |||
22 | /** |
||
23 | * Registers the given password generator with the given name. |
||
24 | * |
||
25 | * @param string $name |
||
26 | * @param callable|PasswordGeneratorInterface|string $generator |
||
27 | */ |
||
28 | 5 | public function register(string $name, $generator): void |
|
29 | { |
||
30 | 5 | if (\is_string($generator)) { |
|
31 | 3 | $generator = $this->createGeneratorFromString($generator); |
|
32 | } |
||
33 | |||
34 | 3 | if (! \is_callable($generator) && ! $generator instanceof PasswordGeneratorInterface) { |
|
35 | 1 | $msg = 'The generators should either be callable or an instance of '.PasswordGeneratorInterface::class; |
|
36 | |||
37 | 1 | throw new \UnexpectedValueException($msg); |
|
38 | } |
||
39 | |||
40 | 2 | static::$generators[$name] = \is_callable($generator) |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
41 | 1 | ? $generator |
|
42 | : function (int $length) use ($generator): string { |
||
43 | 1 | return $generator->generate($length); |
|
44 | 1 | }; |
|
45 | 2 | } |
|
46 | |||
47 | /** |
||
48 | * Get the previously registered generator by the given name. |
||
49 | * |
||
50 | * @param null|string $generatorName |
||
51 | * |
||
52 | * @return callable |
||
53 | */ |
||
54 | 3 | public function get(?string $generatorName = null): callable |
|
55 | { |
||
56 | 3 | if (! \in_array($generatorName, array_keys(static::$generators), true)) { |
|
0 ignored issues
–
show
|
|||
57 | 1 | throw new \UnexpectedValueException( |
|
58 | 1 | 'The '.$generatorName.' password generator is not registered.' |
|
59 | ); |
||
60 | } |
||
61 | |||
62 | 2 | return static::$generators[$generatorName]; |
|
63 | } |
||
64 | |||
65 | /** |
||
66 | * Create a new password generator instance using the given |
||
67 | * fully qualified password generator class name. |
||
68 | * |
||
69 | * @param string $className |
||
70 | * |
||
71 | * @return PasswordGeneratorInterface |
||
72 | */ |
||
73 | 3 | private function createGeneratorFromString(string $className): PasswordGeneratorInterface |
|
74 | { |
||
75 | 3 | if (! class_exists($className)) { |
|
76 | 1 | throw new \RuntimeException( |
|
77 | 1 | "The generator [{$className}] could not be found." |
|
78 | ); |
||
79 | } |
||
80 | |||
81 | 2 | $generatorReflection = new \ReflectionClass($className); |
|
82 | 2 | if (! $generatorReflection->isInstantiable()) { |
|
83 | 1 | throw new \RuntimeException( |
|
84 | 1 | "The generator [{$className}] is not instantiable." |
|
85 | ); |
||
86 | } |
||
87 | |||
88 | 1 | return new $className(); |
|
89 | } |
||
90 | } |
||
91 |