Completed
Push — master ( b265e2...a49287 )
by Avtandil
02:57
created

LodashServiceProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
dl 0
loc 90
ccs 30
cts 40
cp 0.75
rs 10
c 0
b 0
f 0
wmc 7
lcom 2
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
A registerCommands() 0 8 2
A registerBladeDirectives() 0 18 1
A registerRequestMacros() 0 22 1
A boot() 0 10 1
A register() 0 6 1
A loadTranslations() 0 8 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 Illuminate\Http\Request;
15
use Illuminate\Support\ServiceProvider;
16
17
class LodashServiceProvider extends ServiceProvider
18
{
19
    protected $commands = [
20
        'command.lodash.clear-all'     => \Longman\LaravelLodash\Commands\ClearAll::class,
21
        'command.lodash.db.clear'      => \Longman\LaravelLodash\Commands\DbClear::class,
22
        'command.lodash.db.dump'       => \Longman\LaravelLodash\Commands\DbDump::class,
23
        'command.lodash.db.restore'    => \Longman\LaravelLodash\Commands\DbRestore::class,
24
        'command.lodash.log.clear'     => \Longman\LaravelLodash\Commands\LogClear::class,
25
        'command.lodash.user.add'      => \Longman\LaravelLodash\Commands\UserAdd::class,
26
        'command.lodash.user.password' => \Longman\LaravelLodash\Commands\UserPassword::class,
27
    ];
28
29 11
    public function boot(): void
30
    {
31 11
        $this->publishes([
32 11
            __DIR__ . '/../config/config.php' => config_path('lodash.php'),
33
        ]);
34
35 11
        $this->registerBladeDirectives();
36
37 11
        $this->loadTranslations();
38 11
    }
39
40 11
    public function register(): void
41
    {
42 11
        $this->registerCommands();
43
44 11
        $this->registerRequestMacros();
45 11
    }
46
47 11
    protected function registerCommands(): void
48
    {
49 11
        foreach ($this->commands as $name => $class) {
50 11
            $this->app->singleton($name, $class);
51
        }
52
53 11
        $this->commands(array_keys($this->commands));
54 11
    }
55
56 11
    protected function registerBladeDirectives(): void
57
    {
58
        // Display relative time
59
        app('blade.compiler')->directive('datetime', function ($expression) {
60
61
            return "<?php echo '<time datetime=\'' . with({$expression})->toIso8601String()
62
                . '\' title=\'' . $expression . '\'>'
63
                . with({$expression})->diffForHumans() . '</time>' ?>";
64 11
        });
65
66
        // Pluralization helper
67
        app('blade.compiler')->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 11
        });
73 11
    }
74
75 11
    protected function registerRequestMacros(): void
76
    {
77
        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 11
        });
81
82
        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 11
        });
86
87
        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 11
        });
91
92
        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 11
        });
96 11
    }
97
98 11
    protected function loadTranslations(): void
99
    {
100 11
        $this->loadTranslationsFrom(__DIR__ . '/../translations', 'lodash');
101
102 11
        $this->publishes([
103 11
            __DIR__ . '/../translations' => resource_path('lang/vendor/lodash'),
104
        ]);
105 11
    }
106
}
107