Passed
Push — master ( 555e7c...65be85 )
by
unknown
18:12
created

FileReference   B

Complexity

Total Complexity 47

Size/Duplication

Total Lines 504
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 69
c 1
b 0
f 0
dl 0
loc 504
rs 8.64
wmc 47

39 Methods

Rating   Name   Duplication   Size   Complexity  
A getCreationTime() 0 3 1
A getUid() 0 3 1
A getOriginalFile() 0 3 1
A getName() 0 3 1
A rename() 0 5 1
A getSha1() 0 3 1
A getContents() 0 3 1
A getDescription() 0 3 1
A getType() 0 3 1
A delete() 0 5 1
A getHashedIdentifier() 0 3 1
A getStorage() 0 3 1
A getNameWithoutExtension() 0 3 1
A getForLocalProcessing() 0 3 1
A toArray() 0 4 1
A __sleep() 0 5 1
A getModificationTime() 0 3 1
A setContents() 0 3 1
A getFileObject() 0 6 2
A __construct() 0 8 3
A getPublicUrl() 0 3 1
A getExtension() 0 3 1
A getSize() 0 3 1
A __wakeup() 0 4 1
A getReferenceProperty() 0 6 2
A isMissing() 0 3 1
A getTitle() 0 3 1
A getParentFolder() 0 3 1
A getProperties() 0 15 2
A getLink() 0 3 1
A getIdentifier() 0 3 1
A restoreNonNullValuesCallback() 0 4 3
A getReferenceProperties() 0 3 1
A getCombinedIdentifier() 0 3 1
A getAlternative() 0 3 1
A isIndexed() 0 3 1
A getProperty() 0 7 2
A hasProperty() 0 3 1
A getMimeType() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like FileReference often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use FileReference, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace TYPO3\CMS\Core\Resource;
17
18
use TYPO3\CMS\Core\Utility\ArrayUtility;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21
/**
22
 * Representation of a specific usage of a file with possibilities to override certain
23
 * properties of the original file just for this usage of the file.
24
 *
25
 * It acts as a decorator over the original file in the way that most method calls are
26
 * directly passed along to the original file object.
27
 *
28
 * All file related methods are directly passed along; only meta data functionality is adopted
29
 * in this decorator class to priorities possible overrides for the metadata for this specific usage
30
 * of the file.
31
 */
