Completed
Push — master ( 522461...c89cc2 )
by Fabien
02:24
created

ThumbnailGenerator::getDataService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Fab\Media\Thumbnail;
4
5
/*
6
 * This file is part of the Fab/Media project under GPLv2 or later.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.md file that was distributed with this source code.
10
 */
11
12
use Fab\Vidi\Service\DataService;
13
use TYPO3\CMS\Core\Resource\File;
14
use TYPO3\CMS\Core\Resource\ResourceFactory;
15
use TYPO3\CMS\Core\Resource\ResourceStorage;
16
use TYPO3\CMS\Core\Utility\GeneralUtility;
17
use Fab\Vidi\Domain\Model\Selection;
18
19
/**
20
 * Thumbnail Generator for generating thumbnails in batch.
21
 */
22
class ThumbnailGenerator
23
{
24
25
    /**
26
     * @var int
27
     */
28
    protected $numberOfTraversedFiles = 0;
29
30
    /**
31
     * @var int
32
     */
33
    protected $numberOfProcessedFiles = 0;
34
35
    /**
36
     * @var int
37
     */
38
    protected $numberOfMissingFiles = 0;
39
40
    /**
41
     * @var array
42
     */
43
    protected $configuration = [];
44
45
    /**
46
     * @var ResourceStorage
47
     */
48
    protected $storage = null;
49
50
    /**
51
     * @var Selection
52
     */
53
    protected $selection = null;
54
55
    /**
56
     * @var array
57
     */
58
    protected $resultSet = [];
59
60
    /**
61
     * @var array
62
     */
63
    protected $newProcessedFileIdentifiers = [];
64
65
    /**
66
     * Internal variable
67
     *
68
     * @var int
69
     */
70
    protected $lastInsertedProcessedFile = 0;
71
72
    /**
73
     * Generate
74
     *
75
     * @param int $limit
76
     * @param int $offset
77
     * @return void
78
     * @throws \TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException
79
     * @throws \InvalidArgumentException
80
     * @throws \Fab\Media\Exception\InvalidKeyInArrayException
81
     * @throws \Fab\Media\Exception\MissingTcaConfigurationException
82
     */
83
    public function generate($limit = 0, $offset = 0)
0 ignored issues
show
Unused Code introduced by
The parameter $limit is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $offset is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
84
    {
85
86
        // Compute a possible limit and offset for the query.
87
        //$limitAndOffset = '';
88
        //if ($limit > 0 || $offset > 0) {
89
        //    $limitAndOffset = $limit . ' OFFSET ' . $offset;
90
        //}
91
92
        $rows = $this->getDataService()
93
            ->getRecords(
94
                'sys_file',
95
                [
96
                    'storage' => $this->storage->getUid()
97
                ] // todo add limit and offset
98
            );
99
100
        foreach ($rows as $row) {
101
102
103
            $file = ResourceFactory::getInstance()->getFileObject($row['uid'], $row);
104
105
            if ($file->exists()) {
106
107
                $thumbnailUri = $this->getThumbnailService($file)
108
                    ->setOutputType(ThumbnailInterface::OUTPUT_URI)
109
                    ->setConfiguration($this->configuration)
110
                    ->create();
111
112
                $this->resultSet[$file->getUid()] = array(
113
                    'fileUid' => $file->getUid(),
114
                    'fileIdentifier' => $file->getIdentifier(),
115
                    'thumbnailUri' => strpos($thumbnailUri, '_processed_') > 0 ? $thumbnailUri : '', // only returns the thumbnail uri if a processed file has been created.
116
                );
117
118
                //if ($this->isNewProcessedFile()) { // todo restore me
119
                //    $this->incrementNumberOfProcessedFiles();
120
                //   $this->newProcessedFileIdentifiers[$file->getUid()] = $this->lastInsertedProcessedFile;
121
                //}
122
123
                $this->incrementNumberOfTraversedFiles();
124
            } else {
125
                $this->incrementNumberOfMissingFiles();
126
            }
127
        }
128
129
    }
130
131
    /**
132
     * @return int
133
     */
134
    protected function isNewProcessedFile()
135
    {
136
        $isNewProcessedFile = false;
137
        $lastInsertedId = $this->getDatabaseConnection()->sql_insert_id();
0 ignored issues
show
Bug introduced by
The method getDatabaseConnection() does not seem to exist on object<Fab\Media\Thumbnail\ThumbnailGenerator>.

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...
138
        if ($lastInsertedId > 0 && $lastInsertedId !== $this->lastInsertedProcessedFile) {
139
            $this->lastInsertedProcessedFile = $lastInsertedId;
140
            $isNewProcessedFile = true;
141
        }
142
        return $isNewProcessedFile;
143
    }
144
145
    /**
146
     * @return int
147
     */
148
    public function getNumberOfTraversedFiles()
149
    {
150
        return $this->numberOfTraversedFiles;
151
    }
152
153
    /**
154
     * @return int
155
     */
156
    public function getNumberOfProcessedFiles()
157
    {
158
        return $this->numberOfProcessedFiles;
159
    }
160
161
    /**
162
     * @return int
163
     */
164
    public function getTotalNumberOfFiles()
165
    {
166
        return $this->getDataService()
167
            ->count(
168
                'sys_file',
169
                [
170
                    'storage' => $this->storage->getUid()
171
                ]
172
            );
173
    }
174
175
    /**
176
     * @return array
177
     */
178
    public function getResultSet()
179
    {
180
        return $this->resultSet;
181
    }
182
183
    /**
184
     * @return array
185
     */
186
    public function getNewProcessedFileIdentifiers()
187
    {
188
        return $this->newProcessedFileIdentifiers;
189
    }
190
191
    /**
192
     * @return int
193
     */
194
    public function getNumberOfMissingFiles()
195
    {
196
        return $this->numberOfMissingFiles;
197
    }
198
199
    /**
200
     * @param \TYPO3\CMS\Core\Resource\ResourceStorage $storage
201
     * @return $this
202
     */
203
    public function setStorage($storage)
204
    {
205
        $this->storage = $storage;
206
        return $this;
207
    }
208
209
    /**
210
     * @param \Fab\Vidi\Domain\Model\Selection $selection
211
     * @return $this
212
     */
213
    public function setSelection($selection)
214
    {
215
        $this->selection = $selection;
216
        return $this;
217
    }
218
219
    /**
220
     * @param array $configuration
221
     * @return $this
222
     */
223
    public function setConfiguration($configuration)
224
    {
225
        $this->configuration = $configuration;
226
        return $this;
227
    }
228
229
    /**
230
     * @param File $file
231
     * @return object|ThumbnailService
232
     */
233
    protected function getThumbnailService(File $file)
234
    {
235
        return GeneralUtility::makeInstance(ThumbnailService::class, $file);
236
    }
237
238
    /**
239
     * @return void
240
     */
241
    protected function incrementNumberOfTraversedFiles()
242
    {
243
        $this->numberOfTraversedFiles++;
244
    }
245
246
    /**
247
     * @return void
248
     */
249
    protected function incrementNumberOfMissingFiles()
250
    {
251
        $this->numberOfMissingFiles++;
252
    }
253
254
    /**
255
     * @return void
256
     */
257
    protected function incrementNumberOfProcessedFiles()
258
    {
259
        $this->numberOfProcessedFiles++;
260
    }
261
262
    /**
263
     * @return object|DataService
264
     */
265
    protected function getDataService(): DataService
266
    {
267
        return GeneralUtility::makeInstance(DataService::class);
268
    }
269
}
270