FractalServiceProvider   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 56
c 0
b 0
f 0
wmc 6
lcom 1
cbo 3
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 7 1
A registerBindings() 0 12 1
A registerFacades() 0 6 2
A configureService() 0 6 2
1
<?php
2
3
namespace Nord\Lumen\Fractal;
4
5
use Illuminate\Contracts\Config\Repository as ConfigRepository;
6
use Illuminate\Contracts\Container\Container;
7
use Illuminate\Support\ServiceProvider;
8
use Laravel\Lumen\Application;
9
use Nord\Lumen\Fractal\Contracts\FractalService as FractalServiceContract;
10
11
class FractalServiceProvider extends ServiceProvider
12
{
13
    const CONFIG_KEY = 'fractal';
14
15
    /**
16
     * @inheritdoc
17
     */
18
    public function register()
19
    {
20
        $this->app->configure(self::CONFIG_KEY);
0 ignored issues
show
Bug introduced by
The method configure() does not exist on Illuminate\Contracts\Foundation\Application. Did you maybe mean registerConfiguredProviders()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
21
22
        $this->registerBindings($this->app, $this->app['config']);
23
        $this->registerFacades();
24
    }
25
26
27
    /**
28
     * @param Container        $container
29
     * @param ConfigRepository $config
30
     */
31
    protected function registerBindings(Container $container, ConfigRepository $config)
32
    {
33
        $container->singleton(FractalService::class, function () use ($config) {
34
            $fractal = new FractalService();
35
36
            $this->configureService($fractal, $config[self::CONFIG_KEY]);
37
38
            return $fractal;
39
        });
40
41
        $container->alias(FractalService::class, FractalServiceContract::class);
42
    }
43
44
45
    /**
46
     *
47
     */
48
    protected function registerFacades()
49
    {
50
        if (!class_exists('Fractal')) {
51
            class_alias(FractalFacade::class, 'Fractal');
52
        }
53
    }
54
55
56
    /**
57
     * @param FractalService $service
58
     * @param array          $config
59
     */
60
    protected function configureService(FractalService $service, array $config)
61
    {
62
        if (!empty($config['default_serializer'])) {
63
            $service->setDefaultSerializer(new $config['default_serializer']);
64
        }
65
    }
66
}
67