Completed
Push — master ( 2d2c9e...bc008f )
by Morris
168:24 queued 146:13
created

Azure::getBlobClient()   C

Complexity

Conditions 7
Paths 16

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 7
eloc 15
c 1
b 0
f 1
nc 16
nop 0
dl 0
loc 23
rs 6.7272
1
<?php
2
/**
3
 * @copyright Copyright (c) 2018 Robin Appelman <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
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
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OC\Files\ObjectStore;
23
24
use MicrosoftAzure\Storage\Blob\BlobRestProxy;
25
use MicrosoftAzure\Storage\Common\Exceptions\ServiceException;
26
use OCP\Files\ObjectStore\IObjectStore;
27
28
class Azure implements IObjectStore {
29
	/** @var string */
30
	private $containerName;
31
	/** @var string */
32
	private $accountName;
33
	/** @var string */
34
	private $accountKey;
35
	/** @var BlobRestProxy|null */
36
	private $blobClient = null;
37
	/** @var string|null */
38
	private $endpoint = null;
39
	/** @var bool  */
40
	private $autoCreate = false;
41
42
	/**
43
	 * @param array $parameters
44
	 */
45
	public function __construct($parameters) {
46
		$this->containerName = $parameters['container'];
47
		$this->accountName = $parameters['account_name'];
48
		$this->accountKey = $parameters['account_key'];
49
		if (isset($parameters['endpoint'])) {
50
			$this->endpoint = $parameters['endpoint'];
51
		}
52
		if (isset($parameters['autocreate'])) {
53
			$this->autoCreate = $parameters['autocreate'];
54
		}
55
	}
56
57
	/**
58
	 * @return BlobRestProxy
59
	 */
60
	private function getBlobClient() {
61
		if (!$this->blobClient) {
62
			$protocol = $this->endpoint ? substr($this->endpoint, 0, strpos($this->endpoint, ':')) : 'https';
63
			$connectionString = "DefaultEndpointsProtocol=" . $protocol . ";AccountName=" . $this->accountName . ";AccountKey=" . $this->accountKey;
64
			if ($this->endpoint) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->endpoint of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
65
				$connectionString .= ';BlobEndpoint=' . $this->endpoint;
66
			}
67
			$this->blobClient = BlobRestProxy::createBlobService($connectionString);
68
69
			if ($this->autoCreate) {
70
				try {
71
					$this->blobClient->createContainer($this->containerName);
72
				} catch (ServiceException $e) {
0 ignored issues
show
Bug introduced by
The class MicrosoftAzure\Storage\C...ptions\ServiceException 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...
73
					if ($e->getCode() === 409) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
74
						// already exists
75
					} else {
76
						throw $e;
77
					}
78
				}
79
			}
80
		}
81
		return $this->blobClient;
82
	}
83
84
	/**
85
	 * @return string the container or bucket name where objects are stored
86
	 */
87
	public function getStorageId() {
88
		return 'azure::blob::' . $this->containerName;
89
	}
90
91
	/**
92
	 * @param string $urn the unified resource name used to identify the object
93
	 * @return resource stream with the read data
94
	 * @throws \Exception when something goes wrong, message will be logged
95
	 */
96
	public function readObject($urn) {
97
		$blob = $this->getBlobClient()->getBlob($this->containerName, $urn);
98
		return $blob->getContentStream();
99
	}
100
101
	/**
102
	 * @param string $urn the unified resource name used to identify the object
103
	 * @param resource $stream stream with the data to write
104
	 * @throws \Exception when something goes wrong, message will be logged
105
	 */
106
	public function writeObject($urn, $stream) {
107
		$this->getBlobClient()->createBlockBlob($this->containerName, $urn, $stream);
108
	}
109
110
	/**
111
	 * @param string $urn the unified resource name used to identify the object
112
	 * @return void
113
	 * @throws \Exception when something goes wrong, message will be logged
114
	 */
115
	public function deleteObject($urn) {
116
		$this->getBlobClient()->deleteBlob($this->containerName, $urn);
117
	}
118
}
119