forked from thephpleague/flysystem-google-cloud-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StubRiggedBucket.php
53 lines (40 loc) · 1.33 KB
/
StubRiggedBucket.php
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
51
52
53
<?php
declare(strict_types=1);
namespace League\Flysystem\GoogleCloudStorage;
use Google\Cloud\Storage\Bucket;
use LogicException;
use Throwable;
class StubRiggedBucket extends Bucket
{
private array $triggers = [];
public function failForObject($name, ?Throwable $throwable = null): void
{
$this->setupTrigger('object', $name, $throwable);
}
public function failForUpload($name, ?Throwable $throwable = null): void
{
$this->setupTrigger('upload', $name, $throwable);
}
public function object($name, array $options = [])
{
$this->pushTrigger('object', $name);
return parent::object($name, $options);
}
public function upload($data, array $options = [])
{
$this->pushTrigger('upload', $options['name'] ?? 'unknown-object-name');
return parent::upload($data, $options);
}
private function setupTrigger(string $method, string $name, ?Throwable $throwable): void
{
$this->triggers[$method][$name] = $throwable ?: new LogicException('unknown error');
}
private function pushTrigger(string $method, string $name): void
{
$trigger = $this->triggers[$method][$name] ?? null;
if ($trigger instanceof Throwable) {
unset($this->triggers[$method][$name]);
throw $trigger;
}
}
}