Passed
Push — master ( 570c25...c25daf )
by Morris
15:03 queued 13s
created
lib/private/Files/Cache/Storage.php 2 patches
Indentation   +175 added lines, -175 removed lines patch added patch discarded remove patch
@@ -44,179 +44,179 @@
 block discarded – undo
44 44
  * @package OC\Files\Cache
45 45
  */
46 46
 class Storage {
47
-	/** @var StorageGlobal|null */
48
-	private static $globalCache = null;
49
-	private $storageId;
50
-	private $numericId;
51
-
52
-	/**
53
-	 * @return StorageGlobal
54
-	 */
55
-	public static function getGlobalCache() {
56
-		if (is_null(self::$globalCache)) {
57
-			self::$globalCache = new StorageGlobal(\OC::$server->getDatabaseConnection());
58
-		}
59
-		return self::$globalCache;
60
-	}
61
-
62
-	/**
63
-	 * @param \OC\Files\Storage\Storage|string $storage
64
-	 * @param bool $isAvailable
65
-	 * @throws \RuntimeException
66
-	 */
67
-	public function __construct($storage, $isAvailable = true) {
68
-		if ($storage instanceof IStorage) {
69
-			$this->storageId = $storage->getId();
70
-		} else {
71
-			$this->storageId = $storage;
72
-		}
73
-		$this->storageId = self::adjustStorageId($this->storageId);
74
-
75
-		if ($row = self::getStorageById($this->storageId)) {
76
-			$this->numericId = (int)$row['numeric_id'];
77
-		} else {
78
-			$connection = \OC::$server->getDatabaseConnection();
79
-			$available = $isAvailable ? 1 : 0;
80
-			if ($connection->insertIfNotExist('*PREFIX*storages', ['id' => $this->storageId, 'available' => $available])) {
81
-				$this->numericId = $connection->lastInsertId('*PREFIX*storages');
82
-			} else {
83
-				if ($row = self::getStorageById($this->storageId)) {
84
-					$this->numericId = (int)$row['numeric_id'];
85
-				} else {
86
-					throw new \RuntimeException('Storage could neither be inserted nor be selected from the database: ' . $this->storageId);
87
-				}
88
-			}
89
-		}
90
-	}
91
-
92
-	/**
93
-	 * @param string $storageId
94
-	 * @return array
95
-	 */
96
-	public static function getStorageById($storageId) {
97
-		return self::getGlobalCache()->getStorageInfo($storageId);
98
-	}
99
-
100
-	/**
101
-	 * Adjusts the storage id to use md5 if too long
102
-	 * @param string $storageId storage id
103
-	 * @return string unchanged $storageId if its length is less than 64 characters,
104
-	 * else returns the md5 of $storageId
105
-	 */
106
-	public static function adjustStorageId($storageId) {
107
-		if (strlen($storageId) > 64) {
108
-			return md5($storageId);
109
-		}
110
-		return $storageId;
111
-	}
112
-
113
-	/**
114
-	 * Get the numeric id for the storage
115
-	 *
116
-	 * @return int
117
-	 */
118
-	public function getNumericId() {
119
-		return $this->numericId;
120
-	}
121
-
122
-	/**
123
-	 * Get the string id for the storage
124
-	 *
125
-	 * @param int $numericId
126
-	 * @return string|null either the storage id string or null if the numeric id is not known
127
-	 */
128
-	public static function getStorageId($numericId) {
129
-		$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
130
-		$query->select('id')
131
-			->from('storages')
132
-			->where($query->expr()->eq('numeric_id', $query->createNamedParameter($numericId)));
133
-		$result = $query->execute();
134
-		$row = $result->fetch();
135
-		$result->closeCursor();
136
-		if ($row) {
137
-			return $row['id'];
138
-		} else {
139
-			return null;
140
-		}
141
-	}
142
-
143
-	/**
144
-	 * Get the numeric of the storage with the provided string id
145
-	 *
146
-	 * @param $storageId
147
-	 * @return int|null either the numeric storage id or null if the storage id is not knwon
148
-	 */
149
-	public static function getNumericStorageId($storageId) {
150
-		$storageId = self::adjustStorageId($storageId);
151
-
152
-		if ($row = self::getStorageById($storageId)) {
153
-			return (int)$row['numeric_id'];
154
-		} else {
155
-			return null;
156
-		}
157
-	}
158
-
159
-	/**
160
-	 * @return array|null [ available, last_checked ]
161
-	 */
162
-	public function getAvailability() {
163
-		if ($row = self::getStorageById($this->storageId)) {
164
-			return [
165
-				'available' => (int)$row['available'] === 1,
166
-				'last_checked' => $row['last_checked']
167
-			];
168
-		} else {
169
-			return null;
170
-		}
171
-	}
172
-
173
-	/**
174
-	 * @param bool $isAvailable
175
-	 * @param int $delay amount of seconds to delay reconsidering that storage further
176
-	 */
177
-	public function setAvailability($isAvailable, int $delay = 0) {
178
-		$available = $isAvailable ? 1 : 0;
179
-		if (!$isAvailable) {
180
-			\OC::$server->get(LoggerInterface::class)->info('Storage with ' . $this->storageId . ' marked as unavailable', ['app' => 'lib']);
181
-		}
182
-
183
-		$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
184
-		$query->update('storages')
185
-			->set('available', $query->createNamedParameter($available))
186
-			->set('last_checked', $query->createNamedParameter(time() + $delay))
187
-			->where($query->expr()->eq('id', $query->createNamedParameter($this->storageId)));
188
-		$query->execute();
189
-	}
190
-
191
-	/**
192
-	 * Check if a string storage id is known
193
-	 *
194
-	 * @param string $storageId
195
-	 * @return bool
196
-	 */
197
-	public static function exists($storageId) {
198
-		return !is_null(self::getNumericStorageId($storageId));
199
-	}
200
-
201
-	/**
202
-	 * remove the entry for the storage
203
-	 *
204
-	 * @param string $storageId
205
-	 */
206
-	public static function remove($storageId) {
207
-		$storageId = self::adjustStorageId($storageId);
208
-		$numericId = self::getNumericStorageId($storageId);
209
-
210
-		$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
211
-		$query->delete('storages')
212
-			->where($query->expr()->eq('id', $query->createNamedParameter($storageId)));
213
-		$query->execute();
214
-
215
-		if (!is_null($numericId)) {
216
-			$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
217
-			$query->delete('filecache')
218
-				->where($query->expr()->eq('storage', $query->createNamedParameter($numericId)));
219
-			$query->execute();
220
-		}
221
-	}
47
+    /** @var StorageGlobal|null */
48
+    private static $globalCache = null;
49
+    private $storageId;
50
+    private $numericId;
51
+
52
+    /**
53
+     * @return StorageGlobal
54
+     */
55
+    public static function getGlobalCache() {
56
+        if (is_null(self::$globalCache)) {
57
+            self::$globalCache = new StorageGlobal(\OC::$server->getDatabaseConnection());
58
+        }
59
+        return self::$globalCache;
60
+    }
61
+
62
+    /**
63
+     * @param \OC\Files\Storage\Storage|string $storage
64
+     * @param bool $isAvailable
65
+     * @throws \RuntimeException
66
+     */
67
+    public function __construct($storage, $isAvailable = true) {
68
+        if ($storage instanceof IStorage) {
69
+            $this->storageId = $storage->getId();
70
+        } else {
71
+            $this->storageId = $storage;
72
+        }
73
+        $this->storageId = self::adjustStorageId($this->storageId);
74
+
75
+        if ($row = self::getStorageById($this->storageId)) {
76
+            $this->numericId = (int)$row['numeric_id'];
77
+        } else {
78
+            $connection = \OC::$server->getDatabaseConnection();
79
+            $available = $isAvailable ? 1 : 0;
80
+            if ($connection->insertIfNotExist('*PREFIX*storages', ['id' => $this->storageId, 'available' => $available])) {
81
+                $this->numericId = $connection->lastInsertId('*PREFIX*storages');
82
+            } else {
83
+                if ($row = self::getStorageById($this->storageId)) {
84
+                    $this->numericId = (int)$row['numeric_id'];
85
+                } else {
86
+                    throw new \RuntimeException('Storage could neither be inserted nor be selected from the database: ' . $this->storageId);
87
+                }
88
+            }
89
+        }
90
+    }
91
+
92
+    /**
93
+     * @param string $storageId
94
+     * @return array
95
+     */
96
+    public static function getStorageById($storageId) {
97
+        return self::getGlobalCache()->getStorageInfo($storageId);
98
+    }
99
+
100
+    /**
101
+     * Adjusts the storage id to use md5 if too long
102
+     * @param string $storageId storage id
103
+     * @return string unchanged $storageId if its length is less than 64 characters,
104
+     * else returns the md5 of $storageId
105
+     */
106
+    public static function adjustStorageId($storageId) {
107
+        if (strlen($storageId) > 64) {
108
+            return md5($storageId);
109
+        }
110
+        return $storageId;
111
+    }
112
+
113
+    /**
114
+     * Get the numeric id for the storage
115
+     *
116
+     * @return int
117
+     */
118
+    public function getNumericId() {
119
+        return $this->numericId;
120
+    }
121
+
122
+    /**
123
+     * Get the string id for the storage
124
+     *
125
+     * @param int $numericId
126
+     * @return string|null either the storage id string or null if the numeric id is not known
127
+     */
128
+    public static function getStorageId($numericId) {
129
+        $query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
130
+        $query->select('id')
131
+            ->from('storages')
132
+            ->where($query->expr()->eq('numeric_id', $query->createNamedParameter($numericId)));
133
+        $result = $query->execute();
134
+        $row = $result->fetch();
135
+        $result->closeCursor();
136
+        if ($row) {
137
+            return $row['id'];
138
+        } else {
139
+            return null;
140
+        }
141
+    }
142
+
143
+    /**
144
+     * Get the numeric of the storage with the provided string id
145
+     *
146
+     * @param $storageId
147
+     * @return int|null either the numeric storage id or null if the storage id is not knwon
148
+     */
149
+    public static function getNumericStorageId($storageId) {
150
+        $storageId = self::adjustStorageId($storageId);
151
+
152
+        if ($row = self::getStorageById($storageId)) {
153
+            return (int)$row['numeric_id'];
154
+        } else {
155
+            return null;
156
+        }
157
+    }
158
+
159
+    /**
160
+     * @return array|null [ available, last_checked ]
161
+     */
162
+    public function getAvailability() {
163
+        if ($row = self::getStorageById($this->storageId)) {
164
+            return [
165
+                'available' => (int)$row['available'] === 1,
166
+                'last_checked' => $row['last_checked']
167
+            ];
168
+        } else {
169
+            return null;
170
+        }
171
+    }
172
+
173
+    /**
174
+     * @param bool $isAvailable
175
+     * @param int $delay amount of seconds to delay reconsidering that storage further
176
+     */
177
+    public function setAvailability($isAvailable, int $delay = 0) {
178
+        $available = $isAvailable ? 1 : 0;
179
+        if (!$isAvailable) {
180
+            \OC::$server->get(LoggerInterface::class)->info('Storage with ' . $this->storageId . ' marked as unavailable', ['app' => 'lib']);
181
+        }
182
+
183
+        $query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
184
+        $query->update('storages')
185
+            ->set('available', $query->createNamedParameter($available))
186
+            ->set('last_checked', $query->createNamedParameter(time() + $delay))
187
+            ->where($query->expr()->eq('id', $query->createNamedParameter($this->storageId)));
188
+        $query->execute();
189
+    }
190
+
191
+    /**
192
+     * Check if a string storage id is known
193
+     *
194
+     * @param string $storageId
195
+     * @return bool
196
+     */
197
+    public static function exists($storageId) {
198
+        return !is_null(self::getNumericStorageId($storageId));
199
+    }
200
+
201
+    /**
202
+     * remove the entry for the storage
203
+     *
204
+     * @param string $storageId
205
+     */
206
+    public static function remove($storageId) {
207
+        $storageId = self::adjustStorageId($storageId);
208
+        $numericId = self::getNumericStorageId($storageId);
209
+
210
+        $query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
211
+        $query->delete('storages')
212
+            ->where($query->expr()->eq('id', $query->createNamedParameter($storageId)));
213
+        $query->execute();
214
+
215
+        if (!is_null($numericId)) {
216
+            $query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
217
+            $query->delete('filecache')
218
+                ->where($query->expr()->eq('storage', $query->createNamedParameter($numericId)));
219
+            $query->execute();
220
+        }
221
+    }
222 222
 }
