DatabaseApiCommandController   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 4
Bugs 0 Features 3
Metric Value
wmc 11
c 4
b 0
f 3
lcom 2
cbo 1
dl 0
loc 90
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A injectLogManager() 0 3 1
A initializeObject() 0 3 1
A injectDatabaseApiService() 0 3 1
C databaseCompareCommand() 0 38 8
1
<?php
2
namespace Etobi\CoreAPI\Command;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2012 Georg Ringer <[email protected]>
8
 *  (c) 2014 Stefano Kowalke <[email protected]>
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 2 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
use TYPO3\CMS\Extbase\Mvc\Controller\CommandController;
28
29
/**
30
 * API Command Controller
31
 *
32
 * @author Georg Ringer <[email protected]>
33
 * @author Stefano Kowalke <[email protected]>
34
 * @package Etobi\CoreAPI\Service\SiteApiService
35
 */
36
class DatabaseApiCommandController extends CommandController {
37
38
	/**
39
	 * @var \TYPO3\CMS\Core\Log\LogManager $logManager
40
	 */
41
	protected $logManager;
42
43
	/**
44
	 * @var \TYPO3\CMS\Core\Log\Logger $logger
45
	 */
46
	protected $logger;
47
48
	/**
49
	 * @param \TYPO3\CMS\Core\Log\LogManager $logManager
50
	 *
51
	 * @return void
52
	 */
53
	public function injectLogManager(\TYPO3\CMS\Core\Log\LogManager $logManager) {
54
		$this->logManager = $logManager;
55
	}
56
57
	/**
58
	 * Initialize the object
59
	 */
60
	public function initializeObject() {
61
		$this->logger = $this->objectManager->get('TYPO3\CMS\Core\Log\LogManager')->getLogger(__CLASS__);
62
	}
63
64
	/**
65
	 * @var \Etobi\CoreAPI\Service\DatabaseApiService $databaseApiService
66
	 */
67
	protected $databaseApiService;
68
69
	/**
70
	 * Injects the DatabaseApiService object
71
	 *
72
	 * @param \Etobi\CoreAPI\Service\DatabaseApiService $databaseApiService
73
	 *
74
	 * @return void
75
	 */
76
	public function injectDatabaseApiService(\Etobi\CoreAPI\Service\DatabaseApiService $databaseApiService) {
77
		$this->databaseApiService = $databaseApiService;
78
	}
79
80
	/**
81
	 * Database compare.
82
	 * Leave the argument 'actions' empty or use "help" to see the available ones
83
	 *
84
	 * @param string $actions List of actions which will be executed
85
	 * @param bool   $dry
86
	 */
87
	public function databaseCompareCommand($actions = '', $dry = FALSE) {
88
		if ($actions === 'help' || strlen($actions) === 0) {
89
			$actions = $this->databaseApiService->databaseCompareAvailableActions();
90
			foreach ($actions as $number => $action) {
91
				$this->outputLine('  - ' . $action . ' => ' . $number);
92
			}
93
			$this->quit();
94
		}
95
96
		$result = $this->databaseApiService->databaseCompare($actions, $dry);
0 ignored issues
show
Bug introduced by
It seems like $actions defined by $this->databaseApiServic...mpareAvailableActions() on line 89 can also be of type array; however, Etobi\CoreAPI\Service\Da...vice::databaseCompare() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
97
98
		if ($dry) {
99
			$this->outputLine('DB compare would execute the following queries:');
100
			foreach($result as $key => $set) {
101
				$this->outputLine(sprintf('### Action: %s ###', $key));
102
				$this->outputLine('===================================');
103
				$this->logger->info(sprintf('### Action: %s ###', $key));
104
				$this->logger->info('===================================');
105
				foreach($set as $line) {
106
					$this->outputLine($line);
107
					$this->logger->info($line);
108
				}
109
				$this->outputLine(LF);
110
			}
111
			$this->logger->info('DB compare executed in dry mode');
112
		} else {
113
			if (empty($result)) {
114
				$message = 'DB has been compared';
115
				$this->outputLine($message);
116
				$this->logger->info($message);
117
			} else {
118
				$message = vsprintf('DB could not be compared, Error(s): %s', array(LF . implode(LF, $result)));
119
				$this->outputLine($message);
120
				$this->logger->error($message);
121
				$this->quit(1);
122
			}
123
		}
124
	}
125
}