MigrationTrait::logChanges()   B
last analyzed

Complexity

Conditions 8
Paths 17

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 25
c 0
b 0
f 0
ccs 0
cts 22
cp 0
rs 8.4444
cc 8
nc 17
nop 4
crap 72
1
<?php
2
/**
3
 * CMS Pico - Create websites using Pico CMS for Nextcloud.
4
 *
5
 * @copyright Copyright (c) 2019, Daniel Rudolf (<[email protected]>)
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
declare(strict_types=1);
24
25
namespace OCA\CMSPico\Migration;
26
27
use OCA\CMSPico\AppInfo\Application;
28
use OCP\ILogger;
29
use OCP\Migration\IOutput;
30
31
trait MigrationTrait
32
{
33
	/** @var ILogger */
34
	private $logger;
35
36
	/** @var IOutput */
37
	private $output;
38
39
	/**
40
	 * @param ILogger $logger
41
	 */
42
	protected function setLogger(ILogger $logger): void
43
	{
44
		$this->logger = $logger;
45
	}
46
47
	/**
48
	 * @param IOutput $output
49
	 */
50
	protected function setOutput(IOutput $output): void
51
	{
52
		$this->output = $output;
53
	}
54
55
	/**
56
	 * @param string  $message
57
	 * @param mixed ...$arguments
58
	 */
59
	protected function logInfo(string $message, ...$arguments): void
60
	{
61
		if (!$this->logger || !$this->output) {
62
			throw new \LogicException('No logger or output instance set');
63
		}
64
65
		$message = sprintf($message, ...$arguments);
66
		$this->logger->log(ILogger::INFO, $message, [ 'app' => Application::APP_NAME ]);
0 ignored issues
show
Deprecated Code introduced by
The function OCP\ILogger::log() has been deprecated: 20.0.0 use \Psr\Log\LoggerInterface::log ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

66
		/** @scrutinizer ignore-deprecated */ $this->logger->log(ILogger::INFO, $message, [ 'app' => Application::APP_NAME ]);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Deprecated Code introduced by
The constant OCP\ILogger::INFO has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

66
		$this->logger->log(/** @scrutinizer ignore-deprecated */ ILogger::INFO, $message, [ 'app' => Application::APP_NAME ]);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
67
		$this->output->info($message);
68
	}
69
70
	/**
71
	 * @param string $message
72
	 * @param mixed ...$arguments
73
	 */
74
	protected function logWarning(string $message, ...$arguments): void
75
	{
76
		if (!$this->logger || !$this->output) {
77
			throw new \LogicException('No logger or output instance set');
78
		}
79
80
		$message = sprintf($message, ...$arguments);
81
		$this->logger->log(ILogger::WARN, $message, [ 'app' => Application::APP_NAME ]);
0 ignored issues
show
Deprecated Code introduced by
The function OCP\ILogger::log() has been deprecated: 20.0.0 use \Psr\Log\LoggerInterface::log ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

81
		/** @scrutinizer ignore-deprecated */ $this->logger->log(ILogger::WARN, $message, [ 'app' => Application::APP_NAME ]);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Deprecated Code introduced by
The constant OCP\ILogger::WARN has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

81
		$this->logger->log(/** @scrutinizer ignore-deprecated */ ILogger::WARN, $message, [ 'app' => Application::APP_NAME ]);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
82
		$this->output->warning($message);
83
	}
84
85
	/**
86
	 * @param string $title
87
	 * @param array  $newItems
88
	 * @param array  $oldItems
89
	 * @param bool   $warnUpdates
90
	 */
91
	protected function logChanges(string $title, array $newItems, array $oldItems, bool $warnUpdates = false): void
92
	{
93
		if (!$this->logger || !$this->output) {
94
			throw new \LogicException('No logger or output instance set');
95
		}
96
97
		$addedItems = array_diff($newItems, $oldItems);
98
		foreach ($addedItems as $item) {
99
			$this->logInfo('Adding %s "%s"', $title, $item);
100
		}
101
102
		$updatedItems = array_intersect($newItems, $oldItems);
103
		if ($warnUpdates) {
104
			foreach ($updatedItems as $item) {
105
				$this->logWarning('Replacing %s "%s"', $title, $item);
106
			}
107
		} else {
108
			foreach ($updatedItems as $item) {
109
				$this->logInfo('Keeping %s "%s"', $title, $item);
110
			}
111
		}
112
113
		$removedItems = array_diff($oldItems, $newItems);
114
		foreach ($removedItems as $item) {
115
			$this->logWarning('Removing %s "%s"', $title, $item);
116
		}
117
	}
118
}
119