Completed
Push — stable8.2 ( f4a799...dce584 )
by Thomas
59:22
created

Swift   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 16
c 2
b 0
f 1
lcom 1
cbo 0
dl 0
loc 124
ccs 0
cts 61
cp 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 4
A getStorageId() 0 3 1
A writeObject() 0 4 1
A readObject() 0 16 1
A deleteObject() 0 5 1
A deleteContainer() 0 4 1
C init() 0 29 7
1
<?php
2
/**
3
 * @author Jörn Friedrich Dreyer <[email protected]>
4
 * @author Morris Jobke <[email protected]>
5
 * @author Robin Appelman <[email protected]>
6
 *
7
 * @copyright Copyright (c) 2015, ownCloud, Inc.
8
 * @license AGPL-3.0
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
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, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OC\Files\ObjectStore;
25
26
use Guzzle\Http\Exception\ClientErrorResponseException;
27
use OCP\Files\ObjectStore\IObjectStore;
28
use OpenCloud\OpenStack;
29
use OpenCloud\Rackspace;
30
31
class Swift implements IObjectStore {
32
	/**
33
	 * @var \OpenCloud\OpenStack
34
	 */
35
	private $client;
36
37
	/**
38
	 * @var array
39
	 */
40
	private $params;
41
42
	/**
43
	 * @var \OpenCloud\ObjectStore\Service
44
	 */
45
	private $objectStoreService;
46
47
	/**
48
	 * @var \OpenCloud\ObjectStore\Resource\Container
49
	 */
50
	private $container;
51
52
	public function __construct($params) {
53
		if (!isset($params['container'])) {
54
			$params['container'] = 'owncloud';
55
		}
56
		if (!isset($params['autocreate'])) {
57
			// should only be true for tests
58
			$params['autocreate'] = false;
59
		}
60
61
		if (isset($params['apiKey'])) {
62
			$this->client = new Rackspace($params['url'], $params);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \OpenCloud\Rackspace($params['url'], $params) of type object<OpenCloud\Rackspace> is incompatible with the declared type object<OpenCloud\OpenStack> of property $client.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
63
		} else {
64
			$this->client = new OpenStack($params['url'], $params);
65
		}
66
		$this->params = $params;
67
	}
68
69
	protected function init() {
70
		if ($this->container) {
71
			return;
72
		}
73
74
		// the OpenCloud client library will default to 'cloudFiles' if $serviceName is null
75
		$serviceName = null;
76
		if (isset($this->params['serviceName'])) {
77
			$serviceName = $this->params['serviceName'];
78
		}
79
80
		// the OpenCloud client library will default to 'publicURL' if $urlType is null
81
		$urlType = null;
82
		if (isset($this->params['urlType'])) {
83
			$urlType = $this->params['urlType'];
84
		}
85
		$this->objectStoreService = $this->client->objectStoreService($serviceName, $this->params['region'], $urlType);
86
87
		try {
88
			$this->container = $this->objectStoreService->getContainer($this->params['container']);
89
		} catch (ClientErrorResponseException $ex) {
0 ignored issues
show
Bug introduced by
The class Guzzle\Http\Exception\ClientErrorResponseException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
90
			// if the container does not exist and autocreate is true try to create the container on the fly
91
			if (isset($this->params['autocreate']) && $this->params['autocreate'] === true) {
92
				$this->container = $this->objectStoreService->createContainer($this->params['container']);
93
			} else {
94
				throw $ex;
95
			}
96
		}
97
	}
98
99
	/**
100
	 * @return string the container name where objects are stored
101
	 */
102
	public function getStorageId() {
103
		return $this->params['container'];
104
	}
105
106
	/**
107
	 * @param string $urn the unified resource name used to identify the object
108
	 * @param resource $stream stream with the data to write
109
	 * @throws Exception from openstack lib when something goes wrong
110
	 */
111
	public function writeObject($urn, $stream) {
112
		$this->init();
113
		$this->container->uploadObject($urn, $stream);
114
	}
115
116
	/**
117
	 * @param string $urn the unified resource name used to identify the object
118
	 * @return resource stream with the read data
119
	 * @throws Exception from openstack lib when something goes wrong
120
	 */
121
	public function readObject($urn) {
122
		$this->init();
123
		$object = $this->container->getObject($urn);
124
125
		// we need to keep a reference to objectContent or
126
		// the stream will be closed before we can do anything with it
127
		/** @var $objectContent \Guzzle\Http\EntityBody * */
128
		$objectContent = $object->getContent();
129
		$objectContent->rewind();
130
131
		$stream = $objectContent->getStream();
132
		// save the object content in the context of the stream to prevent it being gc'd until the stream is closed
133
		stream_context_set_option($stream, 'swift','content', $objectContent);
134
135
		return $stream;
136
	}
137
138
	/**
139
	 * @param string $urn Unified Resource Name
140
	 * @return void
141
	 * @throws Exception from openstack lib when something goes wrong
142
	 */
143
	public function deleteObject($urn) {
144
		$this->init();
145
		// see https://github.com/rackspace/php-opencloud/issues/243#issuecomment-30032242
146
		$this->container->dataObject()->setName($urn)->delete();
147
	}
148
149
	public function deleteContainer($recursive = false) {
150
		$this->init();
151
		$this->container->delete($recursive);
152
	}
153
154
}
155