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

ServiceProvider::configPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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\Http\Middleware\Otp;
11
use Erdemkeren\Otp\Repositories\DatabaseTokenRepository;
12
use Illuminate\Routing\Router;
13
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
14
15
class ServiceProvider extends BaseServiceProvider
16
{
17
    /**
18
     * Indicates if loading of the provider is deferred.
19
     *
20
     * @var bool
21
     */
22
    protected bool $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
     * @return void
35
     */
36
    public function register(): void
37
    {
38
        $service = $this->createServiceInstance();
39
        $this->registerDefaultPasswordGenerators($service);
40
41
        $this->app->singleton('otp', function () use ($service) {
42
            return $service;
43
        });
44
45
        /** @var Router $router */
46
        $router = $this->app['router'];
47
        $router->aliasMiddleware('otp', Otp::class);
48
    }
49
50
    /**
51
     * Get the services provided by the provider.
52
     *
53
     * @return array
54
     */
55
    public function provides(): array
56
    {
57
        return [
58
            'otp',
59
        ];
60
    }
61
62
    /**
63
     * Create a new otp service instance.
64
     *
65
     * @return OtpService
66
     */
67
    private function createServiceInstance(): OtpService
68
    {
69
        return new OtpService(
70
            new FormatManager(),
0 ignored issues
show
Bug introduced by
The call to Erdemkeren\Otp\FormatManager::__construct() has too few arguments starting with defaultFormat. ( 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
            new FormatManager(),

This check compares calls to functions or methods with their respective definitions. If the call has less 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
            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

71
            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...
72
            new DatabaseTokenRepository()
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\StringGenerator::class);
0 ignored issues
show
Bug introduced by
The method addPasswordGenerator() does not exist on Erdemkeren\Otp\OtpService. ( Ignorable by Annotation )

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

84
        $service->/** @scrutinizer ignore-call */ 
85
                  addPasswordGenerator('string', Generators\StringGenerator::class);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
85
        $service->addPasswordGenerator('numeric', Generators\NumericGenerator::class);
86
        $service->addPasswordGenerator('numeric-no-0', Generators\NumericNo0Generator::class);
87
    }
88
89
    /**
90
     * Get the project config path.
91
     *
92
     * @return string
93
     */
94
    private function configPath(): string
95
    {
96
        return __DIR__.'/../config/otp.php';
97
    }
98
99
    /**
100
     * Get the migration path.
101
     *
102
     * @return string
103
     */
104
    private function migrationPath(): string
105
    {
106
        return __DIR__.'/../database/migrations/';
107
    }
108
109
    /**
110
     * Get the view path.
111
     *
112
     * @return string
113
     */
114
    private function viewPath(): string
115
    {
116
        return __DIR__.'/../views/';
117
    }
118
}
119