Completed
Pull Request — master (#10)
by
unknown
04:25
created

LodashServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 1
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Longman\LaravelLodash;
6
7
use Illuminate\Http\Request;
8
use Illuminate\Support\ServiceProvider;
9
10
class LodashServiceProvider extends ServiceProvider
11
{
12
    protected $commands = [
13
        'command.lodash.clear-all'     => \Longman\LaravelLodash\Commands\ClearAll::class,
14
        'command.lodash.db.clear'      => \Longman\LaravelLodash\Commands\DbClear::class,
15
        'command.lodash.db.dump'       => \Longman\LaravelLodash\Commands\DbDump::class,
16
        'command.lodash.db.restore'    => \Longman\LaravelLodash\Commands\DbRestore::class,
17
        'command.lodash.log.clear'     => \Longman\LaravelLodash\Commands\LogClear::class,
18
        'command.lodash.user.add'      => \Longman\LaravelLodash\Commands\UserAdd::class,
19
        'command.lodash.user.password' => \Longman\LaravelLodash\Commands\UserPassword::class,
20
    ];
21
22 15
    public function boot(): void
23
    {
24 15
        $this->publishes([
25 15
            __DIR__ . '/../config/config.php' => config_path('lodash.php'),
26
        ]);
27
28 15
        $this->registerBladeDirectives();
29
30 15
        $this->loadTranslations();
31 15
    }
32
33 15
    public function register(): void
34
    {
35 15
        $this->registerCommands();
36
37 15
        $this->registerRequestMacros();
38 15
    }
39
40 15
    protected function registerCommands(): void
41
    {
42 15
        foreach ($this->commands as $name => $class) {
43 15
            $this->app->singleton($name, $class);
44
        }
45
46 15
        $this->commands(array_keys($this->commands));
47 15
    }
48
49 15
    protected function registerBladeDirectives(): void
50
    {
51
        // Display relative time
52
        app('blade.compiler')->directive('datetime', static function ($expression) {
53
54
            return "<?php echo '<time datetime=\'' . with({$expression})->toIso8601String()
55
                . '\' title=\'' . $expression . '\'>'
56
                . with({$expression})->diffForHumans() . '</time>' ?>";
57 15
        });
58
59
        // Pluralization helper
60
        app('blade.compiler')->directive('plural', static function ($expression) {
61
            $expression = trim($expression, '()');
62
            [$count, $str, $spacer] = array_pad(preg_split('/,\s*/', $expression), 3, "' '");
0 ignored issues
show
Bug introduced by
The variable $count does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $str does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $spacer does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
63
64
            return "<?php echo $count . $spacer . str_plural($str, $count) ?>";
65 15
        });
66 15
    }
67
68 15
    protected function registerRequestMacros(): void
69
    {
70
        Request::macro('getInt', function (string $name, int $default = 0): int {
71
72
            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...
73 15
        });
74
75
        Request::macro('getBool', function (string $name, bool $default = false): bool {
76
77
            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...
78 15
        });
79
80
        Request::macro('getFloat', function (string $name, float $default = 0): float {
81
82
            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...
83 15
        });
84
85
        Request::macro('getString', function (string $name, string $default = ''): string {
86
87
            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...
88 15
        });
89 15
    }
90
91 15
    protected function loadTranslations(): void
92
    {
93 15
        $this->loadTranslationsFrom(__DIR__ . '/../translations', 'lodash');
94
95 15
        $this->publishes([
96 15
            __DIR__ . '/../translations' => resource_path('lang/vendor/lodash'),
97
        ]);
98 15
    }
99
}
100