OtpServiceProvider::createServiceInstance()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * @copyright 2018 Hilmi Erdem KEREN
5
 * @license MIT
6
 */
7
8
namespace Erdemkeren\Otp;
9
10
use Erdemkeren\Otp\Http\Middleware\Otp;
11
use Erdemkeren\Otp\PasswordGenerators as Generators;
12
use Illuminate\Routing\Router;
13
use Illuminate\Support\ServiceProvider;
14
15
class OtpServiceProvider extends ServiceProvider
16
{
17
    /**
18
     * Indicates if loading of the provider is deferred.
19
     *
20
     * @var bool
21
     */
22
    protected $defer = true;
23
24
    public function boot(): void
25
    {
26
        $this->publishes([$this->configPath() => config_path('otp.php')]);
0 ignored issues
show
Bug introduced by
The function config_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
        $this->publishes([$this->configPath() => /** @scrutinizer ignore-call */ config_path('otp.php')]);
Loading history...
27
        $this->publishes([$this->migrationPath() => database_path('migrations')]);
0 ignored issues
show
Bug introduced by
The function database_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

27
        $this->publishes([$this->migrationPath() => /** @scrutinizer ignore-call */ database_path('migrations')]);
Loading history...
28
        $this->publishes([$this->viewPath() => resource_path('views')]);
0 ignored issues
show
Bug introduced by
The function resource_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
        $this->publishes([$this->viewPath() => /** @scrutinizer ignore-call */ resource_path('views')]);
Loading history...
29
    }
30
31
    /**
32
     * Register the otp service.
33
     */
34
    public function register(): void
35
    {
36
        $service = $this->createServiceInstance();
37
        $this->registerDefaultPasswordGenerators($service);
38
39
        $this->app->singleton('otp', function () use ($service) {
40
            return $service;
41
        });
42
43
        /** @var Router $router */
44
        $router = $this->app['router'];
45
        $router->aliasMiddleware('otp', Otp::class);
46
    }
47
48
    /**
49
     * Get the services provided by the provider.
50
     *
51
     * @return array
52
     */
53
    public function provides(): array
54
    {
55
        return [
56
            'otp',
57
        ];
58
    }
59
60
    /**
61
     * Create a new otp service instance.
62
     *
63
     * @return OtpService
64
     */
65
    private function createServiceInstance(): OtpService
66
    {
67
        return new OtpService(
68
            new PasswordGeneratorManager(),
69
            new Encryptor(config('app.secret')),
70
            config('otp.password_generator', 'string'),
0 ignored issues
show
Unused Code introduced by
The call to Erdemkeren\Otp\config() has too many arguments starting with 'string'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
            /** @scrutinizer ignore-call */ 
71
            config('otp.password_generator', 'string'),

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
71
            6,
72
            Token::class
73
        );
74
    }
75
76
    /**
77
     * Register default password generators to the
78
     * given otp service instance.
79
     *
80
     * @param OtpService $service
81
     */
82
    private function registerDefaultPasswordGenerators($service): void
83
    {
84
        $service->addPasswordGenerator('string', Generators\StringPasswordGenerator::class);
85
        $service->addPasswordGenerator('numeric', Generators\NumericPasswordGenerator::class);
86
        $service->addPasswordGenerator('numeric-no-0', Generators\NumericNo0PasswordGenerator::class);
87
    }
88
89
    /**
90
     * Get the project config path.
91
     *
92
     * @return string
93
     */
94
    private function configPath()
95
    {
96
        return __DIR__.'/../config/otp.php';
97
    }
98
99
    /**
100
     * Get the migration path.
101
     *
102
     * @return string
103
     */
104
    private function migrationPath()
105
    {
106
        return __DIR__.'/../database/migrations/';
107
    }
108
109
    /**
110
     * Get the view path.
111
     *
112
     * @return string
113
     */
114
    private function viewPath()
115
    {
116
        return __DIR__.'/../views/';
117
    }
118
}
119