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

MissingFilesFinderTool::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\Tool;
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\ResourceFactory;
14
use TYPO3\CMS\Core\Resource\StorageRepository;
15
use TYPO3\CMS\Core\Utility\GeneralUtility;
16
use Fab\Vidi\Tool\AbstractTool;
17
18
/**
19
 * Index analyser tool for the Media module.
20
 */
21
class MissingFilesFinderTool extends AbstractTool
22
{
23
24
    /**
25
     * Display the title of the tool on the welcome screen.
26
     *
27
     * @return string
28
     */
29
    public function getTitle()
30
    {
31
        return 'Find missing files';
32
    }
33
34
    /**
35
     * Display the description of the tool in the welcome screen.
36
     *
37
     * @return string
38
     */
39 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...
40
    {
41
        $templateNameAndPath = 'EXT:media/Resources/Private/Standalone/Tool/MissingFilesFinder/Launcher.html';
42
        $view = $this->initializeStandaloneView($templateNameAndPath);
43
        $view->assign('sitePath', PATH_site);
44
        return $view->render();
45
    }
46
47
    /**
48
     * Do the job: analyse Index.
49
     *
50
     * @param array $arguments
51
     * @return string
52
     */
53
    public function work(array $arguments = [])
54
    {
55
56
        // Possible clean up of missing files if the User has clicked so.
57
        if (!empty($arguments['deleteMissingFiles'])) {
58
            $this->deleteMissingFilesAction($arguments['files']);
59
        }
60
61
        $templateNameAndPath = 'EXT:media/Resources/Private/Standalone/Tool/MissingFilesFinder/WorkResult.html';
62
        $view = $this->initializeStandaloneView($templateNameAndPath);
63
64
        $missingReports = [];
65 View Code Duplication
        foreach ($this->getStorageRepository()->findAll() as $storage) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
66
67
            if ($storage->isOnline()) {
68
                $missingFiles = $this->getIndexAnalyser()->searchForMissingFiles($storage);
69
70
                $missingReports[] = array(
71
                    'storage' => $storage,
72
                    'missingFiles' => $missingFiles,
73
                    'numberOfMissingFiles' => count($missingFiles),
74
                );
75
            }
76
        }
77
78
        $view->assign('missingReports', $missingReports);
79
80
        return $view->render();
81
    }
82
83
    /**
84
     * Delete files given as parameter.
85
     * This is a special case as we have a missing file in the file system
86
     * As a result, we can't use $fileObject->delete(); which will
87
     * raise exception "Error while fetching permissions".
88
     *
89
     * @param array $files
90
     * @return void
91
     */
92
    protected function deleteMissingFilesAction(array $files = [])
93
    {
94
95
        foreach ($files as $fileUid) {
96
97
            /** @var \TYPO3\CMS\Core\Resource\File $file */
98
            try {
99
                $file = ResourceFactory::getInstance()->getFileObject($fileUid);
100
                if ($file) {
101
                    // The case is special as we have a missing file in the file system
102
                    // As a result, we can't use $fileObject->delete(); which will
103
                    // raise exception "Error while fetching permissions"
104
                    $this->getDataService()->delete('sys_file', ['uid' => $file->getUid()]);
105
                }
106
            } catch (\Exception $e) {
107
                continue;
108
            }
109
        }
110
    }
111
112
    /**
113
     * Return a pointer to the database.
114
     *
115
     * @return \Fab\Media\Index\IndexAnalyser|object
116
     */
117
    protected function getIndexAnalyser()
118
    {
119
        return GeneralUtility::makeInstance(\Fab\Media\Index\IndexAnalyser::class);
120
    }
121
122
    /**
123
     * @return StorageRepository|object
124
     */
125
    protected function getStorageRepository()
126
    {
127
        return GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\StorageRepository::class);
128
    }
129
130
    /**
131
     * Tell whether the tools should be displayed according to the context.
132
     *
133
     * @return bool
134
     */
135
    public function isShown()
136
    {
137
        return $this->getBackendUser()->isAdmin();
138
    }
139
140
    /**
141
     * @return object|DataService
142
     */
143
    protected function getDataService(): DataService
144
    {
145
        return GeneralUtility::makeInstance(DataService::class);
146
    }
147
148
}
149
150