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

ThumbnailCommandController::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\Command;
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\Media\Property\TypeConverter\ConfigurationArrayConverter;
13
use Fab\Vidi\Service\DataService;
14
use TYPO3\CMS\Core\Resource\StorageRepository;
15
use TYPO3\CMS\Core\Utility\GeneralUtility;
16
use TYPO3\CMS\Extbase\Mvc\Controller\CommandController;
17
18
/**
19
 * Command Controller which handles CLI action related to Thumbnails.
20
 */
21
class ThumbnailCommandController extends CommandController
22
{
23
24
    /**
25
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentTypeException
26
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException
27
     */
28
    protected function initializeCommandMethodArguments()
29
    {
30
        parent::initializeCommandMethodArguments();
31
        if ($this->arguments->hasArgument('configuration')) {
32
            $propertyMappingConfiguration = $this->arguments->getArgument('configuration')->getPropertyMappingConfiguration();
33
            $propertyMappingConfiguration->setTypeConverter(
34
                $this->objectManager->get(ConfigurationArrayConverter::class)
35
            );
36
        }
37
    }
38
39
40
    /**
41
     * Generate a bunch of thumbnails in advance to speed up the output of the Media BE module.
42
     *
43
     * @param int $limit where to stop in the batch processing.
44
     * @param int $offset where to start in the batch processing.
45
     * @param array $configuration override the default thumbnail configuration.
46
     * @param bool $verbose will output a detail result of the thumbnail generation.
47
     * @return void
48
     */
49
    public function generateCommand($limit = 0, $offset = 0, $configuration = [], $verbose = false)
50
    {
51
52
        $this->checkEnvironment();
53
54
        foreach ($this->getStorageRepository()->findAll() as $storage) {
55
56
            // TODO: Make me more flexible by passing thumbnail configuration. For now it will only generate thumbnails for the BE module.
57
58
            $this->outputLine();
59
            $this->outputLine(sprintf('%s (%s)', $storage->getName(), $storage->getUid()));
60
            $this->outputLine('--------------------------------------------');
61
            $this->outputLine();
62
63
            if ($storage->isOnline()) {
64
65
                // For the CLI cause.
66
                $storage->setEvaluatePermissions(false);
67
68
                $thumbnailGenerator = $this->getThumbnailGenerator();
69
                $thumbnailGenerator
70
                    ->setStorage($storage)
71
                    ->setConfiguration($configuration)
72
                    ->generate($limit, $offset);
73
74
                if ($verbose) {
75
                    $resultSet = $thumbnailGenerator->getResultSet();
76
                    foreach ($resultSet as $result) {
77
                        $message = sprintf('* File "%s": %s %s',
78
                            $result['fileUid'],
79
                            $result['fileIdentifier'],
80
                            empty($result['thumbnailUri']) ? '' : ' -> ' . $result['thumbnailUri']
81
                        );
82
                        $this->outputLine($message);
83
                    }
84
                    $this->outputLine();
85
                }
86
87
                $message = sprintf('Done! New generated %s thumbnail(s) from %s traversed file(s) of a total of %s files.',
88
                    $thumbnailGenerator->getNumberOfProcessedFiles(),
89
                    $thumbnailGenerator->getNumberOfTraversedFiles(),
90
                    $thumbnailGenerator->getTotalNumberOfFiles()
91
                );
92
                $this->outputLine($message);
93
94
                // Add warning message if missing files were found along the way.
95
                if ($thumbnailGenerator->getNumberOfMissingFiles() > 0) {
96
97
                    $message = sprintf('ATTENTION! %s missing file(s) detected.',
98
                        $thumbnailGenerator->getNumberOfMissingFiles()
99
                    );
100
                    $this->outputLine($message);
101
                }
102
            } else {
103
                $this->outputLine('Storage is offline!');
104
            }
105
        }
106
    }
107
108
    /**
109
     * @return void
110
     */
111
    protected function checkEnvironment()
112
    {
113
        $user = $this->getDataService()->getRecord(
114
            'be_users', [
115
            'username' => '_cli_lowlevel'
116
        ]);
117
118
        if (empty($user)) {
119
            $this->outputLine('Missing User "_cli_lowlevel" and / or its password.');
120
            $this->sendAndExit(1);
121
        }
122
123
        $user = $this->getDataService()->getRecord(
124
            'be_users', [
125
            'username' => '_cli_scheduler'
126
        ]);
127
128
        if (empty($user)) {
129
            $this->outputLine('Missing User "_cli_scheduler" and / or its password.');
130
            $this->sendAndExit(1);
131
        }
132
    }
133
134
    /**
135
     * @return object|DataService
136
     */
137
    protected function getDataService(): DataService
138
    {
139
        return GeneralUtility::makeInstance(DataService::class);
140
    }
141
142
    /**
143
     * @return \Fab\Media\Thumbnail\ThumbnailGenerator|object
144
     */
145
    protected function getThumbnailGenerator()
146
    {
147
        return GeneralUtility::makeInstance(\Fab\Media\Thumbnail\ThumbnailGenerator::class);
148
    }
149
150
    /**
151
     * @return StorageRepository|object
152
     */
153
    protected function getStorageRepository()
154
    {
155
        return GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\StorageRepository::class);
156
    }
157
}
158