Completed
Push — 1.1 ( d166b0...e7f438 )
by Patrick
11:31 queued 07:46
created

Uploadable   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 310
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 17
c 3
b 0
f 0
lcom 1
cbo 4
dl 0
loc 310
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A enable() 0 8 1
A allowOverwrite() 0 6 1
A appendNumber() 0 6 1
A path() 0 6 1
A pathMethod() 0 6 1
A callback() 0 6 1
A alphanumericFilename() 0 6 1
A sha1Filename() 0 6 1
A customFilename() 0 6 1
A maxSize() 0 6 1
A allow() 0 9 2
A disallow() 0 9 2
A build() 0 8 1
B getConfiguration() 0 24 1
1
<?php
2
3
namespace LaravelDoctrine\Fluent\Extensions\Gedmo;
4
5
use Gedmo\Uploadable\Mapping\Driver\Fluent as UploadableDriver;
6
use Gedmo\Uploadable\Mapping\Validator;
7
use LaravelDoctrine\Fluent\Buildable;
8
use LaravelDoctrine\Fluent\Builders\Builder;
9
use LaravelDoctrine\Fluent\Builders\Delay;
10
use LaravelDoctrine\Fluent\Extensions\ExtensibleClassMetadata;
11
use LaravelDoctrine\Fluent\Extensions\Extension;
12
13
class Uploadable implements Buildable, Delay, Extension
14
{
15
    const MACRO_METHOD = 'uploadable';
16
17
    /**
18
     * @var bool
19
     */
20
    private $allowOverwrite = false;
21
22
    /**
23
     * @var bool
24
     */
25
    private $appendNumber = false;
26
27
    /**
28
     * @var string
29
     */
30
    private $path = '';
31
32
    /**
33
     * @var string
34
     */
35
    private $pathMethod = '';
36
37
    /**
38
     * @var string
39
     */
40
    private $callback = '';
41
42
    /**
43
     * @var string
44
     */
45
    private $filenameGenerator = Validator::FILENAME_GENERATOR_NONE;
46
47
    /**
48
     * @var float
49
     */
50
    private $maxSize = 0;
51
52
    /**
53
     * @var string
54
     */
55
    private $allowedTypes = '';
56
57
    /**
58
     * @var string
59
     */
60
    private $disallowedTypes = '';
61
62
    /**
63
     * @var ExtensibleClassMetadata
64
     */
65
    private $classMetadata;
66
67
    /**
68
     * @param ExtensibleClassMetadata $classMetadata
69
     */
70
    public function __construct(ExtensibleClassMetadata $classMetadata)
71
    {
72
        $this->classMetadata = $classMetadata;
73
    }
74
75
    /**
76
     * Enable the Uploadable extension.
77
     *
78
     * @return void
79
     */
80
    public static function enable()
81
    {
82
        Builder::macro(self::MACRO_METHOD, function (Builder $builder) {
83
            return new static($builder->getClassMetadata());
0 ignored issues
show
Compatibility introduced by
$builder->getClassMetadata() of type object<Doctrine\ORM\Mapping\ClassMetadata> is not a sub-type of object<LaravelDoctrine\F...xtensibleClassMetadata>. It seems like you assume a child class of the class Doctrine\ORM\Mapping\ClassMetadata to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
84
        });
85
86
        UploadableFile::enable();
87
    }
88
89
    /**
90
     * If this option is true, it will overwrite a file if it already exists. If you set "false",
91
     * an exception will be thrown.
92
     *
93
     * Default: false
94
     *
95
     * @return Uploadable
96
     */
97
    public function allowOverwrite()
98
    {
99
        $this->allowOverwrite = true;
100
101
        return $this;
102
    }
103
104
    /**
105
     * If the file already exists and "allowOverwrite" is false, append a number to the filename.
106
     *
107
     * Example: if you're uploading a file named "test.txt", if the file already exists and this option
108
     * is true, the extension will modify the name of the uploaded file to "test-1.txt", where "1" could
109
     * be any number. The extension will check if the file exists until it finds a filename with a number
110
     * as its postfix that is not used. If you use a filename generator and this option is true, it will
111
     * append a number to the filename anyway if a file with the same name already exists.
112
     *
113
     * Default value: false
114
     *
115
     * @return Uploadable
116
     */
117
    public function appendNumber()
118
    {
119
        $this->appendNumber = true;
120
121
        return $this;
122
    }
123
124
    /**
125
     * The path where the files represented by this entity will be moved. Path can be set in other ways:
126
     * From the listener or from a method.
127
     *
128
     * Default: "".
129
     *
130
     * @param string $path
131
     *
132
     * @return Uploadable
133
     */
134
    public function path($path)
135
    {
136
        $this->path = $path;
137
138
        return $this;
139
    }
140
141
    /**
142
     * Similar to "path", this represents the name of a method on the entity that will return the path
143
     * where the files represented by this entity will be moved. This is useful in several cases. For
144
     * example, you can set specific paths for specific entities, or you can get the path from other
145
     * sources (like a framework configuration) instead of hard-coding it in the entity.
146
     *
147
     * As first argument this method takes default path, so you can return a path relative to the default.
148
     *
149
     * Default: "".
150
     *
151
     * @param string $methodName
152
     *
153
     * @return Uploadable
154
     */
155
    public function pathMethod($methodName)
156
    {
157
        $this->pathMethod = $methodName;
158
159
        return $this;
160
    }
161
162
    /**
163
     * Allows you to set a method name. If set, the method will be called after the file is moved.
164
     *
165
     * This method will receive an array with information about the uploaded file, which includes the
166
     * following keys:
167
     *
168
     * <ul>
169
     * <li>fileName: The filename.</li>
170
     * <li>fileExtension: The extension of the file (including the dot). Example: .jpg</li>
171
     * <li>fileWithoutExt: The filename without the extension.</li>
172
     * <li>filePath: The file path. Example: /my/path/filename.jpg</li>
173
     * <li>fileMimeType: The mime-type of the file. Example: text/plain.</li>
174
     * <li>fileSize: Size of the file in bytes. Example: 140000.</li>
175
     * </ul>
176
     *
177
     * Default value: "".
178
     *
179
     * @param string $callback
180
     *
181
     * @return Uploadable
182
     */
183
    public function callback($callback)
184
    {
185
        $this->callback = $callback;
186
187
        return $this;
188
    }
189
190
    /**
191
     * Normalizes the filename, leaving only alphanumeric characters in the filename, and replacing
192
     * anything else with a "-".
193
     *
194
     * @return Uploadable
195
     */
196
    public function alphanumericFilename()
197
    {
198
        $this->filenameGenerator = Validator::FILENAME_GENERATOR_ALPHANUMERIC;
199
200
        return $this;
201
    }
202
203
    /**
204
     * Generates a sha1 filename for the file.
205
     *
206
     * @return Uploadable
207
     */
208
    public function sha1Filename()
209
    {
210
        $this->filenameGenerator = Validator::FILENAME_GENERATOR_SHA1;
211
212
        return $this;
213
    }
214
215
    /**
216
     * Set a custom FilenameGenerator class. This class must implement
217
     * `Gedmo\Uploadable\FilenameGenerator\FilenameGeneratorInterface`.
218
     *
219
     * @param string $className
220
     *
221
     * @return Uploadable
222
     */
223
    public function customFilename($className)
224
    {
225
        $this->filenameGenerator = $className;
226
227
        return $this;
228
    }
229
230
    /**
231
     * Set a maximum size for the file, in bytes. If file size exceeds the value set in this
232
     * configuration, `UploadableMaxSizeException` will be thrown.
233
     *
234
     * Default value: 0, meaning that no size validation will occur.
235
     *
236
     * @param float $bytes
237
     *
238
     * @return Uploadable
239
     */
240
    public function maxSize($bytes)
241
    {
242
        $this->maxSize = $bytes;
243
244
        return $this;
245
    }
246
247
    /**
248
     * Allow only specific types.
249
     *
250
     * @param array|string ...$type can be an array or multiple string parameters
251
     *
252
     * @return Uploadable
253
     */
254
    public function allow($type)
255
    {
256
        if (!is_array($type)) {
257
            $type = func_get_args();
258
        }
259
        $this->allowedTypes = implode(',', $type);
260
261
        return $this;
262
    }
263
264
    /**
265
     * Disallow specific types
266
     *
267
     * @param array|string ...$type can be an array or multiple string parameters
268
     *
269
     * @return Uploadable
270
     */
271
    public function disallow($type)
272
    {
273
        if (!is_array($type)) {
274
            $type = func_get_args();
275
        }
276
        $this->disallowedTypes = implode(',', $type);
277
278
        return $this;
279
    }
280
281
    /**
282
     * Execute the build process
283
     */
284
    public function build()
285
    {
286
        $config = $this->getConfiguration();
287
288
        Validator::validateConfiguration($this->classMetadata, $config);
289
290
        $this->classMetadata->addExtension(UploadableDriver::EXTENSION_NAME, $config);
291
    }
292
293
    /**
294
     * Build the configuration, based on defaults, current extension configuration and accumulated parameters.
295
     *
296
     * @return array
297
     */
298
    private function getConfiguration()
299
    {
300
        return array_merge(
301
            [
302
                'fileMimeTypeField' => false,
303
                'fileNameField'     => false,
304
                'filePathField'     => false,
305
                'fileSizeField'     => false,
306
            ],
307
            $this->classMetadata->getExtension(UploadableDriver::EXTENSION_NAME),
308
            [
309
                'uploadable'        => true,
310
                'allowOverwrite'    => $this->allowOverwrite,
311
                'appendNumber'      => $this->appendNumber,
312
                'path'              => $this->path,
313
                'pathMethod'        => $this->pathMethod,
314
                'callback'          => $this->callback,
315
                'filenameGenerator' => $this->filenameGenerator,
316
                'maxSize'           => (double)$this->maxSize,
317
                'allowedTypes'      => $this->allowedTypes,
318
                'disallowedTypes'   => $this->disallowedTypes,
319
            ]
320
        );
321
    }
322
}
323