Completed
Push — master ( edb43e...425779 )
by Abdelrahman
10:35
created

PasswordResetBrokerManager::setDefaultDriver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * NOTICE OF LICENSE
5
 *
6
 * Part of the Rinvex Fort Package.
7
 *
8
 * This source file is subject to The MIT License (MIT)
9
 * that is bundled with this package in the LICENSE file.
10
 *
11
 * Package: Rinvex Fort Package
12
 * License: The MIT License (MIT)
13
 * Link:    https://rinvex.com
14
 */
15
16
declare(strict_types=1);
17
18
namespace Rinvex\Fort\Services;
19
20
use Illuminate\Support\Str;
21
use InvalidArgumentException;
22
use Illuminate\Contracts\Auth\PasswordBrokerFactory;
23
use Rinvex\Fort\Repositories\PasswordResetTokenRepository;
24
25
class PasswordResetBrokerManager implements PasswordBrokerFactory
26
{
27
    /**
28
     * The application instance.
29
     *
30
     * @var \Illuminate\Foundation\Application
31
     */
32
    protected $app;
33
34
    /**
35
     * The array of created "drivers".
36
     *
37
     * @var array
38
     */
39
    protected $brokers = [];
40
41
    /**
42
     * Create a new PasswordResetBroker manager instance.
43
     *
44
     * @param \Illuminate\Foundation\Application $app
45
     */
46
    public function __construct($app)
47
    {
48
        $this->app = $app;
49
    }
50
51
    /**
52
     * Attempt to get the broker from the local cache.
53
     *
54
     * @param string $name
0 ignored issues
show
Documentation introduced by
Should the type for parameter $name not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
55
     *
56
     * @return \Illuminate\Contracts\Auth\PasswordBroker
57
     */
58
    public function broker($name = null)
59
    {
60
        $name = $name ?: $this->getDefaultDriver();
61
62
        return $this->brokers[$name] ?? $this->brokers[$name] = $this->resolve($name);
63
    }
64
65
    /**
66
     * Resolve the given broker.
67
     *
68
     * @param string $name
69
     *
70
     * @throws \InvalidArgumentException
71
     *
72
     * @return \Illuminate\Contracts\Auth\PasswordBroker
73
     */
74
    protected function resolve($name)
75
    {
76
        $config = $this->getConfig($name);
77
78
        if (is_null($config)) {
79
            throw new InvalidArgumentException("Password resetter [{$name}] is not defined.");
80
        }
81
82
        // The password broker uses a token repository to validate tokens and send user
83
        // password e-mails, as well as validating that password reset process as an
84
        // aggregate service of sorts providing a convenient interface for resets.
85
        return new PasswordResetBroker(
86
            $this->createTokenRepository($config),
87
            $this->app['auth']->createUserProvider($config['provider'])
88
        );
89
    }
90
91
    /**
92
     * Create a token repository instance based on the given configuration.
93
     *
94
     * @param array $config
95
     *
96
     * @return \Rinvex\Fort\Contracts\PasswordResetTokenRepositoryContract
97
     */
98
    protected function createTokenRepository(array $config)
99
    {
100
        $key = $this->app['config']['app.key'];
101
102
        if (Str::startsWith($key, 'base64:')) {
103
            $key = base64_decode(mb_substr($key, 7));
104
        }
105
106
        $connection = $config['connection'] ?? null;
107
108
        return new PasswordResetTokenRepository(
109
            $this->app['db']->connection($connection),
110
            $this->app['hash'],
111
            $config['table'],
112
            $key,
113
            $config['expire']
114
        );
115
    }
116
117
    /**
118
     * Get the password reset broker configuration.
119
     *
120
     * @param string $name
121
     *
122
     * @return array
123
     */
124
    protected function getConfig($name)
125
    {
126
        return $this->app['config']["auth.passwords.{$name}"];
127
    }
128
129
    /**
130
     * Get the default password reset broker name.
131
     *
132
     * @return string
133
     */
134
    public function getDefaultDriver()
135
    {
136
        return $this->app['config']['auth.defaults.passwords'];
137
    }
138
139
    /**
140
     * Set the default password reset broker name.
141
     *
142
     * @param string $name
143
     *
144
     * @return void
145
     */
146
    public function setDefaultDriver($name)
147
    {
148
        $this->app['config']['auth.defaults.passwords'] = $name;
149
    }
150
151
    /**
152
     * Dynamically call the default driver instance.
153
     *
154
     * @param string $method
155
     * @param array  $parameters
156
     *
157
     * @return mixed
158
     */
159
    public function __call($method, $parameters)
160
    {
161
        return $this->broker()->{$method}(...$parameters);
162
    }
163
}
164