Completed
Pull Request — master (#274)
by Victor
02:16
created

Checkpoint::getAll()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4.0027

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 21
ccs 17
cts 18
cp 0.9444
rs 9.0534
cc 4
eloc 14
nc 6
nop 0
crap 4.0027
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
		$checkpoints = [];
110 2
		$checkpointDir = $this->locator->getCheckpointDir();
111 2
		$content = $this->fsHelper->isDir($checkpointDir) ? $this->fsHelper->scandir($checkpointDir) : [];
112
113 2
		foreach ($content as $dir){
114 1
			if (in_array($dir, ['.', '..'])){
115
				continue;
116
			}
117 1
			$checkpoints[] = [
118 1
				'title' => $dir,
119 1
				'date' => date(
120 1
					"F d Y H:i:s.", 
121 1
					$this->fsHelper->filemtime(
122 1
						$this->locator->getCheckpointDir() . '/' . $dir
123 1
					)
124 1
				)
125 1
			];
126 2
		}
127 2
		return $checkpoints;
128
	}
129
130
	protected function getCheckpointName(){
131
		$versionString = implode('.', $this->locator->getInstalledVersion());
132
		return uniqid($versionString . '-');
133
	}
134
135
}
136