LodashServiceProvider   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 78.26%

Importance

Changes 0
Metric Value
dl 0
loc 90
ccs 36
cts 46
cp 0.7826
rs 10
c 0
b 0
f 0
wmc 7
lcom 2
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 10 1
A register() 0 6 1
A registerCommands() 0 8 2
A loadTranslations() 0 8 1
A registerBladeDirectives() 0 18 1
A registerRequestMacros() 0 22 1
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
use Longman\LaravelLodash\Commands\ClearAll;
10
use Longman\LaravelLodash\Commands\DbClear;
11
use Longman\LaravelLodash\Commands\DbDump;
12
use Longman\LaravelLodash\Commands\DbRestore;
13
use Longman\LaravelLodash\Commands\LogClear;
14
use Longman\LaravelLodash\Commands\UserAdd;
15
use Longman\LaravelLodash\Commands\UserPassword;
16
17
use function array_keys;
18
use function array_pad;
19
use function preg_split;
20
use function trim;
21
22
class LodashServiceProvider extends ServiceProvider
23
{
24
    protected $commands = [
25
        'command.lodash.clear-all'     => ClearAll::class,
26
        'command.lodash.db.clear'      => DbClear::class,
27
        'command.lodash.db.dump'       => DbDump::class,
28
        'command.lodash.db.restore'    => DbRestore::class,
29
        'command.lodash.log.clear'     => LogClear::class,
30
        'command.lodash.user.add'      => UserAdd::class,
31
        'command.lodash.user.password' => UserPassword::class,
32
    ];
33
34 71
    public function boot(): void
35
    {
36 71
        $this->publishes([
37 71
            __DIR__ . '/../config/config.php' => config_path('lodash.php'),
38
        ]);
39
40 71
        $this->registerBladeDirectives();
41
42 71
        $this->loadTranslations();
43 71
    }
44
45 71
    public function register(): void
46
    {
47 71
        $this->registerCommands();
48
49 71
        $this->registerRequestMacros();
50 71
    }
51
52 71
    protected function registerCommands(): void
53
    {
54 71
        foreach ($this->commands as $name => $class) {
55 71
            $this->app->singleton($name, $class);
56
        }
57
58 71
        $this->commands(array_keys($this->commands));
59 71
    }
60
61 71
    protected function registerBladeDirectives(): void
62
    {
63
        // Display relative time
64 71
        app('blade.compiler')->directive('datetime', static function ($expression) {
65
66
            return "<?php echo '<time datetime=\'' . with({$expression})->toIso8601String()
67
                . '\' title=\'' . $expression . '\'>'
68
                . with({$expression})->diffForHumans() . '</time>' ?>";
69 71
        });
70
71
        // Pluralization helper
72 71
        app('blade.compiler')->directive('plural', static function ($expression) {
73
            $expression = trim($expression, '()');
74
            [$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...
75
76
            return "<?php echo $count . $spacer . str_plural($str, $count) ?>";
77 71
        });
78 71
    }
79
80 71
    protected function registerRequestMacros(): void
81
    {
82 71
        Request::macro('getInt', function (string $name, int $default = 0): int {
83
84
            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...
85 71
        });
86
87 71
        Request::macro('getBool', function (string $name, bool $default = false): bool {
88
89
            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...
90 71
        });
91
92 71
        Request::macro('getFloat', function (string $name, float $default = 0): float {
93
94
            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...
95 71
        });
96
97 71
        Request::macro('getString', function (string $name, string $default = ''): string {
98
99
            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...
100 71
        });
101 71
    }
102
103 71
    protected function loadTranslations(): void
104
    {
105 71
        $this->loadTranslationsFrom(__DIR__ . '/../translations', 'lodash');
106
107 71
        $this->publishes([
108 71
            __DIR__ . '/../translations' => resource_path('lang/vendor/lodash'),
109
        ]);
110 71
    }
111
}
112