Completed
Push — master ( 425779...b6e961 )
by Abdelrahman
08:30
created

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