Skip to content

Commit 84163e1

Browse files
authored
Merge pull request #73 from ArnabChatterjee20k/fix-migration-documents-status
Fix migration documents status
2 parents 0dd95b1 + e95e298 commit 84163e1

3 files changed

Lines changed: 88 additions & 43 deletions

File tree

composer.lock

Lines changed: 30 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Migration/Cache.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
class Cache
1414
{
1515
/**
16-
* @var array<string, array<string, Resource>> $cache
16+
* @var array<string, array<string, Resource|string>> $cache
1717
*/
1818
protected array $cache = [];
1919

@@ -39,15 +39,18 @@ public function add(Resource $resource): void
3939
$resource->setInternalId(uniqid());
4040
}
4141

42+
if ($resource->getName() == Resource::TYPE_DOCUMENT) {
43+
$status = $resource->getStatus();
44+
$documentId = $resource->getInternalId();
45+
$this->cache[$resource->getName()][$documentId] = $status;
46+
return;
47+
}
48+
4249
if ($resource->getName() == Resource::TYPE_FILE || $resource->getName() == Resource::TYPE_DEPLOYMENT) {
4350
/** @var File|Deployment $resource */
4451
$resource->setData(''); // Prevent Memory Leak
4552
}
4653

47-
if ($resource->getName() == Resource::TYPE_DOCUMENT) {
48-
return;
49-
}
50-
5154
$this->cache[$resource->getName()][$resource->getInternalId()] = $resource;
5255
}
5356

@@ -75,14 +78,22 @@ public function addAll(array $resources): void
7578
*/
7679
public function update(Resource $resource): void
7780
{
78-
if (! in_array($resource->getName(), $this->cache)) {
79-
$this->add($resource);
80-
}
81-
81+
// if documents then updating the status counter only
8282
if ($resource->getName() == Resource::TYPE_DOCUMENT) {
83+
$documentId = $resource->getInternalId();
84+
if (!isset($this->cache[$resource->getName()][$documentId])) {
85+
$this->add($resource);
86+
} else {
87+
$status = $resource->getStatus();
88+
$this->cache[$resource->getName()][$documentId] = $status;
89+
}
8390
return;
8491
}
8592

93+
if (! in_array($resource->getName(), $this->cache)) {
94+
$this->add($resource);
95+
}
96+
8697
$this->cache[$resource->getName()][$resource->getInternalId()] = $resource;
8798
}
8899

@@ -108,6 +119,11 @@ public function updateAll(array $resources): void
108119
*/
109120
public function remove(Resource $resource): void
110121
{
122+
if ($resource->getName() === Resource::TYPE_DOCUMENT) {
123+
if (! isset($this->cache[$resource->getName()][$resource->getInternalId()])) {
124+
throw new \Exception('Resource does not exist in cache');
125+
}
126+
}
111127
if (! in_array($resource, $this->cache[$resource->getName()])) {
112128
throw new \Exception('Resource does not exist in cache');
113129
}
@@ -133,7 +149,7 @@ public function get(string|Resource $resource): array
133149
/**
134150
* Get All Resources
135151
*
136-
* @return array<string, array<string, Resource>>
152+
* @return array<string, array<string, Resource|string>>
137153
*/
138154
public function getAll(): array
139155
{

src/Migration/Transfer.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,22 @@ public function getStatusCounters(): array
130130
}
131131
}
132132

133-
foreach ($this->cache->getAll() as $resources) {
133+
foreach ($this->cache->getAll() as $resourceType => $resources) {
134134
foreach ($resources as $resource) {
135+
if ($resourceType === Resource::TYPE_DOCUMENT && is_string($resource)) {
136+
$documentStatus = $resource;
137+
$status[$resourceType][$documentStatus]++;
138+
139+
if ($status[$resourceType]['pending'] > 0) {
140+
$status[$resourceType]['pending']--;
141+
}
142+
143+
continue;
144+
}
145+
135146
if (isset($status[$resource->getName()])) {
136147
$status[$resource->getName()][$resource->getStatus()]++;
148+
137149
if ($status[$resource->getName()]['pending'] > 0) {
138150
$status[$resource->getName()]['pending']--;
139151
}
@@ -260,11 +272,28 @@ public function getCurrentResource(): string
260272
public function getReport(string $statusLevel = ''): array
261273
{
262274
$report = [];
263-
264275
$cache = $this->cache->getAll();
265276

266277
foreach ($cache as $type => $resources) {
267-
foreach ($resources as $resource) {
278+
foreach ($resources as $id => $resource) {
279+
if ($type === Resource::TYPE_DOCUMENT && is_string($resource)) {
280+
if ($statusLevel && $resource !== $statusLevel) {
281+
continue;
282+
}
283+
// no message for document is stored
284+
$report[] = [
285+
'resource' => $type,
286+
'id' => $id,
287+
'status' => $resource,
288+
'message' => '',
289+
];
290+
continue;
291+
}
292+
293+
if (!is_object($resource)) {
294+
continue;
295+
}
296+
268297
if ($statusLevel && $resource->getStatus() !== $statusLevel) {
269298
continue;
270299
}

0 commit comments

Comments
 (0)