Completed
Push — master ( 6f2a09...4279a1 )
by Avtandil
02:30 queued 33s
created

LodashServiceProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 73.68%

Importance

Changes 0
Metric Value
dl 0
loc 80
ccs 28
cts 38
cp 0.7368
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 2

5 Methods

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