MediaManagerServiceProvider::createParsers()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 11
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Lincable\Providers;
4
5
use Lincable\UrlConf;
6
use Lincable\UrlGenerator;
7
use Lincable\MediaManager;
8
use Lincable\Http\FileRequest;
9
use Illuminate\Support\ServiceProvider;
10
use Lincable\Contracts\Compilers\Compiler;
11
12
class MediaManagerServiceProvider extends ServiceProvider
13
{
14
    /**
15
     * Indicates if loading of the provider is deferred.
16
     *
17
     * @var bool
18
     */
19
    protected $defer = true;
20
21
    /**
22
     * Bootstrap any application services.
23
     *
24
     * @return void
25
     */
26 93
    public function boot()
27
    {
28 93
        $configPath = __DIR__.'/../../../config/lincable.php';
29
30 93
        $this->publishes([
31 93
            $configPath => config_path('lincable.php'),
32
        ]);
33
34 93
        $this->mergeConfigFrom($configPath, 'lincable');
35
36 93
        $this->app['events']->subscribe($this->app['config']['lincable.upload_subscriber']);
37 93
    }
38
39
    /**
40
     * Register bindings in the container.
41
     *
42
     * @return void
43
     */
44 93
    public function register()
45
    {
46 93
        $this->registerCompiler();
47 93
        $this->registerUrlGenerator();
48 93
        $this->registerMediaManager();
49 93
    }
50
51
    /**
52
     * Get the services provided by the provider.
53
     *
54
     * @return array
55
     */
56 1
    public function provides()
57
    {
58 1
        return [MediaManager::class];
59
    }
60
61
    /**
62
     * Register the media manager singleton.
63
     *
64
     * @return void
65
     */
66 93
    public function registerMediaManager()
67
    {
68
        $this->app->singleton(MediaManager::class, function ($app) {
69 39
            return new MediaManager($app, $app->make(UrlGenerator::class));
70 93
        });
71 93
    }
72
73
    /**
74
     * Regiter the compiler default implementation.
75
     *
76
     * @return void
77
     */
78 93
    protected function registerCompiler()
79
    {
80 93
        $this->app->bind(Compiler::class, \Lincable\UrlCompiler::class);
81 93
    }
82
83
    /**
84
     * Create a new url conf with an optional root name.
85
     *
86
     * @return \Licable\UrlConf
0 ignored issues
show
Bug introduced by
The type Licable\UrlConf was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
87
     */
88 40
    protected function createUrlConf()
89
    {
90
        // Create the url conf class.
91 40
        $urlConf = new UrlConf(config('lincable.models.namespace', ''));
92
93 40
        $root = config('lincable.root', '');
94 40
        $urls = config('lincable.urls');
95
        
96
        // Determine wheter a root is present for each url and trim 
97
        // backslashs from right part of string.
98 40
        if ($root !== '') {
99 40
            $root = str_finish(ltrim($root, '/'), '/');
100
        }
101
102
        // Add the new url configuration.
103 40
        foreach ($urls as $model => $url) {
104 30
            $urlConf->push($model, $root.ltrim($url, '/'));
105
        }
106
107 39
        return $urlConf;
108
    }
109
110
    /**
111
     * Register the url generator singleton. 
112
     *
113
     * @return void
114
     */
115 93
    public function registerUrlGenerator()
116
    {
117
        $this->app->singleton(UrlGenerator::class, function ($app) {
118 40
            return new UrlGenerator(
119 40
                $app->make(Compiler::class), 
120 40
                $this->createParsers(), 
121 40
                $this->createUrlConf()
122
            );
123 93
        });
124 93
    }
125
126
    /**
127
     * Create the registered parsers and respective formatters.
128
     * 
129
     * @return mixed
130
     */
131 40
    protected function createParsers() 
132
    {
133
        // Create the default parsers from config.
134 40
        $registeredParsers = collect(array_merge(
135 40
            config('lincable.default_parsers', []),
136 40
            config('lincable.parsers', [])
137
        ));
138
139
        return $registeredParsers->map(function ($formatters, $parser) {
140
            return tap($this->app->make($parser), function ($parser) use ($formatters) {
141 39
                $parser->addFormatters($formatters);
142 39
            });
143 40
        });
144
    }
145
}
146