Passed
Pull Request — master (#77)
by Daniel
51:29
created

MigrationTrait   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
eloc 30
c 1
b 0
f 0
dl 0
loc 85
ccs 0
cts 48
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A logWarning() 0 9 3
A setOutput() 0 3 1
B logChanges() 0 25 8
A logInfo() 0 9 3
A setLogger() 0 3 1
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)
43
	{
44
		$this->logger = $logger;
45
	}
46
47
	/**
48
	 * @param IOutput $output
49
	 */
50
	protected function setOutput(IOutput $output)
51
	{
52
		$this->output = $output;
53
	}
54
55
	/**
56
	 * @param string  $message
57
	 * @param mixed[] ...$arguments
58
	 */
59
	protected function logInfo(string $message, ...$arguments)
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 ]);
67
		$this->output->info($message);
68
	}
69
70
	/**
71
	 * @param string $message
72
	 * @param mixed[] ...$arguments
73
	 */
74
	protected function logWarning(string $message, ...$arguments)
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 ]);
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)
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