FolderItemImage   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 55
dl 0
loc 141
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getLanguageService() 0 3 1
A __construct() 0 32 1
A jsonSerialize() 0 17 1
1
<?php
2
declare(strict_types = 1);
3
4
/*
5
 * This file is part of the package typo3/cms-digital-asset-management.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE file that was distributed with this source code.
9
 */
10
11
namespace TYPO3\CMS\DigitalAssetManagement\Entity;
12
13
use TYPO3\CMS\Backend\Routing\UriBuilder;
14
use TYPO3\CMS\Backend\Utility\BackendUtility;
15
use TYPO3\CMS\Core\Localization\LanguageService;
16
use TYPO3\CMS\Core\Resource\File;
17
use TYPO3\CMS\Core\Utility\GeneralUtility;
18
use TYPO3\CMS\Filelist\Configuration\ThumbnailConfiguration;
19
20
/**
21
 * Immutable image object (a file recognized as image by TYPO3), used by getFolderItemsAction().
22
 *
23
 * @see FolderItemFile
24
 */
25
class FolderItemImage implements \JsonSerializable
26
{
27
    /**
28
     * @var string Always set to "image"
29
     */
30
    protected $type = 'image';
31
32
    /**
33
     * @var int FAL identifier, eg. "42:/path/to/file"
34
     */
35
    protected $identifier;
36
37
    /**
38
     * @var string Full file name without path, eg. "myFile.txt"
39
     */
40
    protected $name;
41
42
    /**
43
     * @var int File modification timestamp, eg. 1553705583
44
     */
45
    protected $mtime;
46
47
    /**
48
     * @var string $mtime formatted for display, eg. "30.02.2042"
49
     */
50
    protected $mtimeDisplay;
51
52
    /**
53
     * @var FilePermission Entity representing access permissions
54
     */
55
    protected $permissions;
56
57
    /**
58
     * @var string File extension / ending, eg. "txt"
59
     */
60
    protected $extension;
61
62
    /**
63
     * @var int File size in bytes, eg. 12345
64
     */
65
    protected $size;
66
67
    /**
68
     * @var string $size formatted for display, eg. "42KB"
69
     */
70
    protected $sizeDisplay;
71
72
    /**
73
     * @var array @TODO Details of translated meta data of this file, to be defined
74
     */
75
    protected $translations;
76
77
    /**
78
     * @var int Number of other records referencing this file, eg. 5
79
     */
80
    protected $references;
81
82
    /**
83
     * Url to edit meta data of file, eg.
84
     * "/typo3/index.php?route=/record/edit&token=06db97b96d1e73ec14bf5d1f35604b20843d54f4&edit[sys_file_metadata][108]=edit"
85
     *
86
     * @var string
87
     */
88
    protected $editMetaUrl;
89
90
    /**
91
     * Url to get thumbnail of image, eg.
92
     * "/typo3/index.php?route=/thumbnails&token=2ef7aa2f65b713771165b3dba9fb2f2aee6d6005&parameters=..."
93
     *
94
     * @var string
95
     */
96
    protected $thumbnailUrl;
97
98
    /**
99
     * @var string
100
     */
101
    protected $publicUrl;
102
103
    /**
104
     * @param File $image
105
     */
106
    public function __construct(File $image)
107
    {
108
        $this->identifier = $image->getCombinedIdentifier();
0 ignored issues
show
Documentation Bug introduced by
The property $identifier was declared of type integer, but $image->getCombinedIdentifier() is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
109
        $this->name = $image->getName();
110
        $this->mtime = $image->getModificationTime();
111
        $this->mtimeDisplay = BackendUtility::date($this->mtime) ?? '';
112
        $this->permissions = new FilePermission($image);
113
        $this->extension = $image->getExtension();
114
        $this->size = $image->getSize();
115
        $this->sizeDisplay = GeneralUtility::formatSize(
116
            $this->size,
117
            $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_common.xlf:byteSizeUnits')
118
        );
119
        $this->translations = [];
120
        $this->references = (int)BackendUtility::referenceCount('sys_file', $image->getUid());
121
        $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
122
        $urlParameters = [
123
            'edit' => [
124
                'sys_file_metadata' => [
125
                    $image->getMetaData()['uid'] => 'edit',
0 ignored issues
show
Bug introduced by
The method getMetaData() does not exist on TYPO3\CMS\Core\Resource\File. Did you maybe mean _getMetaData()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

125
                    $image->/** @scrutinizer ignore-call */ 
126
                            getMetaData()['uid'] => 'edit',

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
126
                ]
127
            ],
128
            'returnUrl' => (string)$uriBuilder->buildUriFromRoute('file_DigitalAssetManagement'),
129
        ];
130
        $this->editMetaUrl = (string)$uriBuilder->buildUriFromRoute('record_edit', $urlParameters);
131
        // @todo: This use an internal class of ext:filelist
132
        $thumbnailConfiguration = GeneralUtility::makeInstance(ThumbnailConfiguration::class);
133
        $this->thumbnailUrl = BackendUtility::getThumbnailUrl($image->getUid(), [
0 ignored issues
show
Bug introduced by
The method getThumbnailUrl() does not exist on TYPO3\CMS\Backend\Utility\BackendUtility. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

133
        /** @scrutinizer ignore-call */ 
134
        $this->thumbnailUrl = BackendUtility::getThumbnailUrl($image->getUid(), [

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
134
            'width' => $thumbnailConfiguration->getWidth(),
135
            'height' => $thumbnailConfiguration->getHeight()
136
        ]);
137
        $this->publicUrl = $image->getPublicUrl();
138
    }
139
140
    public function jsonSerialize()
141
    {
142
        return [
143
            'type' => $this->type,
144
            'identifier' => $this->identifier,
145
            'name' => $this->name,
146
            'mtime' => $this->mtime,
147
            'mtimeDisplay' => $this->mtimeDisplay,
148
            'permissions' => $this->permissions,
149
            'extension' => $this->extension,
150
            'size' => $this->size,
151
            'sizeDisplay' => $this->sizeDisplay,
152
            'translations' => $this->translations,
153
            'references' => $this->references,
154
            'editMetaUrl' => $this->editMetaUrl,
155
            'thumbnailUrl' => $this->thumbnailUrl,
156
            'publicUrl' => $this->publicUrl,
157
        ];
158
    }
159
160
    /**
161
     * @return LanguageService
162
     */
163
    protected function getLanguageService()
164
    {
165
        return $GLOBALS['LANG'];
166
    }
167
}
168