-
Notifications
You must be signed in to change notification settings - Fork 1
/
Transfer.php
165 lines (128 loc) · 4.59 KB
/
Transfer.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
namespace Utopia\Transfer;
class Transfer
{
public const GROUP_GENERAL = 'general';
public const GROUP_AUTH = 'auth';
public const GROUP_STORAGE = 'storage';
public const GROUP_FUNCTIONS = 'functions';
public const GROUP_DATABASES = 'databases';
public const GROUP_SETTINGS = 'settings';
public const GROUP_AUTH_RESOURCES = [Resource::TYPE_USER, Resource::TYPE_TEAM, Resource::TYPE_MEMBERSHIP, Resource::TYPE_HASH];
public const GROUP_STORAGE_RESOURCES = [Resource::TYPE_FILE, Resource::TYPE_BUCKET];
public const GROUP_FUNCTIONS_RESOURCES = [Resource::TYPE_FUNCTION, Resource::TYPE_ENVVAR, Resource::TYPE_DEPLOYMENT];
public const GROUP_DATABASES_RESOURCES = [Resource::TYPE_DATABASE, Resource::TYPE_COLLECTION, Resource::TYPE_INDEX, Resource::TYPE_ATTRIBUTE, Resource::TYPE_DOCUMENT];
public const GROUP_SETTINGS_RESOURCES = [];
public const ALL_PUBLIC_RESOURCES = [
Resource::TYPE_USER, Resource::TYPE_TEAM,
Resource::TYPE_MEMBERSHIP, Resource::TYPE_FILE,
Resource::TYPE_BUCKET, Resource::TYPE_FUNCTION,
Resource::TYPE_ENVVAR, Resource::TYPE_DEPLOYMENT,
Resource::TYPE_DATABASE, Resource::TYPE_COLLECTION,
Resource::TYPE_INDEX, Resource::TYPE_ATTRIBUTE,
Resource::TYPE_DOCUMENT,
];
public const STORAGE_MAX_CHUNK_SIZE = 1024 * 1024 * 5; // 5MB
/**
* @return Transfer
*/
public function __construct(Source $source, Destination $destination)
{
$this->source = $source;
$this->destination = $destination;
$this->cache = new Cache();
$this->source->registerCache($this->cache);
$this->destination->registerCache($this->cache);
$this->destination->setSource($source);
return $this;
}
protected Source $source;
protected Destination $destination;
protected string $currentResource;
/**
* A local cache of resources that were transferred.
*/
protected Cache $cache;
protected array $options = [];
protected array $callbacks = [];
protected array $events = [];
public function getStatusCounters()
{
$status = [];
foreach ($this->cache->getAll() as $resources) {
foreach ($resources as $resource) {
/** @var Resource $resource */
if (!array_key_exists($resource->getName(), $status)) {
$status[$resource->getName()] = [
Resource::STATUS_PENDING => 0,
Resource::STATUS_SUCCESS => 0,
Resource::STATUS_ERROR => 0,
Resource::STATUS_SKIPPED => 0,
Resource::STATUS_PROCESSING => 0,
Resource::STATUS_WARNING => 0,
];
}
$status[$resource->getName()][$resource->getStatus()]++;
}
}
return $status;
}
/**
* Transfer Resources between adapters
*
* @param callable $callback (array $resources)
*/
public function run(array $resources, callable $callback): void
{
// Allows you to push entire groups if you want.
$computedResources = [];
foreach ($resources as $resource) {
if (is_array($resource)) {
$computedResources = array_merge($computedResources, $resource);
} else {
$computedResources[] = $resource;
}
}
$this->destination->run($computedResources, $callback, $this->source);
}
/**
* Get Cache
*/
public function getCache(): Cache
{
return $this->cache;
}
/**
* Get Current Resource
*
**/
public function getCurrentResource(): string
{
return $this->currentResource;
}
/**
* Get Transfer Report
*
* @param string $statusLevel
* If no status level is provided, all status types will be returned.
*/
public function getReport(string $statusLevel = ''): array
{
$report = [];
$cache = $this->cache->getAll();
foreach ($cache as $type => $resources) {
foreach ($resources as $resource) {
if ($statusLevel && $resource->getStatus() !== $statusLevel) {
continue;
}
$report[] = [
'resource' => $type,
'id' => $resource->getId(),
'status' => $resource->getStatus(),
'message' => $resource->getReason(),
];
}
}
return $report;
}
}