Please login to merge, or discard this patch.
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -73,7 +73,7 @@  discard block
 block discarded – undo
73 73
 		$this->storageId = self::adjustStorageId($this->storageId);
74 74
 
75 75
 		if ($row = self::getStorageById($this->storageId)) {
76
-			$this->numericId = (int)$row['numeric_id'];
76
+			$this->numericId = (int) $row['numeric_id'];
77 77
 		} else {
78 78
 			$connection = \OC::$server->getDatabaseConnection();
79 79
 			$available = $isAvailable ? 1 : 0;
@@ -81,9 +81,9 @@  discard block
 block discarded – undo
81 81
 				$this->numericId = $connection->lastInsertId('*PREFIX*storages');
82 82
 			} else {
83 83
 				if ($row = self::getStorageById($this->storageId)) {
84
-					$this->numericId = (int)$row['numeric_id'];
84
+					$this->numericId = (int) $row['numeric_id'];
85 85
 				} else {
86
-					throw new \RuntimeException('Storage could neither be inserted nor be selected from the database: ' . $this->storageId);
86
+					throw new \RuntimeException('Storage could neither be inserted nor be selected from the database: '.$this->storageId);
87 87
 				}
88 88
 			}
89 89
 		}
@@ -150,7 +150,7 @@  discard block
 block discarded – undo
