Passed
Push — master ( 31284f...e373e6 )
by Pauli
01:55
created

PlaylistDateInit::setCreationDates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * ownCloud - Music app
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Pauli Järvinen <[email protected]>
10
 * @copyright Pauli Järvinen 2020
11
 */
12
13
namespace OCA\Music\Migration;
14
15
use OCP\IConfig;
0 ignored issues
show
Bug introduced by
The type OCP\IConfig was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use OCP\IDBConnection;
0 ignored issues
show
Bug introduced by
The type OCP\IDBConnection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use OCP\Migration\IOutput;
0 ignored issues
show
Bug introduced by
The type OCP\Migration\IOutput was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use OCP\Migration\IRepairStep;
0 ignored issues
show
Bug introduced by
The type OCP\Migration\IRepairStep was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
20
class PlaylistDateInit implements IRepairStep {
21
22
	/** @var IDBConnection */
23
	private $db;
24
25
	/** @var IConfig */
26
	private $config;
27
28
	public function __construct(IDBConnection $connection, IConfig $config) {
29
		$this->db = $connection;
30
		$this->config = $config;
31
	}
32
33
	public function getName() {
34
		return 'Set creation date for playlists without one';
35
	}
36
37
	/**
38
	 * @inheritdoc
39
	 */
40
	public function run(IOutput $output) {
41
		$installedVersion = $this->config->getAppValue('music', 'installed_version');
42
43
		// Music version 0.14.1 added the `created` column to the playlists table
44
		if (\version_compare($installedVersion, '0.14.1', '<')) {
45
			$n = $this->setCreationDates();
46
			$output->info("This date was added as creation date for $n playlists");
47
		}
48
	}
49
50
	private function setCreationDates() {
51
		$now = new \DateTime();
52
		$sql = "UPDATE `*PREFIX*music_playlists` SET `created` = ? WHERE `created` IS NULL";
53
		$params = [$now->format('Y-m-d H:i:s')];
54
		return $this->db->executeUpdate($sql, $params);
55
	}
56
}
57