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

FileCacheCommandController::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
namespace Fab\Media\Command;
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
11
use Fab\Vidi\Service\DataService;
12
use FilesystemIterator;
13
use TYPO3\CMS\Core\Database\Connection;
14
use TYPO3\CMS\Core\Database\ConnectionPool;
15
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
16
use TYPO3\CMS\Core\Resource\ProcessedFileRepository;
17
use TYPO3\CMS\Core\Resource\StorageRepository;
18
use TYPO3\CMS\Core\Utility\GeneralUtility;
19
use TYPO3\CMS\Extbase\Mvc\Controller\CommandController;
20
21
/**
22
 * Command Controller which handles actions related to the Cache.
23
 */
24
class FileCacheCommandController extends CommandController
25
{
26
27
    /**
28
     * Warm up the cache. Update some caching columns such as "number_of_references" to speed up the search.
29
     *
30
     * @return void
31
     */
32
    public function warmUpCommand()
33
    {
34
        $numberOfEntries = $this->getCacheService()->warmUp();
35
        $message = sprintf('Done! Processed %s entries', $numberOfEntries);
36
        $this->outputLine($message);
37
    }
38
39
    /**
40
     * Flush all processed files to be used for debugging mainly.
41
     *
42
     * @return void
43
     */
44
    public function flushProcessedFilesCommand()
45
    {
46
47
        foreach ($this->getStorageRepository()->findAll() as $storage) {
48
49
            // This only works for local driver
50
            if ($storage->getDriverType() === 'Local') {
51
52
                $this->outputLine();
53
                $this->outputLine(sprintf('%s (%s)', $storage->getName(), $storage->getUid()));
54
                $this->outputLine('--------------------------------------------');
55
                $this->outputLine();
56
57
                #$storage->getProcessingFolder()->delete(true); // will not work
58
59
                // Well... not really FAL friendly but straightforward for Local drivers.
60
                $processedDirectoryPath = PATH_site . $storage->getProcessingFolder()->getPublicUrl();
61
                $fileIterator = new FilesystemIterator($processedDirectoryPath, FilesystemIterator::SKIP_DOTS);
62
                $numberOfProcessedFiles = iterator_count($fileIterator);
63
64
                GeneralUtility::rmdir($processedDirectoryPath, true);
65
                GeneralUtility::mkdir($processedDirectoryPath); // recreate the directory.
66
67
                $message = sprintf('Done! Removed %s processed file(s).', $numberOfProcessedFiles);
68
                $this->outputLine($message);
69
70
                $count = $this->getDataService()
71
                    ->count(
72
                        'sys_file_processedfile',
73
                        [
74
                            'storage' => $storage->getUid()
75
                        ]
76
                    );
77
78
                // Remove the record as well.
79
                $this->getDataService()->delete('sys_file_processedfile', ['storage' => $storage->getUid()]);
80
81
                $message = sprintf('Done! Removed %s records from "sys_file_processedfile".', $count);
82
                $this->outputLine($message);
83
            }
84
85
        }
86
87
        // Remove possible remaining "sys_file_processedfile"
88
        $this
89
            ->getConnection('sys_file_processedfile')
90
            ->query('TRUNCATE sys_file_processedfile');
91
    }
92
93
    /**
94
     * @return \Fab\Media\Cache\CacheService|object
95
     */
96
    protected function getCacheService()
97
    {
98
        return GeneralUtility::makeInstance(\Fab\Media\Cache\CacheService::class);
99
    }
100
101
    /**
102
     * @return StorageRepository|object
103
     */
104
    protected function getStorageRepository()
105
    {
106
        return GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\StorageRepository::class);
107
    }
108
109
    /**
110
     * @return object|DataService
111
     */
112
    protected function getDataService(): DataService
113
    {
114
        return GeneralUtility::makeInstance(DataService::class);
115
    }
116
117
    /**
118
     * @param string $tableName
119
     * @return object|Connection
120
     */
121
    protected function getConnection($tableName): Connection
122
    {
123
        /** @var ConnectionPool $connectionPool */
124
        $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
125
        return $connectionPool->getConnectionForTable($tableName);
126
    }
127
128
}
129