1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Erdemkeren\TemporaryAccess; |
4
|
|
|
|
5
|
|
|
use UnexpectedValueException; |
6
|
|
|
use Illuminate\Support\ServiceProvider; |
7
|
|
|
use Erdemkeren\TemporaryAccess\Token\TokenGenerator; |
8
|
|
|
use Erdemkeren\TemporaryAccess\Contracts\AccessTokenRepositoryInterface; |
9
|
|
|
use Erdemkeren\TemporaryAccess\Token\TokenGenerator\TokenGeneratorInterface; |
10
|
|
|
|
11
|
|
|
class TemporaryAccessServiceProvider extends ServiceProvider |
12
|
|
|
{ |
13
|
|
|
protected $defer = true; |
14
|
|
|
|
15
|
|
|
public function boot() |
16
|
|
|
{ |
17
|
|
|
$this->publishes([ |
18
|
|
|
__DIR__.'/../database/migrations/' => database_path('migrations'), |
19
|
|
|
], 'migrations'); |
20
|
|
|
|
21
|
|
|
$this->publishes([ |
22
|
|
|
__DIR__.'/../config/temporary_access.php' => config_path('temporary_access.php'), |
23
|
|
|
]); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function register() |
27
|
|
|
{ |
28
|
|
|
$this->app->singleton(AccessTokenRepositoryInterface::class, function () { |
29
|
|
|
return new DatabaseAccessTokenRepository($this->app['db']->connection(), |
30
|
|
|
config('temporary_access.table', 'temporary_access_tokens'), config('temporary_access.expires', 15)); |
31
|
|
|
}); |
32
|
|
|
|
33
|
|
|
$this->app->singleton(TokenGeneratorInterface::class, function () { |
34
|
|
|
$generator = config('temporary_access.token_generator', 'string'); |
35
|
|
|
$generators = $this->getTokenGenerators(); |
36
|
|
|
if(! array_key_exists($generator, $generators)) { |
37
|
|
|
throw new UnexpectedValueException( |
38
|
|
|
"The access token generator [$generator] could not be found." |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
return $generators[$generator]; |
|
|
|
|
42
|
|
|
} |
43
|
|
|
}); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function provides() |
47
|
|
|
{ |
48
|
|
|
return [ |
49
|
|
|
TokenGeneratorInterface::class, |
50
|
|
|
AccessTokenRepositoryInterface::class, |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private function getTokenGenerators(): array |
55
|
|
|
{ |
56
|
|
|
return [ |
57
|
|
|
'string' => function (string $key): TokenGeneratorInterface { |
58
|
|
|
return new TokenGenerator\StringTokenGenerator($key); |
59
|
|
|
}, |
60
|
|
|
'numeric' => function (string $key): TokenGeneratorInterface { |
61
|
|
|
return new TokenGenerator\NumericTokenGenerator($key); |
62
|
|
|
}, |
63
|
|
|
'numeric-no-0' => function (string $key): TokenGeneratorInterface { |
64
|
|
|
return new TokenGenerator\NumericNo0TokenGenerator($key); |
65
|
|
|
} |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.