Completed
Push — master ( 4d101c...278637 )
by Lukas
18:49 queued 06:10
created

ConvertMysqlToMB4::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 3
nop 2
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Thomas Müller <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2017, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OC\Core\Command\Db;
23
24
use Doctrine\DBAL\Platforms\MySqlPlatform;
25
use OC\DB\MySqlTools;
26
use OC\Migration\ConsoleOutput;
27
use OC\Repair\Collation;
28
use OCP\IConfig;
29
use OCP\IDBConnection;
30
use OCP\ILogger;
31
use OCP\IURLGenerator;
32
use Symfony\Component\Console\Command\Command;
33
use Symfony\Component\Console\Input\InputInterface;
34
use Symfony\Component\Console\Output\OutputInterface;
35
36
class ConvertMysqlToMB4 extends Command {
37
	/** @var IConfig */
38
	private $config;
39
40
	/** @var IDBConnection */
41
	private $connection;
42
43
	/** @var IURLGenerator */
44
	private $urlGenerator;
45
46
	/** @var ILogger */
47
	private $logger;
48
49
	/**
50
	 * @param IConfig $config
51
	 * @param IDBConnection $connection
52
	 * @param IURLGenerator $urlGenerator
53
	 * @param ILogger $logger
54
	 */
55 View Code Duplication
	public function __construct(IConfig $config, IDBConnection $connection, IURLGenerator $urlGenerator, ILogger $logger) {
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...
56
		$this->config = $config;
57
		$this->connection = $connection;
58
		$this->urlGenerator = $urlGenerator;
59
		$this->logger = $logger;
60
		parent::__construct();
61
	}
62
63
	protected function configure() {
64
		$this
65
			->setName('db:convert-mysql-charset')
66
			->setDescription('Convert charset of MySQL/MariaDB to use utf8mb4');
67
	}
68
69
	protected function execute(InputInterface $input, OutputInterface $output) {
70
		if (!$this->connection->getDatabasePlatform() instanceof MySqlPlatform) {
0 ignored issues
show
Bug introduced by
The class Doctrine\DBAL\Platforms\MySqlPlatform does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
71
			$output->writeln("This command is only valid for MySQL/MariaDB databases.");
72
			return 1;
73
		}
74
75
		$tools = new MySqlTools();
76
		if (!$tools->supports4ByteCharset($this->connection)) {
77
			$url = $this->urlGenerator->linkToDocs('admin-mysql-utf8mb4');
78
			$output->writeln("The database is not properly setup to use the charset utf8mb4.");
79
			$output->writeln("For more information please read the documentation at $url");
80
			return 1;
81
		}
82
83
		// enable charset
84
		$this->config->setSystemValue('mysql.utf8mb4', true);
85
86
		// run conversion
87
		$coll = new Collation($this->config, $this->logger, $this->connection, false);
88
		$coll->run(new ConsoleOutput($output));
89
90
		return 0;
91
	}
92
}
93