Test Failed
Push — dev ( 1dc13c...3f81c0 )
by Yan
02:20
created

MediaManager::buildUrlGenerator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Lincable;
4
5
use Lincable\Parsers\Parser;
6
use Illuminate\Contracts\Compilers\Compiler;
0 ignored issues
show
Bug introduced by
The type Illuminate\Contracts\Compilers\Compiler 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...
7
use Illuminate\Contracts\Container\Container;
8
9
class MediaManager
10
{
11
    /**
12
     * The container implementation.
13
     * 
14
     * @var \Illuminate\Contracts\Container\Container
15
     */
16
    protected $app;
17
18
    /**
19
     * The compiler implementation.
20
     * 
21
     * @var \Illuminate\Contracts\Compilers\Compiler
22
     */
23
    protected $compiler;
24
25
    /**
26
     * The model url configuration.
27
     * 
28
     * @var \Lincable\UrlConf
29
     */
30
    protected $urlConf;
31
32
    /**
33
     * The disk for file storage.
34
     * 
35
     * @var \Illuminate\Contracts\Filesystem\Filesystem
36
     */
37
    protected $disk;
38
39
    /**
40
     * The collection with the url parsers.
41
     * 
42
     * @var \Illuminate\Support\Collection
43
     */
44
    protected $parsers;
45
46
    /**
47
     * Create a new class instance.
48
     * 
49
     * @param  \Illuminate\Contracts\Container\Container $app
50
     * @return void
51
     */
52
    public function __construct(Container $app, Compiler $compiler)
53
    {
54
        $this->app = $app;
55
        $this->compiler = $compiler;
56
        $this->parsers = collect();
57
        $this->readConfig();
58
    }
59
60
    /**
61
     * Set the media url compiler.
62
     * 
63
     * @param  \Illuminate\Contracts\Compilers\Compiler $compiler
64
     * @return this
65
     */
66
    public function setCompiler(Compiler $compiler)
67
    {
68
        $this->compiler = $compiler;
69
    }
70
71
    /**
72
     * Return the compiler class.
73
     * 
74
     * @return \Illuminate\Contracts\Compilers\Compiler
75
     */
76
    public function getCompiler()
77
    {
78
        return $this->compiler;
79
    }
80
81
    /**
82
     * Set the new disk to use.
83
     * 
84
     * @param  string $disk
85
     * @return this
86
     */
87
    public function setDisk(string $disk)
88
    {
89
        $this->disk = $this->app['filesystem']->disk($disk);
90
        return $this;
91
    }
92
93
    /**
94
     * Set a root path for the urls.
95
     * 
96
     * @param  string|null $root
97
     * @return this
98
     */
99
    public function setRoot(string $root = null)
100
    {
101
        $this->urlConf = $this->createUrlConfWithRoot($root);
102
        return $this;
103
    }
104
105
    /**
106
     * Add a new parser to the manager.
107
     * 
108
     * @param  mixed $parser
109
     * @param  mixed $formatters
110
     * @return this
111
     */
112
    public function addParser($parser, $formatters = [])
113
    {
114
        if (is_string($parser)) {
115
116
            // Resolve the parser instance.
117
            $parser = $this->app->make($parser);
118
        }
119
120
        if (! $parser instanceof Parser) {
121
            $type = gettype($parser);
122
123
            // Force the parser to be a Parser class.
124
            throw new Exception("Parser must be a Lincable\Parsers\Parser, [{$type}] given.");
125
        }
126
127
        $this->parsers->push($parser->addFormatters($formatters));
128
        return $this;
129
    }
130
131
    /**
132
     * Return a url generator instance with the manager configuration.
133
     * 
134
     * @return \Lincable\UrlGenerator
135
     */
136
    public function buildUrlGenerator()
137
    {
138
        return new UrlGenerator($this->compiler, $this->parsers, $this->urlConf);
139
    }
140
141
    /**
142
     * Read the configuration from container.
143
     * 
144
     * @return void
145
     */
146
    protected function readConfig() 
147
    {
148
        $this->setRoot($this->getConfig('root'));
149
        
150
        // Create the default parsers from config.
151
        $parsers = array_merge(
152
            $this->getConfig('default_parsers', []),
153
            $this->getConfig('parsers', [])
154
        );
155
        
156
        foreach($parsers as $parser => $formatters) {
157
            $this->addParser($parser, $formatters);
158
        }
159
        
160
        // Create a new disk filesystem.
161
        $this->disk = $this->app['filesystem']->disk($this->getConfig('disk'));
162
    }
163
164
    /**
165
     * Return the configuration value.
166
     * 
167
     * @param  string $key
168
     * @param  mixed  $default
169
     * @return mixed
170
     */
171
    protected function getConfig(string $key, $default = null) 
172
    {
173
        return $this->app["config"]["lincable.{$key}"] ?: $default;
174
    }
175
176
    /**
177
     * Create a new url conf with a root url.
178
     * 
179
     * @param  string|null $root
180
     * @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...
181
     */
182
    protected function createUrlConfWithRoot(string $root = null) 
183
    {
184
        // Add the url configuration.  
185
        $urls = $this->getConfig('urls', []);
186
        $urlConf = new UrlConf($this->getConfig('models.namespace'));
187
         
188
        foreach($urls as $model => $url) {
189
            $url = str_start($url, '/');
190
            
191
            if ($root && $root !== '') {
192
193
                // Prepend the root url on the url.
194
                $url = rtrim('\/', $root).$url;
195
            }
196
197
            // Push the new mode url configuration.  
198
            $urlConf->push($model, $url);
199
        }
200
201
        return $urlConf;
202
    }
203
}