32
class FileReference implements FileInterface
33
{
34
    /**
35
     * Various properties of the FileReference. Note that these information can be different
36
     * to the ones found in the originalFile.
37
     *
38
     * @var array
39
     */
40
    protected $propertiesOfFileReference;
41
42
    /**
43
     * The file name of this file. It's either the fileName of the original underlying file,
44
     * or the overlay file name supplied by the user for this particular usage (FileReference) of the file.
45
     *
46
     * @var string
47
     */
48
    protected $name;
49
50
    /**
51
     * Reference to the original File object underlying this FileReference.
52
     *
53
     * @var File
54
     */
55
    protected $originalFile;
56
57
    /**
58
     * Properties merged with the parent object (File) if
59
     * the value is not defined (NULL). Thus, FileReference properties act
60
     * as overlays for the defined File properties.
61
     *
62
     * @var array
63
     */
64
    protected $mergedProperties = [];
65
66
    /**
67
     * Constructor for a file in use object. Should normally not be used
68
     * directly, use the corresponding factory methods instead.
69
     *
70
     * @param array $fileReferenceData
71
     * @param ResourceFactory $factory
72
     *
73
     * @throws \InvalidArgumentException
74
     * @throws Exception\FileDoesNotExistException
75
     */
76
    public function __construct(array $fileReferenceData, $factory = null)
77
    {
78
        $this->propertiesOfFileReference = $fileReferenceData;
79
        if (!$fileReferenceData['uid_local']) {
80
            throw new \InvalidArgumentException('Incorrect reference to original file given for FileReference.', 1300098528);
81
        }
82
        $this->originalFile = $this->getFileObject((int)$fileReferenceData['uid_local'], $factory);
83
        $this->name = $fileReferenceData['name'] !== '' ? $fileReferenceData['name'] : $this->originalFile->getName();
84
    }
85
86
    /**
87
     * @param int $uidLocal
88
     * @param ResourceFactory|null $factory
89
     * @return FileInterface
90
     *
91
     * @throws Exception\FileDoesNotExistException
92
     */
93
    private function getFileObject(int $uidLocal, ResourceFactory $factory = null): FileInterface
94
    {
95
        if ($factory === null) {
96
            $factory = GeneralUtility::makeInstance(ResourceFactory::class);
97
        }
98
        return $factory->getFileObject($uidLocal);
99
    }
100
101
    /*******************************
102
     * VARIOUS FILE PROPERTY GETTERS
103
     *******************************/
104
    /**
105
     * Returns true if the given key exists for this file.
106
     *
107
     * @param string $key The property to be looked up
108
     * @return bool
109
     */
110
    public function hasProperty($key)
111
    {
112
        return array_key_exists($key, $this->getProperties());
113
    }
114
115
    /**
116
     * Gets a property, falling back to values of the parent.
117
     *
118
     * @param string $key The property to be looked up
119
     * @return mixed
120
     * @throws \InvalidArgumentException
121
     */
122
    public function getProperty($key)
123
    {
124
        if (!$this->hasProperty($key)) {
125
            throw new \InvalidArgumentException('Property "' . $key . '" was not found in file reference or original file.', 1314226805);
126
        }
127
        $properties = $this->getProperties();
128
        return $properties[$key];
129
    }
130
131
    /**
132
     * Gets a property of the file reference.
133
     *
134
     * @param string $key The property to be looked up
135
     * @return mixed
136
     * @throws \InvalidArgumentException
137
     */
138
    public function getReferenceProperty($key)
139
    {
140
        if (!array_key_exists($key, $this->propertiesOfFileReference)) {
141
            throw new \InvalidArgumentException('Property "' . $key . '" of file reference was not found.', 1360684914);
142
        }
143
        return $this->propertiesOfFileReference[$key];
144
    }
145
146
    /**
147
     * Gets all properties, falling back to values of the parent.
148
     *
149
     * @return array
150
     */
151
    public function getProperties()
152
    {
153
        if (empty($this->mergedProperties)) {
154
            $this->mergedProperties = $this->propertiesOfFileReference;
155
            ArrayUtility::mergeRecursiveWithOverrule(
156
                $this->mergedProperties,
157
                $this->originalFile->getProperties(),
158
                true,
159
                true,
160
                false
161
            );
162
            array_walk($this->mergedProperties, [$this, 'restoreNonNullValuesCallback']);
163
        }
164
165
        return $this->mergedProperties;
166
    }
167
168
    /**
169
     * Callback to handle the NULL value feature
170
     *
171
     * @param mixed $value
172
     * @param mixed $key
173
     */
174
    protected function restoreNonNullValuesCallback(&$value, $key)
175
    {
176
        if (array_key_exists($key, $this->propertiesOfFileReference) && $this->propertiesOfFileReference[$key] !== null) {
177
            $value = $this->propertiesOfFileReference[$key];
178
        }
179
    }
180
181
    /**
182
     * Gets all properties of the file reference.
183
     *
184
     * @return array
185
     */
186
    public function getReferenceProperties()
187
    {
188
        return $this->propertiesOfFileReference;
189
    }
190
191
    /**
192
     * Returns the name of this file
193
     *
194
     * @return string
195
     */
196
    public function getName()
197
    {
198
        return $this->originalFile->getName();
199
    }
200
201
    /**
202
     * Returns the title text to this image
203
     *
204
     * @todo Possibly move this to the image domain object instead
205
     *
206
     * @return string
207
     */
208
    public function getTitle()
209
    {
210
        return $this->getProperty('title');
211
    }
212
213
    /**
214
     * Returns the alternative text to this image
215
     *
216
     * @todo Possibly move this to the image domain object instead
217
     *
218
     * @return string
219
     */
220
    public function getAlternative()
221
    {
222
        return $this->getProperty('alternative');
223
    }
224
225
    /**
226
     * Returns the description text to this file
227
     *
228
     * @todo Possibly move this to the image domain object instead
229
     *
230
     * @return string
231
     */
232
    public function getDescription()
233
    {
234
        return $this->getProperty('description');
235
    }
236
237
    /**
238
     * Returns the link that should be active when clicking on this image
239
     *
240
     * @todo Move this to the image domain object instead
241
     *
242
     * @return string
243
     */
244
    public function getLink()
245
    {
246
        return $this->propertiesOfFileReference['link'];
247
    }
248
249
    /**
250
     * Returns the uid of this File In Use
251
     *
252
     * @return int
253
     */
254
    public function getUid()
255
    {
256
        return (int)$this->propertiesOfFileReference['uid'];
257
    }
258
259
    /**
260
     * Returns the size of this file
261
     *
262
     * @return int
263
     */
264
    public function getSize()
265
    {
266
        return (int)$this->originalFile->getSize();
267
    }
268
269
    /**
270
     * Returns the Sha1 of this file
271
     *
272
     * @return string
273
     */
274
    public function getSha1()
275
    {
276
        return $this->originalFile->getSha1();
277
    }
278
279
    /**
280
     * Get the file extension of this file
281
     *
282
     * @return string The file extension
283
     */
284
    public function getExtension()
285
    {
286
        return $this->originalFile->getExtension();
287
    }
288
289
    /**
290
     * Returns the basename (the name without extension) of this file.
291
     *
292
     * @return string
293
     */
294
    public function getNameWithoutExtension()
295
    {
296
        return $this->originalFile->getNameWithoutExtension();
297
    }
298
299
    /**
300
     * Get the MIME type of this file
301
     *
302
     * @return string mime type
303
     */
304
    public function getMimeType()
305
    {
306
        return $this->originalFile->getMimeType();
307
    }
308
309
    /**
310
     * Returns the modification time of the file as Unix timestamp
311
     *
312
     * @return int
313
     */
314
    public function getModificationTime()
315
    {
316
        return (int)$this->originalFile->getModificationTime();
317
    }
318
319
    /**
320
     * Returns the creation time of the file as Unix timestamp
321
     *
322
     * @return int
323
     */
324
    public function getCreationTime()
325
    {
326
        return (int)$this->originalFile->getCreationTime();
327
    }
328
329
    /**
330
     * Returns the fileType of this file
331
     *
332
     * @return int $fileType
333
     */
334
    public function getType()
335
    {
336
        return (int)$this->originalFile->getType();
337
    }
338
339
    /**
340
     * Check if file is marked as missing by indexer
341
     *
342
     * @return bool
343
     */
344
    public function isMissing()
345
    {
346
        return (bool)$this->originalFile->getProperty('missing');
347
    }
348
349
    /******************
350
     * CONTENTS RELATED
351
     ******************/
352
    /**
353
     * Get the contents of this file
354
     *
355
     * @return string File contents
356
     */
357
    public function getContents()
358
    {
359
        return $this->originalFile->getContents();
360
    }
361
362
    /**
363
     * Replace the current file contents with the given string
364
     *
365
     * @param string $contents The contents to write to the file.
366
     * @return File The file object (allows chaining).
367
     */
368
    public function setContents($contents)
369
    {
370
        return $this->originalFile->setContents($contents);
371
    }
372
373
    /****************************************
374
     * STORAGE AND MANAGEMENT RELATED METHODS
375
     ****************************************/
376
    /**
377
     * Get the storage the original file is located in
378
     *
379
     * @return ResourceStorage
380
     */
381
    public function getStorage()
382
    {
383
        return $this->originalFile->getStorage();
384
    }
385
386
    /**
387
     * Returns the identifier of the underlying original file
388
     *
389
     * @return string
390
     */
391
    public function getIdentifier()
392
    {
393
        return $this->originalFile->getIdentifier();
394
    }
395
396
    /**
397
     * Returns a combined identifier of the underlying original file
398
     *
399
     * @return string Combined storage and file identifier, e.g. StorageUID:path/and/fileName.png
400
     */
401
    public function getCombinedIdentifier()
402
    {
403
        return $this->originalFile->getCombinedIdentifier();
404
    }
405
406
    /**
407
     * Deletes only this particular FileReference from the persistence layer
408
     * (database table sys_file_reference) but leaves the original file untouched.
409
     *
410
     * @throws \BadMethodCallException
411
     */
412
    public function delete()
413
    {
414
        // @todo Implement this function. This should only delete the
415
        // FileReference (sys_file_reference) record, not the file itself.
416
        throw new \BadMethodCallException('Function not implemented FileReference::delete().', 1333754461);
417
        //return $this->fileRepository->removeUsageRecord($this);
418
    }
419
420
    /**
421
     * Renames the fileName in this particular usage.
422
     *
423
     * @param string $newName The new name
424
     * @param string $conflictMode
425
     */
426
    public function rename($newName, $conflictMode = DuplicationBehavior::RENAME)
427
    {
428
        // @todo Implement this function. This should only rename the
429
        // FileReference (sys_file_reference) record, not the file itself.
430
        throw new \BadMethodCallException('Function not implemented FileReference::rename().', 1333754473);
431
        //return $this->fileRepository->renameUsageRecord($this, $newName);
432
    }
433
434
    /*****************
435
     * SPECIAL METHODS
436
     *****************/
437
    /**
438
     * Returns a publicly accessible URL for this file
439
     *
440
     * WARNING: Access to the file may be restricted by further means, e.g.
441
     * some web-based authentication. You have to take care of this yourself.
442
     *
443
     * @param bool  $relativeToCurrentScript   Determines whether the URL returned should be relative to the current script, in case it is relative at all (only for the LocalDriver)
444
     * @return string|null NULL if file is missing or deleted, the generated url otherwise
445
     */
446
    public function getPublicUrl($relativeToCurrentScript = false)
447
    {
448
        return $this->originalFile->getPublicUrl($relativeToCurrentScript);
449
    }
450
451
    /**
452
     * Returns TRUE if this file is indexed.
453
     * This is always true for FileReference objects, as they rely on a
454
     * sys_file_reference record to be present, which in turn can only exist if
455
     * the original file is indexed.
456
     *
457
     * @return bool
458
     */
459
    public function isIndexed()
460
    {
461
        return true;
462
    }
463
464
    /**
465
     * Returns a path to a local version of this file to process it locally (e.g. with some system tool).
466
     * If the file is normally located on a remote storages, this creates a local copy.
467
     * If the file is already on the local system, this only makes a new copy if $writable is set to TRUE.
468
     *
469
     * @param bool $writable Set this to FALSE if you only want to do read operations on the file.
470
     * @return string
471
     */
472
    public function getForLocalProcessing($writable = true)
473
    {
474
        return $this->originalFile->getForLocalProcessing($writable);
475
    }
476
477
    /**
478
     * Returns an array representation of the file.
479
     * (This is used by the generic listing module vidi when displaying file records.)
480
     *
481
     * @return array Array of main data of the file. Don't rely on all data to be present here, it's just a selection of the most relevant information.
482
     */
483
    public function toArray()
484
    {
485
        $array = array_merge($this->originalFile->toArray(), $this->propertiesOfFileReference);
486
        return $array;
487
    }
488
489
    /**
490
     * Gets the original file being referenced.
491
     *
492
     * @return File
493
     */
494
    public function getOriginalFile()
495
    {
496
        return $this->originalFile;
497
    }
498
499
    /**
500
     * Get hashed identifier
501
     *
502
     * @return string
503
     */
504
    public function getHashedIdentifier()
505
    {
506
        return $this->getStorage()->hashFileIdentifier($this->getIdentifier());
507
    }
508
509
    /**
510
     * Returns the parent folder.
511
     *
512
     * @return FolderInterface
513
     */
514
    public function getParentFolder()
515
    {
516
        return $this->originalFile->getParentFolder();
517
    }
518
519
    /**
520
     * Avoids exporting original file object which contains
521
     * singleton dependencies that must not be serialized.
522
     *
523
     * @return string[]
524
     */
525
    public function __sleep(): array
526
    {
527
        $keys = get_object_vars($this);
528
        unset($keys['originalFile']);
529
        return array_keys($keys);
530
    }
531
532
    public function __wakeup(): void
533
    {
534
        $this->originalFile = $this->getFileObject(
535
            (int)$this->propertiesOfFileReference['uid_local']
536
        );
537
    }
538
}
539