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

ThumbnailGeneratorTool::getDatabaseConnection()   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
namespace Fab\Media\Tool;
3
4
/*
5
 * This file is part of the Fab/Media project under GPLv2 or later.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.md file that was distributed with this source code.
9
 */
10
use TYPO3\CMS\Core\Resource\ResourceFactory;
11
use TYPO3\CMS\Core\Resource\StorageRepository;
12
use TYPO3\CMS\Core\Utility\GeneralUtility;
13
use Fab\Vidi\Tool\AbstractTool;
14
15
/**
16
 * Thumbnail generator tool for the Media module.
17
 */
18
class ThumbnailGeneratorTool extends AbstractTool
19
{
20
21
    /**
22
     * Display the title of the tool on the welcome screen.
23
     *
24
     * @return string
25
     */
26
    public function getTitle()
27
    {
28
        return 'Generate thumbnails';
29
    }
30
31
    /**
32
     * Display the description of the tool in the welcome screen.
33
     *
34
     * @return string
35
     */
36 View Code Duplication
    public function getDescription()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
    {
38
        $templateNameAndPath = 'EXT:media/Resources/Private/Standalone/Tool/ThumbnailGenerator/Launcher.html';
39
        $view = $this->initializeStandaloneView($templateNameAndPath);
40
        $view->assign('sitePath', PATH_site);
41
        return $view->render();
42
    }
43
44
    /**
45
     * Do the job: analyse Index.
46
     *
47
     * @param array $arguments
48
     * @return string
49
     */
50
    public function work(array $arguments = [])
51
    {
52
53
        $reports = [];
54
55
        $limit = 500; // default value
56
        $newOffset = 0;
57
58
        // Possible clean up of missing files if the User has clicked so.
59
        if (isset($arguments['limit']) && isset($arguments['offset'])) {
60
61
            $limit = (int)$arguments['limit'];
62
            $offset = (int)$arguments['offset'];
63
64
            foreach ($this->getStorageRepository()->findAll() as $storage) {
65
66
                if ($storage->isOnline()) {
67
68
                    $thumbnailGenerator = $this->getThumbnailGenerator();
69
                    $thumbnailGenerator
70
                        ->setStorage($storage)
71
                        ->generate($limit, $offset);
72
73
                    $formattedResultSet = [];
74
                    $resultSet = $thumbnailGenerator->getResultSet();
75
                    $processedFileIdentifiers = $thumbnailGenerator->getNewProcessedFileIdentifiers();
76
77
                    foreach ($processedFileIdentifiers as $fileIdentifier => $processedFileIdentifier) {
78
                        $result = $resultSet[$fileIdentifier];
79
                        $formattedResultSet[] = sprintf('* File "%s": %s %s',
80
                            $result['fileUid'],
81
                            $result['fileIdentifier'],
82
                            empty($result['thumbnailUri']) ? '' : ' -> ' . $result['thumbnailUri']
83
                        );
84
                    }
85
86
                    $reports[] = array(
87
                        'storage' => $storage,
88
                        'isStorageOnline' => true,
89
                        'resultSet' => $formattedResultSet,
90
                        'numberOfProcessedFiles' => $thumbnailGenerator->getNumberOfProcessedFiles(),
91
                        'numberOfTraversedFiles' => $thumbnailGenerator->getNumberOfTraversedFiles(),
92
                        'numberOfMissingFiles' => $thumbnailGenerator->getNumberOfMissingFiles(),
93
                        'totalNumberOfFiles' => $thumbnailGenerator->getTotalNumberOfFiles(),
94
                    );
95
                } else {
96
                    $reports[] = array(
97
                        'storage' => $storage,
98
                        'isStorageOnline' => false,
99
                    );
100
                }
101
            }
102
103
            $newOffset = $limit + $offset;
104
        }
105
106
        $templateNameAndPath = 'EXT:media/Resources/Private/Standalone/Tool/ThumbnailGenerator/WorkResult.html';
107
        $view = $this->initializeStandaloneView($templateNameAndPath);
108
109
        $view->assign('limit', $limit);
110
        $view->assign('offset', $newOffset);
111
        $view->assign('reports', $reports);
112
        return $view->render();
113
    }
114
115
    /**
116
     * @return \Fab\Media\Thumbnail\ThumbnailGenerator|object
117
     */
118
    protected function getThumbnailGenerator()
119
    {
120
        return GeneralUtility::makeInstance(\Fab\Media\Thumbnail\ThumbnailGenerator::class);
121
    }
122
123
    /**
124
     * @return StorageRepository|object
125
     */
126
    protected function getStorageRepository()
127
    {
128
        return GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\StorageRepository::class);
129
    }
130
131
    /**
132
     * Tell whether the tools should be displayed according to the context.
133
     *
134
     * @return bool
135
     */
136
    public function isShown()
137
    {
138
        return $this->getBackendUser()->isAdmin();
139
    }
140
141
}
142
143