dev-think-one /
laravel-email-change-verification
| 1 | <?php |
||
| 2 | |||
| 3 | namespace EmailChangeVerification\Broker; |
||
| 4 | |||
| 5 | use EmailChangeVerification\Token\DatabaseTokenRepository; |
||
| 6 | use EmailChangeVerification\Token\TokenRepositoryInterface; |
||
| 7 | use Illuminate\Support\Str; |
||
| 8 | use InvalidArgumentException; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * @mixin BrokerInterface |
||
| 12 | */ |
||
| 13 | class BrokerManager implements BrokerFactory |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * The application instance. |
||
| 17 | * |
||
| 18 | * @var \Illuminate\Contracts\Foundation\Application |
||
| 19 | */ |
||
| 20 | protected $app; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The array of created "drivers". |
||
| 24 | * |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | protected $brokers = []; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Create a new EmailChangeBrokerManager manager instance. |
||
| 31 | * |
||
| 32 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
| 33 | * @return void |
||
| 34 | */ |
||
| 35 | 21 | public function __construct($app) |
|
| 36 | { |
||
| 37 | 21 | $this->app = $app; |
|
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Attempt to get the broker from the local cache. |
||
| 42 | * |
||
| 43 | * @param string|null $name |
||
| 44 | * |
||
| 45 | * @return BrokerInterface |
||
| 46 | */ |
||
| 47 | 21 | public function broker($name = null) |
|
| 48 | { |
||
| 49 | 21 | $name = $name ?: $this->getDefaultDriver(); |
|
| 50 | |||
| 51 | 21 | return $this->brokers[$name] ?? ($this->brokers[$name] = $this->resolve($name)); |
|
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Resolve the given broker. |
||
| 56 | * |
||
| 57 | * @param string $name |
||
| 58 | * |
||
| 59 | * @return BrokerInterface |
||
| 60 | * |
||
| 61 | * @throws \InvalidArgumentException |
||
| 62 | */ |
||
| 63 | 20 | protected function resolve($name) |
|
| 64 | { |
||
| 65 | 20 | $config = $this->getConfig($name); |
|
| 66 | |||
| 67 | 20 | if (is_null($config)) { |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 68 | 1 | throw new InvalidArgumentException("Email change verificator [{$name}] is not defined."); |
|
| 69 | } |
||
| 70 | |||
| 71 | // The email change broker uses a token repository to validate tokens and send user |
||
| 72 | // verification e-mails, as well as validating that email change process as an |
||
| 73 | // aggregate service of sorts providing a convenient interface for email change. |
||
| 74 | 19 | return new Broker( |
|
| 75 | 19 | $this->createTokenRepository($config), |
|
| 76 | 19 | $this->app['auth']->createUserProvider($config['provider'] ?? null) |
|
| 77 | 19 | ); |
|
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Create a token repository instance based on the given configuration. |
||
| 82 | * |
||
| 83 | * @param array $config |
||
| 84 | * @return TokenRepositoryInterface |
||
| 85 | */ |
||
| 86 | 19 | protected function createTokenRepository(array $config) |
|
| 87 | { |
||
| 88 | 19 | $key = $this->app['config']['app.key']; |
|
| 89 | |||
| 90 | 19 | if (Str::startsWith($key, 'base64:')) { |
|
| 91 | $key = base64_decode(substr($key, 7)); |
||
| 92 | } |
||
| 93 | |||
| 94 | 19 | $connection = $config['connection'] ?? null; |
|
| 95 | |||
| 96 | 19 | return new DatabaseTokenRepository( |
|
| 97 | 19 | $this->app['db']->connection($connection), |
|
| 98 | 19 | $this->app['hash'], |
|
| 99 | 19 | $config['table'], |
|
| 100 | 19 | $key, |
|
| 101 | 19 | $config['expire'], |
|
| 102 | 19 | $config['throttle'] ?? 0 |
|
| 103 | 19 | ); |
|
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Get the email change broker configuration. |
||
| 108 | * |
||
| 109 | * @param string $name |
||
| 110 | * @return array |
||
| 111 | */ |
||
| 112 | 20 | protected function getConfig($name) |
|
| 113 | { |
||
| 114 | 20 | return $this->app['config']["email-change-verification.brokers.{$name}"]; |
|
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Get the default email change broker name. |
||
| 119 | * |
||
| 120 | * @return string |
||
| 121 | */ |
||
| 122 | 20 | public function getDefaultDriver(): string |
|
| 123 | { |
||
| 124 | 20 | return $this->app['config']['email-change-verification.default']; |
|
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Set the default email change broker name. |
||
| 129 | * |
||
| 130 | * @param string $name |
||
| 131 | * @return void |
||
| 132 | */ |
||
| 133 | 1 | public function setDefaultDriver(string $name): void |
|
| 134 | { |
||
| 135 | 1 | $this->app['config']['email-change-verification.default'] = $name; |
|
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Dynamically call the default driver instance. |
||
| 140 | * |
||
| 141 | * @param string $method |
||
| 142 | * @param array $parameters |
||
| 143 | * @return mixed |
||
| 144 | */ |
||
| 145 | 20 | public function __call($method, $parameters) |
|
| 146 | { |
||
| 147 | 20 | return $this->broker()->{$method}(...$parameters); |
|
| 148 | } |
||
| 149 | } |
||
| 150 |