MultiUnitModelsServiceProvider::boot()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 9.44
c 0
b 0
f 0
cc 4
nc 3
nop 0
1
<?php
2
3
namespace MaksimM\MultiUnitModels;
4
5
use Exception;
6
use Illuminate\Support\Facades\Validator;
7
use Illuminate\Support\ServiceProvider;
8
use MaksimM\MultiUnitModels\Traits\MultiUnitSupport;
9
use UnitConverter\Unit\AbstractUnit;
10
11
class MultiUnitModelsServiceProvider extends ServiceProvider
12
{
13
    /**
14
     * Bootstrap the application services.
15
     *
16
     * @throws Exception
17
     */
18
    public function boot()
19
    {
20
        if ($this->app->runningInConsole()) {
21
            if (!str_contains($this->app->version(), 'Lumen')) {
22
                $this->publishes([
23
                    __DIR__.'/../config/multi-units.php' => config_path('multi-units.php'),
24
                ], 'config');
25
            }
26
        }
27
        Validator::extend('supported_units', function ($attribute, $value, $parameters, $validator) {
0 ignored issues
show
Unused Code introduced by
The parameter $validator is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
28
            if (2 != count($parameters)) {
29
                throw new Exception('supported_units validator requires exact 2 parameters');
30
            }
31
            $modelClass = $parameters[0];
32
            $unitField = $parameters[1];
33
            /**
34
             * @var $model MultiUnitSupport
35
             */
36
            $model = (new $modelClass());
37
38
            return in_array($value, array_map(function ($unitClass) {
39
                /**
40
                 * @var AbstractUnit $unit
41
                 */
42
                $unit = new $unitClass();
43
44
                return $unit->getId();
45
            }, $model->getMultiUnitFieldSupportedUnits($unitField)));
46
        });
47
    }
48
49
    /**
50
     * Register the application services.
51
     */
52
    public function register()
53
    {
54
        $this->mergeConfigFrom(__DIR__.'/../config/multi-units.php', 'multi-units');
55
    }
56
}
57