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

EmailVerificationBrokerManager::getDefaultDriver()   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 0
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 Rinvex\Fort\Repositories\EmailVerificationTokenRepository;
23
use Rinvex\Fort\Contracts\EmailVerificationBrokerFactoryContract;
24
25
class EmailVerificationBrokerManager implements EmailVerificationBrokerFactoryContract
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 EmailVerificationBroker 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 \Rinvex\Fort\Contracts\EmailVerificationBrokerContract
73
     */
74
    protected function resolve($name)
75
    {
76
        $config = $this->getConfig($name);
77
78
        if (is_null($config)) {
79
            throw new InvalidArgumentException("Email verification broker [{$name}] is not defined.");
80
        }
81
82
        // The email verification broker uses a token repository to validate tokens and send email verification e-mails
83
        return new EmailVerificationBroker(
84
            $this->createTokenRepository($config),
85
            $this->app['auth']->createUserProvider($config['provider'])
86
        );
87
    }
88
89
    /**
90
     * Create a token repository instance based on the given configuration.
91
     *
92
     * @param array $config
93
     *
94
     * @return \Rinvex\Fort\Contracts\EmailVerificationTokenRepositoryContract
95
     */
96
    protected function createTokenRepository(array $config)
97
    {
98
        $key = $this->app['config']['app.key'];
99
100
        if (Str::startsWith($key, 'base64:')) {
101
            $key = base64_decode(mb_substr($key, 7));
102
        }
103
104
        $connection = $config['connection'] ?? null;
105
106
        return new EmailVerificationTokenRepository(
107
            $this->app['db']->connection($connection),
108
            $this->app['hash'],
109
            $this->app['config']['rinvex.fort.tables.email_verifications'],
110
            $key,
111
            $config['expire']
112
        );
113
    }
114
115
    /**
116
     * Get the email verification broker configuration.
117
     *
118
     * @param string $name
119
     *
120
     * @return array
121
     */
122
    protected function getConfig($name)
123
    {
124
        return $this->app['config']["rinvex.fort.emailverification.{$name}"];
125
    }
126
127
    /**
128
     * Get the default email verification broker name.
129
     *
130
     * @return string
131
     */
132
    public function getDefaultDriver()
133
    {
134
        return $this->app['config']['rinvex.fort.emailverification.broker'];
135
    }
136
137
    /**
138
     * Set the default email verification broker name.
139
     *
140
     * @param string $name
141
     *
142
     * @return void
143
     */
144
    public function setDefaultDriver($name)
145
    {
146
        $this->app['config']['rinvex.fort.emailverification.broker'] = $name;
147
    }
148
149
    /**
150
     * Dynamically call the default driver instance.
151
     *
152
     * @param string $method
153
     * @param array  $parameters
154
     *
155
     * @return mixed
156
     */
157
    public function __call($method, $parameters)
158
    {
159
        return $this->broker()->{$method}(...$parameters);
160
    }
161
}
162