BackendApiCommandController::lockCommand()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 14

Duplication

Lines 9
Ratio 47.37 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 9
loc 19
rs 9.4285
cc 3
eloc 14
nc 4
nop 1
1
<?php
2
namespace Etobi\CoreAPI\Command;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2014 Helmut Hummel <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 2 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *  A copy is found in the text file GPL.txt and important notices to the license
19
 *  from the author is found in LICENSE.txt distributed with these scripts.
20
 *
21
 *
22
 *  This script is distributed in the hope that it will be useful,
23
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
 *  GNU General Public License for more details.
26
 *
27
 *  This copyright notice MUST APPEAR in all copies of the script!
28
 ***************************************************************/
29
use TYPO3\CMS\Extbase\Mvc\Controller\CommandController;
30
31
/**
32
 * API Command Controller
33
 */
34
class BackendApiCommandController extends CommandController {
35
36
	/**
37
	 * @var \TYPO3\CMS\Core\Log\LogManager $logManager
38
	 */
39
	protected $logManager;
40
41
	/**
42
	 * @var \TYPO3\CMS\Core\Log\Logger $logger
43
	 */
44
	protected $logger;
45
46
	/**
47
	 * @param \TYPO3\CMS\Core\Log\LogManager $logManager
48
	 *
49
	 * @return void
50
	 */
51
	public function injectLogManager(\TYPO3\CMS\Core\Log\LogManager $logManager) {
52
		$this->logManager = $logManager;
53
	}
54
55
	/**
56
	 * Initialize the object
57
	 */
58
	public function initializeObject() {
59
		$this->logger = $this->objectManager->get('TYPO3\CMS\Core\Log\LogManager')->getLogger(__CLASS__);
60
	}
61
62
	/**
63
	 * Locks backend access for all users by writing a lock file that is checked when the backend is accessed.
64
	 *
65
	 * @param string $redirectUrl URL to redirect to when the backend is accessed
66
	 */
67
	public function lockCommand($redirectUrl = NULL) {
68
		if (@is_file((PATH_typo3conf . 'LOCK_BACKEND'))) {
69
			$message = 'A lockfile already exists. Overwriting it...';
70
			$this->outputLine($message);
71
			$this->logger->info($message);
72
		}
73
74
		\TYPO3\CMS\Core\Utility\GeneralUtility::writeFile(PATH_typo3conf . 'LOCK_BACKEND', (string)$redirectUrl);
75
76 View Code Duplication
		if ($redirectUrl === NULL) {
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...
77
			$message = 'Wrote lock file to \'typo3conf/LOCK_BACKEND\'';
78
			$this->outputLine($message);
79
			$this->logger->info($message);
80
		} else {
81
			$message = 'Wrote lock file to \'typo3conf/LOCK_BACKEND\' with instruction to redirect to: \'' . $redirectUrl . '\'';
82
			$this->outputLine($message);
83
			$this->logger->info($message);
84
		}
85
	}
86
87
	/**
88
	 * Unlocks the backend access by deleting the lock file
89
	 */
90
	public function unlockCommand() {
91
		if (@is_file((PATH_typo3conf . 'LOCK_BACKEND'))) {
92
			unlink(PATH_typo3conf . 'LOCK_BACKEND');
93
			if (@is_file((PATH_typo3conf . 'LOCK_BACKEND'))) {
94
				$message = 'ERROR: Could not remove lock file \'typo3conf/LOCK_BACKEND\'!';
95
				$this->outputLine($message);
96
				$this->logger->error($message);
97
				$this->quit(1);
98
			} else {
99
				$message = 'Removed lock file \'typo3conf/LOCK_BACKEND\'';
100
				$this->outputLine($message);
101
				$this->logger->info($message);
102
			}
103
		} else {
104
			$message = 'No lock file \'typo3conf/LOCK_BACKEND\' was found, hence no lock could be removed.';
105
			$this->outputLine($message);
106
			$this->logger->info($message);
107
			$this->quit(1);
108
		}
109
	}
110
}