Completed
Pull Request — master (#4)
by Neo
02:47
created

LodashServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 10
ccs 0
cts 6
cp 0
crap 2
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the Laravel Lodash package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
declare(strict_types=1);
11
12
namespace Longman\LaravelLodash;
13
14
use Illuminate\Http\Request;
15
use Illuminate\Support\ServiceProvider;
16
17
class LodashServiceProvider extends ServiceProvider
18
{
19
    public function boot(): void
20
    {
21
        $this->publishes([
22
            __DIR__ . '/../config/config.php' => config_path('lodash.php'),
23
        ]);
24
25
        $this->registerBladeDirectives();
26
27
        $this->loadTranslations();
28
    }
29
30
    public function register(): void
31
    {
32
        $this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'lodash');
33
34
        $this->registerCommands();
35
36
        $this->registerRequestMacros();
37
    }
38
39
    protected function registerCommands(): void
40
    {
41
        if (empty($commands = (array) config('lodash.available_commands'))) {
42
            return;
43
        }
44
45
        foreach ($commands as $name => $class) {
46
            $this->app->singleton($name, $class);
47
        }
48
49
        $this->commands(array_keys($commands));
50
    }
51
52
    protected function registerBladeDirectives(): void
53
    {
54
        $register_directives = config('lodash.register_blade_directives');
55
56
        if ($register_directives['datetime'] ?? false) {
57
            Blade::directive('datetime', function ($expression) {
58
                return "<?php echo '<time datetime=\'' . with({$expression})->toIso8601String()
59
                    . '\' title=\'' . $expression . '\'>'
60
                    . with({$expression})->diffForHumans() . '</time>' ?>";
61
            });
62
        }
63
64
      if ($register_directives['plural'] ?? false) {
65
            Blade::directive('plural', function ($expression) {
66
                $expression = trim($expression, '()');
67
                list($count, $str, $spacer) = array_pad(preg_split('/,\s*/', $expression), 3, "' '");
68
    
69
                return "<?php echo $count . $spacer . str_plural($str, $count) ?>";
70
            });
71
        }
72
    }
73
74
    protected function registerRequestMacros(): void
75
    {
76
        $register_request_macros = config('lodash.register_request_macros');
77
78 View Code Duplication
        if ($register_request_macros['getInt'] ?? false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
            Request::macro('getInt', function (string $name, int $default = 0): int {
80
                return (int) $this->get($name, $default);
0 ignored issues
show
Bug introduced by
The method get() does not seem to exist on object<Longman\LaravelLo...\LodashServiceProvider>.

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...
81
            });
82
        }
83
84 View Code Duplication
        if ($register_request_macros['getBool'] ?? false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
            Request::macro('getBool', function (string $name, bool $default = false): bool {
86
                return (bool) $this->get($name, $default);
0 ignored issues
show
Bug introduced by
The method get() does not seem to exist on object<Longman\LaravelLo...\LodashServiceProvider>.

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...
87
            });
88
        }
89
90 View Code Duplication
        if ($register_request_macros['getFloat'] ?? false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
            Request::macro('getFloat', function (string $name, float $default = 0): float {
92
                return (float) $this->get($name, $default);
0 ignored issues
show
Bug introduced by
The method get() does not seem to exist on object<Longman\LaravelLo...\LodashServiceProvider>.

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...
93
            });
94
        }
95
96 View Code Duplication
        if ($register_request_macros['getString'] ?? false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
            Request::macro('getString', function (string $name, string $default = ''): string {
98
                return (string) $this->get($name, $default);
0 ignored issues
show
Bug introduced by
The method get() does not seem to exist on object<Longman\LaravelLo...\LodashServiceProvider>.

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...
99
            });
100
        }
101
    }
102
103
    protected function loadTranslations(): void
104
    {
105
        $this->loadTranslationsFrom(__DIR__ . '/../translations', 'lodash');
106
107
        $this->publishes([
108
            __DIR__ . '/../translations' => resource_path('lang/vendor/lodash'),
109
        ]);
110
    }
111
}
112