1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
declare(strict_types=1);
namespace Kreait\Firebase;
use Google\Cloud\Storage\Bucket;
use Google\Cloud\Storage\StorageClient;
use Kreait\Firebase\Exception\RuntimeException;
class Storage implements Contract\Storage
{
private StorageClient $storageClient;
private ?string $defaultBucket;
/** @var Bucket[] */
private array $buckets = [];
/**
* @internal
*/
public function __construct(StorageClient $storageClient, ?string $defaultBucket = null)
{
$this->storageClient = $storageClient;
$this->defaultBucket = $defaultBucket;
}
public function getStorageClient(): StorageClient
{
return $this->storageClient;
}
public function getBucket(?string $name = null): Bucket
{
$name = $name ?: $this->defaultBucket;
if ($name === null) {
throw new RuntimeException(
'No bucket name was given and no default bucked was configured.'
);
}
if (!\array_key_exists($name, $this->buckets)) {
$this->buckets[$name] = $this->storageClient->bucket($name);
}
return $this->buckets[$name];
}
}