BusServiceProvider   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 2 Features 1
Metric Value
wmc 3
c 2
b 2
f 1
lcom 1
cbo 7
dl 0
loc 46
ccs 19
cts 19
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 8 1
B register() 0 26 2
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Providers;
13
14
use Illuminate\Bus\Dispatcher;
15
use Illuminate\Support\ServiceProvider;
16
use Tinyissue\Form\FormInterface;
17
use Tinyissue\Http\Requests\Request as FormRequest;
18
use Illuminate\Foundation\Application;
19
20
/**
21
 * BusServiceProvider is the request service provider for bootstrapping and registering services in current request.
22
 *
23
 * @author Mohamed Alsharaf <[email protected]>
24
 */
25
class BusServiceProvider extends ServiceProvider
26
{
27
    /**
28
     * Bootstrap any application services.
29
     *
30
     * @param \Illuminate\Bus\Dispatcher $dispatcher
31
     */
32 63
    public function boot(Dispatcher $dispatcher)
33
    {
34
        $dispatcher->mapUsing(function ($command) {
35
            return Dispatcher::simpleMapping(
36
                            $command, 'Tinyissue\Commands', 'Tinyissue\Handlers\Commands'
37
            );
38 63
        });
39 63
    }
40
41
    /**
42
     * Register any application services.
43
     */
44 63
    public function register()
45
    {
46
        // Resolve form object by injecting the current model being edited
47
        $this->app->resolving(FormInterface::class, function (FormInterface $form, Application $app) {
48 50
            $form->setup($app->router->getCurrentRoute()->parameters());
49 50
            $form->setLoggedUser(auth()->user());
50
51 50
            return $form;
52 63
        });
53
54
        // Resolve form request by injecting the current model being edited
55
        $this->app->resolving(FormRequest::class, function (FormRequest $request, Application $app) {
56
            $form = array_first($app->router->getCurrentRoute()->parameters(), function ($key, $value) {
57 14
                return $value instanceof FormInterface;
58 27
            }, function () use ($request, $app) {
59 27
                return $app->make($request->getFormClassName());
60 27
            });
61 27
            if ($form) {
62 27
                $form->setup($app->router->getCurrentRoute()->parameters());
63 27
                $form->setLoggedUser(auth()->user());
64 27
                $request->setForm($form);
65
            }
66
67 27
            return $request;
68 63
        });
69 63
    }
70
}
71