EmailVerificationBrokerManager::getDefaultDriver()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Auth\Services;
6
7
use InvalidArgumentException;
8
use Rinvex\Auth\Contracts\EmailVerificationBrokerContract;
9
use Rinvex\Auth\Contracts\EmailVerificationBrokerFactoryContract;
10
11
class EmailVerificationBrokerManager implements EmailVerificationBrokerFactoryContract
12
{
13
    /**
14
     * The application instance.
15
     *
16
     * @var \Illuminate\Foundation\Application
17
     */
18
    protected $app;
19
20
    /**
21
     * The array of created "drivers".
22
     *
23
     * @var array
24
     */
25
    protected $brokers = [];
26
27
    /**
28
     * Create a new EmailVerificationBroker manager instance.
29
     *
30
     * @param \Illuminate\Foundation\Application $app
31
     */
32
    public function __construct($app)
33
    {
34
        $this->app = $app;
35
    }
36
37
    /**
38
     * Attempt to get the broker from the local cache.
39
     *
40
     * @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...
41
     *
42
     * @return \Rinvex\Auth\Contracts\EmailVerificationBrokerContract
43
     */
44
    public function broker($name = null): EmailVerificationBrokerContract
45
    {
46
        $name = $name ?: $this->getDefaultDriver();
47
48
        return $this->brokers[$name] ?? $this->brokers[$name] = $this->resolve($name);
49
    }
50
51
    /**
52
     * Resolve the given broker.
53
     *
54
     * @param string $name
55
     *
56
     * @throws \InvalidArgumentException
57
     *
58
     * @return \Rinvex\Auth\Contracts\EmailVerificationBrokerContract
59
     */
60
    protected function resolve($name): EmailVerificationBrokerContract
61
    {
62
        $config = $this->getConfig($name);
63
64
        if (is_null($config)) {
65
            throw new InvalidArgumentException("Email verification broker [{$name}] is not defined.");
66
        }
67
68
        return new EmailVerificationBroker(
69
            $this->app['auth']->createUserProvider($config['provider']),
70
            $this->app['config']['app.key'],
71
            $config['expire']
72
        );
73
    }
74
75
    /**
76
     * Get the email verification broker configuration.
77
     *
78
     * @param string $name
79
     *
80
     * @return array
81
     */
82
    protected function getConfig($name): array
83
    {
84
        return $this->app['config']["auth.emails.{$name}"];
85
    }
86
87
    /**
88
     * Get the default email verification broker name.
89
     *
90
     * @return string
91
     */
92
    public function getDefaultDriver(): string
93
    {
94
        return $this->app['config']['auth.defaults.emails'];
95
    }
96
97
    /**
98
     * Set the default email verification broker name.
99
     *
100
     * @param string $name
101
     *
102
     * @return void
103
     */
104
    public function setDefaultDriver($name): void
105
    {
106
        $this->app['config']['auth.defaults.emails'] = $name;
107
    }
108
109
    /**
110
     * Dynamically call the default driver instance.
111
     *
112
     * @param string $method
113
     * @param array  $parameters
114
     *
115
     * @return mixed
116
     */
117
    public function __call($method, $parameters)
118
    {
119
        return $this->broker()->{$method}(...$parameters);
120
    }
121
}
122