Completed
Push — master ( 9d421d...7b686b )
by Avtandil
10:28 queued 08:22
created

LodashServiceProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 70.59%

Importance

Changes 0
Metric Value
dl 0
loc 79
ccs 24
cts 34
cp 0.7059
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 8 1
A register() 0 6 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 11
    public function boot(): void
31
    {
32 11
        $this->publishes([
33 11
            __DIR__ . '/../config/config.php' => config_path('lodash.php'),
34
        ]);
35
36 11
        $this->registerBladeDirectives();
37 11
    }
38
39 11
    public function register(): void
40
    {
41 11
        $this->registerCommands();
42
43 11
        $this->registerRequestMacros();
44 11
    }
45
46 11
    protected function registerCommands(): void
47
    {
48 11
        foreach ($this->commands as $name => $class) {
49 11
            $this->app->singleton($name, $class);
50
        }
51
52 11
        $this->commands(array_keys($this->commands));
53 11
    }
54
55 11
    protected function registerBladeDirectives(): void
56
    {
57
        // Display relative time
58
        Blade::directive('datetime', function ($expression) {
59
60
            return "<?php echo '<time datetime=\'' . with({$expression})->toIso8601String()
61
                . '\' title=\'' . $expression . '\'>'
62
                . with({$expression})->diffForHumans() . '</time>' ?>";
63 11
        });
64
65
        // Pluralization helper
66
        Blade::directive('plural', function ($expression) {
67
            $expression = trim($expression, '()');
68
            list($count, $str, $spacer) = array_pad(preg_split('/,\s*/', $expression), 3, "' '");
69
70
            return "<?php echo $count . $spacer . str_plural($str, $count) ?>";
71 11
        });
72 11
    }
73
74 11
    protected function registerRequestMacros(): void
75
    {
76
        Request::macro('getInt', function (string $name, int $default = 0): int {
77
78
            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...
79 11
        });
80
81
        Request::macro('getBool', function (string $name, bool $default = false): bool {
82
83
            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...
84 11
        });
85
86
        Request::macro('getFloat', function (string $name, float $default = 0): float {
87
88
            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...
89 11
        });
90
91
        Request::macro('getString', function (string $name, string $default = ''): string {
92
93
            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...
94 11
        });
95 11
    }
96
}
97