150 150
 		$storageId = self::adjustStorageId($storageId);
151 151
 
152 152
 		if ($row = self::getStorageById($storageId)) {
153
-			return (int)$row['numeric_id'];
153
+			return (int) $row['numeric_id'];
154 154
 		} else {
155 155
 			return null;
156 156
 		}
@@ -162,7 +162,7 @@  discard block
 block discarded – undo
162 162
 	public function getAvailability() {
163 163
 		if ($row = self::getStorageById($this->storageId)) {
164 164
 			return [
165
-				'available' => (int)$row['available'] === 1,
165
+				'available' => (int) $row['available'] === 1,
166 166
 				'last_checked' => $row['last_checked']
167 167
 			];
168 168
 		} else {
@@ -177,7 +177,7 @@  discard block
 block discarded – undo
177 177
 	public function setAvailability($isAvailable, int $delay = 0) {
178 178
 		$available = $isAvailable ? 1 : 0;
179 179
 		if (!$isAvailable) {
180
-			\OC::$server->get(LoggerInterface::class)->info('Storage with ' . $this->storageId . ' marked as unavailable', ['app' => 'lib']);
180
+			\OC::$server->get(LoggerInterface::class)->info('Storage with '.$this->storageId.' marked as unavailable', ['app' => 'lib']);
181 181
 		}
182 182
 
183 183
 		$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
Please login to merge, or discard this patch.