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

getDatabaseConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
dl 4
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 TYPO3\CMS\Core\Resource\StorageRepository;
12
use TYPO3\CMS\Core\Utility\GeneralUtility;
13
use TYPO3\CMS\Extbase\Mvc\Controller\CommandController;
14
15
/**
16
 * Command Controller which handles actions related to File Index.
17
 */
18 View Code Duplication
class DuplicateRecordsCommandController extends CommandController
0 ignored issues
show
Duplication introduced by
This class 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...
19
{
20
21
    /**
22
     * @var array
23
     */
24
    protected $message = [];
25
26
    /**
27
     * @var array
28
     */
29
    protected $duplicateRecords = [];
30
31
    /**
32
     * @var \TYPO3\CMS\Core\Mail\MailMessage
33
     */
34
    protected $mailMessage;
35
36
    /**
37
     * Check whether the Index is Ok. In case not, display a message on the console.
38
     *
39
     * @return void
40
     */
41
    public function analyseCommand()
42
    {
43
44
        foreach ($this->getStorageRepository()->findAll() as $storage) {
45
46
            // For the CLI cause.
47
            $storage->setEvaluatePermissions(false);
48
49
            $this->printOut();
50
            $this->printOut(sprintf('%s (%s)', $storage->getName(), $storage->getUid()));
51
            $this->printOut('--------------------------------------------');
52
53
            if ($storage->isOnline()) {
54
55
                $duplicateRecords = $this->getIndexAnalyser()->searchForDuplicateIdentifiers($storage);
56
57
                // Duplicate file object
58
                if (empty($duplicateRecords)) {
59
                    $this->printOut();
60
                    $this->printOut('Looks good, no duplicate records!');
61
                } else {
62
                    $this->printOut();
63
                    $this->printOut('Duplicated identifiers detected:');
64
                    $this->duplicateRecords[$storage->getUid()] = $duplicateRecords; // Store duplicate files.
65
66
                    foreach ($duplicateRecords as $identifier => $duplicate) {
67
68
                        // build temporary array
69
                        $uids = [];
70
                        foreach ($duplicate as $value) {
71
                            $uids[] = $value['uid'];
72
                        }
73
74
                        $message = sprintf('* uids "%s" having same identifier %s',
75
                            implode(',', $uids),
76
                            $identifier
77
                        );
78
                        $this->printOut($message);
79
80
                    }
81
                }
82
            } else {
83
                $this->outputLine('Storage is offline!');
84
            }
85
        }
86
87
        $to = $this->getTo();
88
        if (!empty($to)) {
89
            $this->sendReport();
90
        }
91
    }
92
93
    /**
94
     * Print a message and store its content in a variable for the email report.
95
     *
96
     * @param string $message
97
     * @return void
98
     */
99
    protected function printOut($message = '')
100
    {
101
        $this->message[] = $message;
102
        $this->outputLine($message);
103
    }
104
105
    /**
106
     * Send a possible report to an admin.
107
     *
108
     * @throws \Exception
109
     * @return void
110
     */
111
    protected function sendReport()
112
    {
113
        if ($this->hasReport()) {
114
115
            // Prepare email.
116
            $this->getMailMessage()->setTo($this->getTo())
117
                ->setFrom($this->getFrom())
118
                ->setSubject('Duplicate records detected!')
119
                ->setBody(implode("\n", $this->message));
120
121
            $isSent = $this->getMailMessage()->send();
122
123
            if (!$isSent) {
124
                throw new \Exception('I could not send a message', 1408343882);
125
            }
126
127
            $to = $this->getTo();
128
            $this->outputLine();
129
            $message = sprintf('Report was sent to %s', key($to));
130
            $this->outputLine($message);
131
        }
132
    }
133
134
    /**
135
     * Send a report
136
     *
137
     * @return bool
138
     */
139
    protected function hasReport()
140
    {
141
        return !empty($this->duplicateRecords);
142
    }
143
144
    /**
145
     * @return array
146
     */
147
    protected function getTo()
148
    {
149
150
        $to = [];
151
152
        // @todo make me more flexible!
153
        if (!empty($GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'])) {
154
            $emailAddress = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'];
155
            $name = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'];
156
            $to[$emailAddress] = $name;
157
158
        }
159
        return $to;
160
    }
161
162
    /**
163
     * @return array
164
     */
165
    protected function getFrom()
166
    {
167
168
        $from = [];
169
170
        // @todo make me more flexible!
171
        if (!empty($GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'])) {
172
            $emailAddress = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'];
173
            $name = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'];
174
            $from[$emailAddress] = $name;
175
        }
176
        return $from;
177
    }
178
179
    /**
180
     * @return StorageRepository|object
181
     */
182
    protected function getStorageRepository()
183
    {
184
        return GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\StorageRepository::class);
185
    }
186
187
    /**
188
     * @return \TYPO3\CMS\Core\Mail\MailMessage|object
189
     */
190
    public function getMailMessage()
191
    {
192
        if (is_null($this->mailMessage)) {
193
            $this->mailMessage = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Mail\MailMessage::class);
194
        }
195
        return $this->mailMessage;
196
    }
197
198
    /**
199
     * Return a pointer to the database.
200
     *
201
     * @return \Fab\Media\Index\IndexAnalyser|object
202
     */
203
    protected function getIndexAnalyser()
204
    {
205
        return GeneralUtility::makeInstance(\Fab\Media\Index\IndexAnalyser::class);
206
    }
207
208
}
209