Completed
Push — dev ( 4e6f9a...6e9c7d )
by Yan
04:26 queued 02:24
created

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