Completed
Push — master ( 3111fa...5df1df )
by Hilmi Erdem
07:25
created

TemporaryAccessServiceProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 58
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 10 1
A register() 0 19 2
A provides() 0 7 1
A getTokenGenerators() 0 14 1
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];
0 ignored issues
show
Unused Code introduced by
return $generators[$generator]; does not seem to be reachable.

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 or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

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.

Loading history...
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