Completed
Pull Request — master (#254)
by Victor
29:22 queued 02:10
created

Checkpoint   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 22.22%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
c 2
b 0
f 0
lcom 1
cbo 3
dl 0
loc 98
ccs 12
cts 54
cp 0.2222
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A restore() 0 10 2
A getCheckpointName() 0 4 1
B create() 0 43 6
A getAll() 0 11 2
1
<?php
2
/**
3
 * @author Victor Dubiniuk <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2015, ownCloud, Inc.
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 Owncloud\Updater\Utils;
23
24
use Owncloud\Updater\Console\Application;
25
use Owncloud\Updater\Utils\FilesystemHelper;
26
use Owncloud\Updater\Utils\Locator;
27
28
class Checkpoint {
29
30
	const CORE_DIR = 'core';
31
	const THIRDPARTY_DIR = '3rdparty';
32
	const APP_DIR = 'apps';
33
34
	/**
35
	 * @var Locator $locator
36
	 */
37
	protected $locator;
38
39
	/**
40
	 * @var Filesystemhelper $fsHelper
41
	 */
42
	protected $fsHelper;
43
44
	/**
45
	 *
46
	 * @param Locator $locator
47
	 */
48 2
	public function __construct(Locator $locator, FilesystemHelper $fsHelper){
49 2
		$this->locator = $locator;
50 2
		$this->fsHelper = $fsHelper;
51 2
	}
52
53
	public function create(){
54
		$checkpointName = $this->getCheckpointName();
55
		$checkpointPath = $this->locator->getCheckpointDir() . '/' . $checkpointName;
56
		try{
57
			if (!$this->fsHelper->isWritable($this->locator->getCheckpointDir())){
58
				throw new \Exception($this->locator->getCheckpointDir() . ' is not writable.');
59
			}
60
			$this->fsHelper->mkdir($checkpointPath);
61
62
			$checkpointCorePath = $checkpointPath . '/' . self::CORE_DIR;
63
			$this->fsHelper->mkdir($checkpointCorePath);
64
			$core = $this->locator->getRootDirItems();
65
			foreach ($core as $coreItem){
66
				$cpItemPath = $checkpointCorePath . '/' . basename($coreItem);
67
				$this->fsHelper->copyr($coreItem, $cpItemPath, true);
68
			}
69
			//copy config.php
70
			$configDirSrc = $this->locator->getOwncloudRootPath() . '/config';
71
			$configDirDst = $checkpointCorePath . '/config';
72
			$this->fsHelper->copyr($configDirSrc, $configDirDst, true);
73
74
			//copy 3rdparty
75
			$this->fsHelper->copyr($this->locator->getOwncloudRootPath() . '/' . self::THIRDPARTY_DIR, $checkpointCorePath . '/' . self::THIRDPARTY_DIR, true);
76
77
			$checkpointAppPath = $checkpointPath . '/' . self::APP_DIR;
78
			$this->fsHelper->mkdir($checkpointAppPath);
79
			$appManager = Application::$container['utils.appmanager'];
80
			$apps = $appManager->getAllApps();
81
			foreach ($apps as $appId){
82
				$appPath = $appManager->getAppPath($appId);
83
				if ($appPath){
84
					$this->fsHelper->copyr($appPath, $checkpointAppPath . '/' . $appId, true);
85
				}
86
			}
87
88
		} catch (\Exception $e){
89
			$application = Application::$container['application'];
90
			$application->getLogger()->error($e->getMessage());
91
			$this->fsHelper->removeIfExists($checkpointPath);
92
			throw $e;
93
		}
94
		return $checkpointName;
95
	}
96
97
	public function restore($checkpointId){
98
		$checkpointDir = $this->locator->getCheckpointDir() . '/' . $checkpointId;
99
		if (!$this->fsHelper->fileExists($checkpointDir)){
100
			$message = sprintf('Checkpoint %s does not exist.', $checkpointId);
101
			throw new \UnexpectedValueException($message);
102
		}
103
		$ocRoot = $this->locator->getOwncloudRootPath();
104
		$this->fsHelper->copyr($checkpointDir . '/' . self::CORE_DIR, $ocRoot, false);
105
		$this->fsHelper->copyr($checkpointDir . '/' . self::APP_DIR, $ocRoot . '/' . self::APP_DIR, false);
106
	}
107
108 2
	public function getAll(){
109 2
		$checkpointDir = $this->locator->getCheckpointDir();
110 2
		$content = $this->fsHelper->isDir($checkpointDir) ? $this->fsHelper->scandir($checkpointDir) : [];
111 2
		$checkpoints = array_filter(
112
				$content,
113 2
				function($dir){
114 1
					return !in_array($dir, ['.', '..']);
115 2
				}
116
		);
117 2
		return $checkpoints;
118
	}
119
120
	protected function getCheckpointName(){
121
		$versionString = implode('.', $this->locator->getInstalledVersion());
122
		return uniqid($versionString . '-');
123
	}
124
125
}
126