Passed
Pull Request — master (#20)
by Hilmi Erdem
10:18
created

ServiceProvider::createServiceInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
/*
4
 * @copyright 2018 Hilmi Erdem KEREN
5
 * @license MIT
6
 */
7
8
namespace Erdemkeren\Otp;
9
10
use Erdemkeren\Otp\Generators;
11
use Erdemkeren\Otp\Repositories\DatabaseTokenRepository;
12
use Illuminate\Routing\Router;
13
use Erdemkeren\Otp\Http\Middleware\Otp;
0 ignored issues
show
Bug introduced by
The type Erdemkeren\Otp\Http\Middleware\Otp was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
15
16
/**
17
 * Class ServiceProvider.
18
 */
19
class ServiceProvider extends BaseServiceProvider
20
{
21
    /**
22
     * Indicates if loading of the provider is deferred.
23
     *
24
     * @var bool
25
     */
26
    protected bool $defer = true;
27
28
    public function boot(): void
29
    {
30
        $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

30
        $this->publishes([$this->configPath() => /** @scrutinizer ignore-call */ config_path('otp.php')]);
Loading history...
31
        $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

31
        $this->publishes([$this->migrationPath() => /** @scrutinizer ignore-call */ database_path('migrations')]);
Loading history...
32
        $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

32
        $this->publishes([$this->viewPath() => /** @scrutinizer ignore-call */ resource_path('views')]);
Loading history...
33
    }
34
35
    /**
36
     * Register the otp service.
37
     *
38
     * @return void
39
     */
40
    public function register(): void
41
    {
42
        $service = $this->createServiceInstance();
43
        $this->registerDefaultPasswordGenerators($service);
44
45
        $this->app->singleton('otp', function () use ($service) {
46
            return $service;
47
        });
48
49
        /** @var Router $router */
50
        $router = $this->app['router'];
51
        $router->aliasMiddleware('otp', Otp::class);
52
    }
53
54
    /**
55
     * Get the services provided by the provider.
56
     *
57
     * @return array
58
     */
59
    public function provides(): array
60
    {
61
        return [
62
            'otp',
63
        ];
64
    }
65
66
    /**
67
     * Create a new otp service instance.
68
     *
69
     * @return OtpService
70
     */
71
    private function createServiceInstance(): OtpService
72
    {
73
        return new OtpService(
74
            new GeneratorManager(),
75
            new Encryptor(config('app.secret')),
0 ignored issues
show
Bug introduced by
The function config 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

75
            new Encryptor(/** @scrutinizer ignore-call */ config('app.secret')),
Loading history...
Bug introduced by
The type Erdemkeren\Otp\Encryptor was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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